Skip to content

Commit

Permalink
Allow private constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
fullwall committed Mar 30, 2020
1 parent 17b733e commit 321a53b
Showing 1 changed file with 9 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

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 @@ -338,29 +337,17 @@ private static Class<?> getGenericType(Field field) {
*/
@SuppressWarnings("unchecked")
public static <T> T load(Class<? extends T> clazz, DataKey root) {
T instance;
T instance = null;
try {
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;
} catch (IllegalAccessException e) {
for (Constructor<?> constructor : clazz.getDeclaredConstructors()) {
if (constructor.getParameterCount() == 0) {
constructor.setAccessible(true);
instance = (T) constructor.newInstance();
break;
}
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
if (instance == null)
return null;
Expand Down

0 comments on commit 321a53b

Please sign in to comment.