Created
July 10, 2012 06:37
-
-
Save Zren/3081608 to your computer and use it in GitHub Desktop.
Annotated Fields for Serialization
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package ca.xshade.bukkit.util.config; | |
| import org.bukkit.util.Vector; | |
| public class AnnotatedNodeObject { | |
| @Node(node = "testBlarg") | |
| String test; | |
| @Node(node = "vector") | |
| Vector vector; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package ca.xshade.bukkit.util.config; | |
| import java.lang.reflect.Field; | |
| import java.lang.reflect.InvocationTargetException; | |
| import java.lang.reflect.Method; | |
| import java.util.LinkedHashMap; | |
| import java.util.Map; | |
| import org.apache.commons.lang.StringUtils; | |
| import org.bukkit.configuration.serialization.ConfigurationSerializable; | |
| public class ConfigUtil { | |
| public static Map<String, Object> serialize(Object instance) { | |
| if (instance == null) | |
| return null; | |
| Map<String, Object> result = new LinkedHashMap<String, Object>(); | |
| Class<?> clazz = instance.getClass(); | |
| for (Field f : clazz.getDeclaredFields()) { | |
| if (f.isAnnotationPresent(Node.class)) { | |
| Node nodeAnnotation = f.getAnnotation(Node.class); | |
| String node = nodeAnnotation.node(); | |
| if (node == null) | |
| continue; | |
| Object fieldObj = null; | |
| try { | |
| fieldObj = f.get(instance); | |
| } catch (IllegalArgumentException e1) { | |
| // TODO Auto-generated catch block | |
| e1.printStackTrace(); | |
| } catch (IllegalAccessException e1) { | |
| try { | |
| String getterName = "get" + StringUtils.capitalize(f.getName()); | |
| Method m = clazz.getMethod(getterName); | |
| fieldObj = m.invoke(instance); | |
| } catch (IllegalArgumentException | InvocationTargetException | IllegalAccessException | NoSuchMethodException | SecurityException e) { | |
| // TODO Auto-generated catch block | |
| e.printStackTrace(); | |
| } | |
| } | |
| if (fieldObj == null) | |
| continue; | |
| if (fieldObj instanceof ConfigurationSerializable) { | |
| ConfigurationSerializable configurationSerializable = (ConfigurationSerializable) fieldObj; | |
| fieldObj = configurationSerializable.serialize(); | |
| } | |
| result.put(node, fieldObj); | |
| } | |
| } | |
| return result; | |
| } | |
| public static Object deserialize(Class<?> clazz, Map<String, Object> args) { | |
| Object instance; | |
| try { | |
| instance = clazz.newInstance(); | |
| } catch (InstantiationException | IllegalAccessException e1) { | |
| // TODO Auto-generated catch block | |
| e1.printStackTrace(); | |
| return null; | |
| } | |
| for (Field f : clazz.getDeclaredFields()) { | |
| if (f.isAnnotationPresent(Node.class)) { | |
| Node nodeAnnotation = f.getAnnotation(Node.class); | |
| String node = nodeAnnotation.node(); | |
| System.out.format("node = %s%n", node); | |
| if (node == null) | |
| continue; | |
| Object nodeObj = args.get(node); | |
| System.out.format("nodeObj = %s%n", nodeObj); | |
| if (nodeObj == null) | |
| continue; | |
| if (ConfigurationSerializable.class.isAssignableFrom(f.getType())) { | |
| try { | |
| Method m = f.getType().getMethod("deserialize", Map.class); | |
| nodeObj = m.invoke(null, nodeObj); | |
| } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { | |
| // TODO Auto-generated catch block | |
| e.printStackTrace(); | |
| } | |
| } | |
| System.out.format("nodeObj = %s%n", nodeObj); | |
| if (f.getType().isInstance(nodeObj)) { | |
| try { | |
| f.set(instance, nodeObj); | |
| } catch (IllegalAccessException e) { | |
| try { | |
| String setterName = "set" + StringUtils.capitalize(f.getName()); | |
| Method m = clazz.getMethod(setterName, f.getType()); | |
| m.invoke(instance, nodeObj); | |
| } catch (NoSuchMethodException | SecurityException | IllegalArgumentException | InvocationTargetException | IllegalAccessException e2) { | |
| // TODO Auto-generated catch block | |
| e2.printStackTrace(); | |
| } | |
| } | |
| } | |
| } | |
| } | |
| return instance; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package ca.xshade.bukkit.util.config; | |
| import java.io.File; | |
| import java.util.Map; | |
| import org.bukkit.configuration.file.YamlConfiguration; | |
| import org.bukkit.util.Vector; | |
| import org.junit.Before; | |
| import org.junit.Test; | |
| public class ConfigUtilTest { | |
| File file; | |
| YamlConfiguration config; | |
| @Before | |
| public void before() { | |
| config = new YamlConfiguration(); | |
| } | |
| @Test | |
| public void testAnnotatedNodes() { | |
| AnnotatedNodeObject obj = new AnnotatedNodeObject(); | |
| obj.test = "blarg"; | |
| obj.vector = new Vector(); | |
| config.set("test", ConfigUtil.serialize(obj)); | |
| System.out.println(config.saveToString()); | |
| Object value = config.get("test"); | |
| Map<String, Object> listMap = (Map<String, Object>) value; | |
| obj = (AnnotatedNodeObject) ConfigUtil.deserialize(AnnotatedNodeObject.class, listMap); | |
| System.out.format("%s%n", obj.test); | |
| System.out.format("%s%n", obj.vector); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package ca.xshade.bukkit.util.config; | |
| import java.lang.annotation.ElementType; | |
| import java.lang.annotation.Retention; | |
| import java.lang.annotation.RetentionPolicy; | |
| import java.lang.annotation.Target; | |
| @Retention(RetentionPolicy.RUNTIME) | |
| @Target(ElementType.FIELD) | |
| public @interface Node { | |
| String node(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment