Class ReflectionUtil

java.lang.Object
com.viaversion.viarewind.legacysupport.util.ReflectionUtil

public class ReflectionUtil extends Object
Common reflection utilities for Java 8 and newer.
  • Constructor Details

    • ReflectionUtil

      public ReflectionUtil()
  • Method Details

    • findMethod

      public static Method findMethod(Class<?> clazz, String[] methodNames, Class<?>... parameterTypes)
    • getMethod

      public static Method getMethod(Class<?> clazz, String methodName, Class<?>... parameterTypes)
      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 in
      methodName - The name of the method
      parameterTypes - The parameter types of the method
      Returns:
      The method if found, otherwise null
    • getFieldAndCache

      public static Field getFieldAndCache(Class<?> clazz, String fieldName)
    • getFieldAccessible

      public static Field getFieldAccessible(Class<?> clazz, String fieldName)
      Gets and field from the fieldCache or directly and makes it accessible.
      Parameters:
      clazz - The class
      fieldName - The field name
      Returns:
      The field
    • failSafeGetField

      public static Field failSafeGetField(Class<?> clazz, String fieldName)
      Get a field from a class, and print an error message if it fails.
      Parameters:
      clazz - The class
      fieldName - 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

      public static void removeFinal(Field field)
      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 class
      object - The object
      name - The field name
      value - The value
      Throws:
      IllegalAccessException - If the field cannot be accessed
    • failSafeSetValue

      public static void failSafeSetValue(Class<?> clazz, Object object, String name, Object value)
      Set a field value, and print an error message if it fails.
      Parameters:
      clazz - The class
      object - The object
      name - The field name
      value - The value
    • failSafeGetClass

      public static Class<?> failSafeGetClass(String name)