Clean up reflection, add resource pack image and reload listener

This commit is contained in:
tterrag
2018-12-12 14:41:22 -05:00
parent 20fdff17ff
commit 12c5daa5f6
6 changed files with 87 additions and 43 deletions

View File

@@ -0,0 +1,36 @@
package com.tterrag.blur.util;
import java.lang.reflect.Field;
import java.util.Arrays;
public class ReflectionHelper {
@SuppressWarnings("unchecked")
public static <T> T getValue(Class<?> cls, Object instance, String...names) {
try {
return (T) getField(cls, names).get(instance);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
public static Field getField(Class<?> cls, String... names) {
for (String name : names) {
Field f = getFieldInternal(cls, name);
if (f != null) {
return f;
}
}
throw new IllegalArgumentException("Could not find any of fields " + Arrays.toString(names) + " on class " + cls);
}
private static Field getFieldInternal(Class<?> cls, String name) {
try {
Field f = cls.getDeclaredField(name);
f.setAccessible(true);
return f;
} catch (NoSuchFieldException | SecurityException e) {
return null;
}
}
}