Class ReflectionUtil
java.lang.Object
com.viaversion.viarewind.legacysupport.util.ReflectionUtil
Common reflection utilities for Java 8 and newer.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic Class<?>failSafeGetClass(String name) static FieldfailSafeGetField(Class<?> clazz, String fieldName) Get a field from a class, and print an error message if it fails.static voidfailSafeSetValue(Class<?> clazz, Object object, String name, Object value) Set a field value, and print an error message if it fails.static MethodfindMethod(Class<?> clazz, String[] methodNames, Class<?>... parameterTypes) static FieldgetFieldAccessible(Class<?> clazz, String fieldName) Gets and field from thefieldCacheor directly and makes it accessible.static FieldgetFieldAndCache(Class<?> clazz, String fieldName) static MethodRecursively search for a method in the class and its superclasses.static voidremoveFinal(Field field) Remove the final modifier from a field.static voidUnsafe wrapper for value setting which bypasses final checks.static void
-
Constructor Details
-
ReflectionUtil
public ReflectionUtil()
-
-
Method Details
-
findMethod
-
getMethod
Recursively search for a method in the class and its superclasses. Returns null if the method is not found.- Parameters:
clazz- The class to search inmethodName- The name of the methodparameterTypes- The parameter types of the method- Returns:
- The method if found, otherwise null
-
getFieldAndCache
-
getFieldAccessible
Gets and field from thefieldCacheor directly and makes it accessible.- Parameters:
clazz- The classfieldName- The field name- Returns:
- The field
-
failSafeGetField
Get a field from a class, and print an error message if it fails.- Parameters:
clazz- The classfieldName- The field name- Returns:
- The field, or null if it fails
-
setValue
public static void setValue(Class<?> clazz, Object object, String name, Object value, boolean isFinal) throws IllegalAccessException - Throws:
IllegalAccessException
-
removeFinal
Remove the final modifier from a field.- Parameters:
field- The field
-
setValue
public static void setValue(Class<?> clazz, Object object, String name, Object value) throws IllegalAccessException Unsafe wrapper for value setting which bypasses final checks.- Parameters:
clazz- The classobject- The objectname- The field namevalue- The value- Throws:
IllegalAccessException- If the field cannot be accessed
-
failSafeSetValue
Set a field value, and print an error message if it fails.- Parameters:
clazz- The classobject- The objectname- The field namevalue- The value
-
failSafeGetClass
-