Skip to content

Commit

Permalink
Use constructor instead of special method in persistence loader
Browse files Browse the repository at this point in the history
  • Loading branch information
fullwall committed Mar 30, 2020
1 parent c021670 commit 17b733e
Showing 1 changed file with 22 additions and 2 deletions.
Expand Up @@ -2,6 +2,7 @@

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Collection;
Expand Down Expand Up @@ -193,7 +194,11 @@ private static void deserialise(PersistField field, DataKey root) throws Excepti
return;
}
if (clazz == Double.class && type != Double.class) {
return;
if (type == Float.class) {
value = ((Double) value).floatValue();
} else {
return;
}
}
if (clazz == Byte.class && type != Short.class && type != Byte.class && type != Integer.class
&& type != Double.class && type != Long.class && type != Float.class) {
Expand Down Expand Up @@ -331,10 +336,25 @@ private static Class<?> getGenericType(Field field) {
* The root key to load from
* @return The loaded instance
*/
@SuppressWarnings("unchecked")
public static <T> T load(Class<? extends T> clazz, DataKey root) {
T instance;
try {
instance = clazz.newInstance();
Constructor<T> constructor = (Constructor<T>) clazz.getConstructor();
constructor.setAccessible(true);
instance = constructor.newInstance();
} catch (IllegalArgumentException e) {
e.printStackTrace();
return null;
} catch (InvocationTargetException e) {
e.printStackTrace();
return null;
} catch (NoSuchMethodException e) {
e.printStackTrace();
return null;
} catch (SecurityException e) {
e.printStackTrace();
return null;
} catch (InstantiationException e) {
e.printStackTrace();
return null;
Expand Down

0 comments on commit 17b733e

Please sign in to comment.