Files
ModTweaker/src/main/java/modtweaker2/helpers/ReflectionHelper.java
T

160 lines
6.8 KiB
Java
Raw Normal View History

package modtweaker2.helpers;
2015-01-11 21:59:45 +02:00
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
public class ReflectionHelper {
2015-07-06 17:42:39 +02:00
2015-07-07 12:00:17 +02:00
/**
* Creates a new instance with the provided constructor and arguments.
* @param constructor The constructor of the class for which a new instance should be created.
* @param arguments Array of objects to be passed as arguments to the constructor call.
* @return A new object created by calling the constructor.
*/
2015-07-06 17:42:39 +02:00
public static <T> T getInstance(Constructor<T> constructor, Object... arguments) {
try {
return constructor.newInstance(arguments);
} catch (Exception e) { LogHelper.logError("Exception creating instance of " + constructor.getClass().getName(), e); }
return null;
}
2015-07-07 12:00:17 +02:00
/**
* Obtains the constructor for the named class identified by the parameter types.
* @param className The fully qualified name of the class.
* @param argumentTypes Parameter types to identify the constructor.
* @return Constructor object representing the declared constructor for the parameter types.
*/
public static Constructor<?> getConstructor(final String className, final Class<?>... argumentTypes) {
if(className == null || argumentTypes == null) {
LogHelper.logError("The provided class name or arguments can't be null.");
return null;
2015-07-06 17:42:39 +02:00
}
2015-01-11 21:59:45 +02:00
try {
2015-07-07 12:00:17 +02:00
Class<?> clazz = Class.forName(className);
Constructor<?> constructor = clazz.getDeclaredConstructor(argumentTypes);
2015-01-11 21:59:45 +02:00
constructor.setAccessible(true);
return constructor;
2015-07-08 13:56:27 +02:00
} catch (Exception ex) { LogHelper.logError("Exception getting constructor of " + className, ex); }
2015-01-11 21:59:45 +02:00
return null;
}
2015-07-06 17:42:39 +02:00
2015-07-07 12:00:17 +02:00
/**
* Obtains the constructor for the class identified by the parameter types.
* @param clazz The Class for which the constructor should be obtained.
* @param argumentTypes Parameter types to identify the constructor.
* @return Constructor object representing the declared constructor for the parameter types.
*/
public static <T> Constructor<T> getConstructor(Class<T> clazz, Class<?>... types) {
2015-07-06 17:42:39 +02:00
try {
Constructor<T> constructor = clazz.getDeclaredConstructor(types);
constructor.setAccessible(true);
return constructor;
2015-07-20 09:02:07 +02:00
} catch (Exception ex) { LogHelper.logError("Exception getting constructor of " + clazz.getName(), ex); }
2015-07-07 12:00:17 +02:00
2015-07-06 17:42:39 +02:00
return null;
}
2015-01-11 21:59:45 +02:00
2015-07-07 12:00:17 +02:00
/**
* Returns the value of a private field for an object instance.
* @param object An object instance from which the field value is to be extracted.
* @param fieldNames A list of field names for which the value should be extracted.
* The functions returns value of the first field found.
* @return The value of the provided field name.
*/
@SuppressWarnings("unchecked")
public static <T> T getObject(Object object, String... fieldNames) {
Class<?> cls = object.getClass();
for (String field : fieldNames) {
2015-01-11 21:59:45 +02:00
try {
Field result = cls.getDeclaredField(field);
result.setAccessible(true);
2015-07-07 12:00:17 +02:00
return (T) result.get(object);
2015-07-20 09:02:07 +02:00
} catch (Exception ex) { LogHelper.logError("Exception in getObject()", ex); }
2015-01-11 21:59:45 +02:00
}
2015-07-08 13:56:27 +02:00
LogHelper.logError("Could not retrieve any object for the provided field names.");
2015-01-11 21:59:45 +02:00
return null;
}
2015-07-07 12:00:17 +02:00
/**
* Returns the value of a private final field for an object instance and removes the final modifier.
* @param object An object instance from which the field value is to be extracted.
* @param fieldNames A list of field names for which the value should be extracted.
* The functions returns value of the first field found.
* @return The value of the provided field name.
*/
@SuppressWarnings("unchecked")
public static <T> T getFinalObject(Object object, String... fieldNames) {
Class<?> cls = object.getClass();
for (String field : fieldNames) {
2015-01-11 21:59:45 +02:00
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);
2015-07-07 12:00:17 +02:00
return (T) result.get(object);
2015-07-20 09:02:07 +02:00
} catch (Exception ex) { LogHelper.logError("Exception in getFinalObject()", ex); }
2015-01-11 21:59:45 +02:00
}
2015-07-07 12:00:17 +02:00
2015-07-08 13:56:27 +02:00
LogHelper.logError("Could not retrieve any final object for the provided field names.");
2015-01-11 21:59:45 +02:00
return null;
}
2015-07-07 12:00:17 +02:00
/**
* Returns the value of a private static field for a class.
* @param clazz The class for which the field value is to be extracted.
* @param fieldNames A list of field names for which the value should be extracted.
* @return The value of the provided field name.
*/
@SuppressWarnings("unchecked")
public static <T> T getStaticObject(Class<?> clazz, String... fieldNames) {
for (String field : fieldNames) {
2015-01-11 21:59:45 +02:00
try {
2015-07-07 12:00:17 +02:00
Field result = clazz.getDeclaredField(field);
2015-01-11 21:59:45 +02:00
result.setAccessible(true);
return (T) result.get(null);
2015-07-20 09:02:07 +02:00
} catch (Exception e) { LogHelper.logError("Exception in getStaticObject()", e); }
2015-01-11 21:59:45 +02:00
}
2015-07-08 13:56:27 +02:00
LogHelper.logError("Could not retrieve any static object for the provided field names.");
2015-01-11 21:59:45 +02:00
return null;
}
2015-07-07 12:00:17 +02:00
/**
* Returns the value of a private static field for a class.
* @param className The fully qualified name of the class.
* @param fieldNames A list of field names for which the value should be extracted.
* @return The value of the provided field name.
*/
public static <T> T getStaticObject(String className, String... fieldNames) {
try {
Class<?> clazz = Class.forName(className);
return getStaticObject(clazz, fieldNames);
2015-07-20 09:02:07 +02:00
} catch (ClassNotFoundException e) { LogHelper.logError("Exception in getStaticObject()", e); }
2015-07-07 12:00:17 +02:00
2015-07-08 13:56:27 +02:00
LogHelper.logError("Could not retrieve any static object for the provided field names.");
2015-07-07 12:00:17 +02:00
return null;
}
2015-01-11 21:59:45 +02:00
2015-07-07 12:00:17 +02:00
public static void setPrivateValue(Class<?> cls, String field, int var) {
2015-01-11 21:59:45 +02:00
try {
Field f = cls.getDeclaredField(field);
f.setAccessible(true);
f.setInt(null, var);
2015-07-20 09:02:07 +02:00
} catch (Exception e) { LogHelper.logError("Exception in setPrivateValue()", e); }
2015-01-11 21:59:45 +02:00
}
2015-07-07 12:00:17 +02:00
@SuppressWarnings({ "rawtypes", "unchecked" })
public static void setPrivateValue( Class cls, Object o, String field, Object var) {
2015-01-11 21:59:45 +02:00
cpw.mods.fml.relauncher.ReflectionHelper.setPrivateValue(cls, o, var, field);
}
}