Generic cleanup of warnings, whitespace and style.

By: Erik Broes <erikbroes@grum.nl>
This commit is contained in:
Bukkit/Spigot
2011-12-25 16:02:30 +01:00
parent 98960fd73e
commit aaab1cba23
257 changed files with 1408 additions and 1152 deletions

View File

@@ -7,9 +7,11 @@ import java.util.Map;
* <p>
* These objects MUST implement one of the following, in addition to the methods
* as defined by this interface:
* - A static method "deserialize" that accepts a single {@link Map<String, Object>} and returns the class.
* - A static method "valueOf" that accepts a single {@link Map<String, Object>} and returns the class.
* - A constructor that accepts a single {@link Map<String, Object>}.
* <ul>
* <li>A static method "deserialize" that accepts a single {@link Map<String, Object>} and returns the class.</li>
* <li>A static method "valueOf" that accepts a single {@link Map<String, Object>} and returns the class.</li>
* <li>A constructor that accepts a single {@link Map<String, Object>}.</li>
* </ul>
*/
public interface ConfigurationSerializable {
/**
@@ -17,7 +19,7 @@ public interface ConfigurationSerializable {
* <p>
* This class must provide a method to restore this class, as defined in the
* {@link ConfigurationSerializable} interface javadocs.
*
*
* @return Map containing the current state of this class
*/
public Map<String, Object> serialize();

View File

@@ -17,27 +17,27 @@ public class ConfigurationSerialization {
public static final String SERIALIZED_TYPE_KEY = "==";
private final Class<? extends ConfigurationSerializable> clazz;
private static Map<String, Class<? extends ConfigurationSerializable>> aliases = new HashMap<String, Class<? extends ConfigurationSerializable>>();
static {
registerClass(Vector.class);
registerClass(BlockVector.class);
}
protected ConfigurationSerialization(Class<? extends ConfigurationSerializable> clazz) {
this.clazz = clazz;
}
protected Method getMethod(String name, boolean isStatic) {
try {
Method method = clazz.getDeclaredMethod(name, Map.class);
if (!ConfigurationSerializable.class.isAssignableFrom(method.getReturnType())) {
return null;
}
if (Modifier.isStatic(method.getModifiers()) != isStatic) {
return null;
}
return method;
} catch (NoSuchMethodException ex) {
return null;
@@ -45,7 +45,7 @@ public class ConfigurationSerialization {
return null;
}
}
protected Constructor<? extends ConfigurationSerializable> getConstructor() {
try {
return clazz.getConstructor(Map.class);
@@ -55,11 +55,11 @@ public class ConfigurationSerialization {
return null;
}
}
protected ConfigurationSerializable deserializeViaMethod(Method method, Map<String, Object> args) {
try {
ConfigurationSerializable result = (ConfigurationSerializable)method.invoke(null, args);
ConfigurationSerializable result = (ConfigurationSerializable) method.invoke(null, args);
if (result == null) {
Logger.getLogger(ConfigurationSerialization.class.getName()).log(Level.SEVERE, "Could not call method '" + method.toString() + "' of " + clazz + " for deserialization: method returned null");
} else {
@@ -68,55 +68,55 @@ public class ConfigurationSerialization {
} catch (Throwable ex) {
Logger.getLogger(ConfigurationSerialization.class.getName()).log(Level.SEVERE, "Could not call method '" + method.toString() + "' of " + clazz + " for deserialization", ex);
}
return null;
}
protected ConfigurationSerializable deserializeViaCtor(Constructor<? extends ConfigurationSerializable> ctor, Map<String, Object> args) {
try {
return ctor.newInstance(args);
} catch (Throwable ex) {
Logger.getLogger(ConfigurationSerialization.class.getName()).log(Level.SEVERE, "Could not call constructor '" + ctor.toString() + "' of " + clazz + " for deserialization", ex);
}
return null;
}
public ConfigurationSerializable deserialize(Map<String, Object> args) {
if (args == null) {
throw new IllegalArgumentException("Args must not be null");
}
ConfigurationSerializable result = null;
Method method = null;
if (result == null) {
method = getMethod("deserialize", true);
if (method != null) {
result = deserializeViaMethod(method, args);
}
}
if (result == null) {
method = getMethod("valueOf", true);
if (method != null) {
result = deserializeViaMethod(method, args);
}
}
if (result == null) {
Constructor<? extends ConfigurationSerializable> constructor = getConstructor();
if (constructor != null) {
result = deserializeViaCtor(constructor, args);
}
}
return result;
}
/**
* Attempts to deserialize the given arguments into a new instance of the given class.
* <p>
@@ -125,7 +125,7 @@ public class ConfigurationSerialization {
* <p>
* If a new instance could not be made, an example being the class not fully implementing
* the interface, null will be returned.
*
*
* @param args Arguments for deserialization
* @param clazz Class to deserialize into
* @return New instance of the specified class
@@ -133,7 +133,7 @@ public class ConfigurationSerialization {
public static ConfigurationSerializable deserializeObject(Map<String, Object> args, Class<? extends ConfigurationSerializable> clazz) {
return new ConfigurationSerialization(clazz).deserialize(args);
}
/**
* Attempts to deserialize the given arguments into a new instance of the given class.
* <p>
@@ -142,17 +142,17 @@ public class ConfigurationSerialization {
* <p>
* If a new instance could not be made, an example being the class not fully implementing
* the interface, null will be returned.
*
*
* @param args Arguments for deserialization
* @return New instance of the specified class
*/
public static ConfigurationSerializable deserializeObject(Map<String, Object> args) {
Class<? extends ConfigurationSerializable> clazz = null;
if (args.containsKey(SERIALIZED_TYPE_KEY)) {
try {
String alias = (String)args.get(SERIALIZED_TYPE_KEY);
String alias = (String) args.get(SERIALIZED_TYPE_KEY);
if (alias == null) {
throw new IllegalArgumentException("Specified class does not exist ('" + alias + ")'");
} else {
@@ -165,71 +165,73 @@ public class ConfigurationSerialization {
} else {
throw new IllegalArgumentException("Args doesn't contain type key ('" + SERIALIZED_TYPE_KEY + "')");
}
return new ConfigurationSerialization(clazz).deserialize(args);
}
/**
* Registers the given {@link ConfigurationSerializable} class by its alias
*
*
* @param clazz Class to register
*/
public static void registerClass(Class<? extends ConfigurationSerializable> clazz) {
DelegateDeserialization delegate = clazz.getAnnotation(DelegateDeserialization.class);
if (delegate == null ) {
if (delegate == null) {
registerClass(clazz, getAlias(clazz));
registerClass(clazz, clazz.getName());
}
}
/**
* Registers the given alias to the specified {@link ConfigurationSerializable} class
*
*
* @param clazz Class to register
* @param alias Alias to register as
*/
public static void registerClass(Class<? extends ConfigurationSerializable> clazz, String alias) {
aliases.put(alias, clazz);
}
/**
* Unregisters the specified alias to a {@link ConfigurationSerializable}
*
*
* @param alias Alias to unregister
*/
public static void unregisterClass(String alias) {
aliases.remove(alias);
}
/**
* Unregisters any aliases for the specified {@link ConfigurationSerializable} class
*
*
* @param clazz Class to unregister
*/
public static void unregisterClass(Class<? extends ConfigurationSerializable> clazz) {
while (aliases.values().remove(clazz));
while (aliases.values().remove(clazz)) {
;
}
}
/**
* Attempts to get a registered {@link ConfigurationSerializable} class by its alias
*
*
* @param alias Alias of the serializable
* @return Registered class, or null if not found
*/
public static Class<? extends ConfigurationSerializable> getClassByAlias(String alias) {
return aliases.get(alias);
}
/**
* Gets the correct alias for the given {@link ConfigurationSerializable} class
*
*
* @param clazz Class to get alias for
* @return Alias to use for the class
*/
public static String getAlias(Class<? extends ConfigurationSerializable> clazz) {
DelegateDeserialization delegate = clazz.getAnnotation(DelegateDeserialization.class);
if (delegate != null) {
if ((delegate.value() == null) || (delegate.value() == clazz)) {
delegate = null;
@@ -237,7 +239,7 @@ public class ConfigurationSerialization {
return getAlias(delegate.value());
}
}
if (delegate == null) {
SerializableAs alias = clazz.getAnnotation(SerializableAs.class);
@@ -245,7 +247,7 @@ public class ConfigurationSerialization {
return alias.value();
}
}
return clazz.getName();
}
}

View File

@@ -14,7 +14,7 @@ import java.lang.annotation.Target;
public @interface DelegateDeserialization {
/**
* Which class should be used as a delegate for this classes deserialization
*
*
* @return Delegate class
*/
public Class<? extends ConfigurationSerializable> value();

View File

@@ -21,7 +21,7 @@ public @interface SerializableAs {
* <p>
* This name MUST be unique. We recommend using names such as "MyPluginThing" instead of
* "Thing".
*
*
* @return Name to serialize the class as.
*/
public String value();