1 package org.bukkit.configuration.serialization;
3 import java.lang.reflect.Constructor;
4 import java.lang.reflect.InvocationTargetException;
5 import java.lang.reflect.Method;
6 import java.lang.reflect.Modifier;
7 import java.util.HashMap;
9 import java.util.logging.Level;
10 import java.util.logging.Logger;
12 import org.apache.commons.lang.Validate;
13 import org.bukkit.Color;
14 import org.bukkit.FireworkEffect;
15 import org.bukkit.configuration.Configuration;
16 import org.bukkit.inventory.ItemStack;
17 import org.bukkit.potion.PotionEffect;
18 import org.bukkit.util.BlockVector;
19 import org.bukkit.util.Vector;
26 private final Class<? extends ConfigurationSerializable> clazz;
27 private static Map<String, Class<? extends ConfigurationSerializable>> aliases =
new HashMap<String, Class<? extends ConfigurationSerializable>>();
42 protected Method
getMethod(String name,
boolean isStatic) {
44 Method method = clazz.getDeclaredMethod(name, Map.class);
49 if (Modifier.isStatic(method.getModifiers()) != isStatic) {
54 }
catch (NoSuchMethodException ex) {
56 }
catch (SecurityException ex) {
61 protected Constructor<? extends ConfigurationSerializable>
getConstructor() {
63 return clazz.getConstructor(Map.class);
64 }
catch (NoSuchMethodException ex) {
66 }
catch (SecurityException ex) {
76 Logger.getLogger(
ConfigurationSerialization.class.getName()).log(Level.SEVERE,
"Could not call method '" + method.toString() +
"' of " + clazz +
" for deserialization: method returned null");
80 }
catch (Throwable ex) {
83 "Could not call method '" + method.toString() +
"' of " + clazz +
" for deserialization",
84 ex instanceof InvocationTargetException ? ex.getCause() : ex);
92 return ctor.newInstance(args);
93 }
catch (Throwable ex) {
96 "Could not call constructor '" + ctor.toString() +
"' of " + clazz +
" for deserialization",
97 ex instanceof InvocationTargetException ? ex.getCause() : ex);
104 Validate.notNull(args,
"Args must not be null");
107 Method method = null;
109 if (result == null) {
112 if (method != null) {
117 if (result == null) {
120 if (method != null) {
125 if (result == null) {
126 Constructor<? extends ConfigurationSerializable> constructor =
getConstructor();
128 if (constructor != null) {
166 Class<? extends ConfigurationSerializable> clazz = null;
173 throw new IllegalArgumentException(
"Cannot have null alias");
177 throw new IllegalArgumentException(
"Specified class does not exist ('" + alias +
"')");
179 }
catch (ClassCastException ex) {
180 ex.fillInStackTrace();
184 throw new IllegalArgumentException(
"Args doesn't contain type key ('" +
SERIALIZED_TYPE_KEY +
"')");
195 public static void registerClass(Class<? extends ConfigurationSerializable> clazz) {
198 if (delegate == null) {
211 public static void registerClass(Class<? extends ConfigurationSerializable> clazz, String alias) {
212 aliases.put(alias, clazz);
221 aliases.remove(alias);
229 public static void unregisterClass(Class<? extends ConfigurationSerializable> clazz) {
230 while (aliases.values().remove(clazz)) {
241 public static Class<? extends ConfigurationSerializable>
getClassByAlias(String alias) {
242 return aliases.get(alias);
251 public static String
getAlias(Class<? extends ConfigurationSerializable> clazz) {
254 if (delegate != null) {
255 if ((delegate.
value() == null) || (delegate.
value() == clazz)) {
262 if (delegate == null) {
265 if ((alias != null) && (alias.
value() != null)) {
266 return alias.
value();
270 return clazz.getName();