mirror of
https://github.com/GTNewHorizons/ModTweaker.git
synced 2026-11-15 09:34:55 +00:00
Also changes like all packages inb4 "jared changes a package name in ModTweaker2 breaking compatability with everything" drama tweet.
This commit is contained in:
1 parent
4fd2551072
commit
ec562b62ea
187 files changed
+977
-991
No files matched your search
@@ -0,0 +1,71 @@
|
||||
package modtweaker2.helpers;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Modifier;
|
||||
|
||||
public class ReflectionHelper {
|
||||
public static Constructor getConstructor(String string, Class... types) {
|
||||
try {
|
||||
Class clazz = Class.forName(string);
|
||||
Constructor constructor = clazz.getDeclaredConstructor(types);
|
||||
constructor.setAccessible(true);
|
||||
return constructor;
|
||||
} catch (Exception ex) {}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static <T> T getObject(Object o, String... fieldName) {
|
||||
Class cls = o.getClass();
|
||||
for (String field : fieldName) {
|
||||
try {
|
||||
Field result = cls.getDeclaredField(field);
|
||||
result.setAccessible(true);
|
||||
return (T) result.get(o);
|
||||
} catch (Exception ex) {}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static <T> T getFinalObject(Object o, String... fieldName) {
|
||||
Class cls = o.getClass();
|
||||
for (String field : fieldName) {
|
||||
try {
|
||||
Field result = cls.getDeclaredField(field);
|
||||
result.setAccessible(true);
|
||||
Field modifiersField = Field.class.getDeclaredField("modifiers");
|
||||
modifiersField.setAccessible(true);
|
||||
modifiersField.setInt(result, result.getModifiers() & ~Modifier.FINAL);
|
||||
return (T) result.get(o);
|
||||
} catch (Exception ex) {}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static <T> T getStaticObject(Class cls, String... fieldName) {
|
||||
for (String field : fieldName) {
|
||||
try {
|
||||
Field result = cls.getDeclaredField(field);
|
||||
result.setAccessible(true);
|
||||
return (T) result.get(null);
|
||||
} catch (Exception e) {}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void setPrivateValue(Class cls, String field, int var) {
|
||||
try {
|
||||
Field f = cls.getDeclaredField(field);
|
||||
f.setAccessible(true);
|
||||
f.setInt(null, var);
|
||||
} catch (Exception e) {}
|
||||
}
|
||||
|
||||
public static void setPrivateValue(Class cls, Object o, String field, Object var) {
|
||||
cpw.mods.fml.relauncher.ReflectionHelper.setPrivateValue(cls, o, var, field);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user