Class ReflectionUtil

java.lang.Object
de.tomatengames.util.ReflectionUtil

public class ReflectionUtil extends Object
Provides convenient methods to utilize the Reflection API.
Since:
1.5
  • Method Details

    • Class

      public static Class<?> Class(String name)
      Get a class by its name, wraps Class.forName(String).
      Parameters:
      name - the name of the class
      Returns:
      the class reference
      Throws:
      ReflectionException - if the class is not found
    • field

      public static Field field(Class<?> c, String name)
      Get a class field by its name, wraps Class.getDeclaredField(String).
      Parameters:
      c - the class
      name - the name of the field
      Returns:
      the field reference
      Throws:
      ReflectionException - if no such field is found
    • constructor

      public static <T> Constructor<T> constructor(Class<T> c, Class<?>... paramTypes)
      Get a constructor by its parameter types, wraps Class.getDeclaredConstructor(Class...).
      Type Parameters:
      T - the class type, made available to the output constructor reference without casting
      Parameters:
      c - the class
      paramTypes - the parameter types
      Returns:
      the constructor reference
      Throws:
      ReflectionException - if no such constructor is found
    • method

      public static Method method(Class<?> c, String name, Class<?>... paramTypes)
      Get a method by its name and parameter types, wraps Class.getDeclaredMethod(String, Class...).
      Parameters:
      c - the class
      name - the name of the method
      paramTypes - the parameter types
      Returns:
      the method reference
      Throws:
      ReflectionException - if no such method is found
    • searchField

      public static Field searchField(Class<?> c, String name)
      Searches the given class hierarchy for any field of the given name.
      Parameters:
      c - the most specific class searched
      name - the name of the field
      Returns:
      the first found field reference or null if no such field is found in the class or any superclass
    • findField

      public static Field findField(Class<?> c, String name)
      searchField(Class, String) but throws an exception when no field is found.
      Parameters:
      c - the most specific class searched
      name - the name of the field
      Returns:
      the first found field reference
      Throws:
      ReflectionException - if no such field is found
    • searchConstructor

      public static <T> Constructor<T> searchConstructor(Class<T> c, Class<?>... compatibleParamTypes)
      Searches the given class for a constructor that the given parameter types can be passed to.
      Type Parameters:
      T - the class type, made available to the output constructor reference without casting
      Parameters:
      c - the class
      compatibleParamTypes - the passable parameter types
      Returns:
      a constructor reference matching the given parameter types or null if no such constructor is found
    • findConstructor

      public static <T> Constructor<T> findConstructor(Class<T> c, Class<?>... compatibleParamTypes)
      searchConstructor(Class, Class...) but throws an exception if no constructor is found.
      Type Parameters:
      T - the class type
      Parameters:
      c - the class
      compatibleParamTypes - the passable parameter types
      Returns:
      a constructor reference matching the given parameter types
      Throws:
      ReflectionException - if no such constructor is found
    • searchMethod

      public static Method searchMethod(Class<?> c, String name, Class<?>... compatibleParamTypes)
      Searches the given class hierarchy for a method of the given name that the given parameter types can be passed to.
      Parameters:
      c - the most specific class
      name - the name of the method
      compatibleParamTypes - the passable parameter types
      Returns:
      the first found method reference matching the given name and parameter types or null if no such method is found
    • findMethod

      public static Method findMethod(Class<?> c, String name, Class<?>... compatibleParamTypes)
      searchMethod(Class, String, Class...) but throws an exception if no method is found.
      Parameters:
      c - the most specific class
      name - the name of the method
      compatibleParamTypes - the passable parameter types
      Returns:
      the first found method reference matching the given name and parameter types
      Throws:
      ReflectionException - if no such method is found
    • construct

      public static <T> T construct(Constructor<T> constr, Object... param)
      Constructs a new object using the given constructor and constructor parameters, wraps Constructor.newInstance(Object...).
      Type Parameters:
      T - the type of the returned object
      Parameters:
      constr - the constructor reference
      param - the constructor parameters
      Returns:
      the new object
      Throws:
      ReflectionException - if a ReflectiveOperationException occurs, which may be an IllegalAccessException, an InstantiationException or an InvocationTargetException.
    • construct

      public static <T> T construct(Constructor<T> constr)
      Constructs a new object using the given constructor and no parameters.
      Type Parameters:
      T - the type of the returned object
      Parameters:
      constr - the constructor reference
      Returns:
      the new object
      Throws:
      ReflectionException - if a ReflectiveOperationException occurs
      See Also:
    • run

      public static Object run(Object o, Method m, Object... param)
      Runs the given method of the given object using the given parameters.
      Parameters:
      o - the target object
      m - the method reference
      param - the method parameters
      Returns:
      the return value
      Throws:
      ReflectionException - if a ReflectiveOperationException occurs, which may be an IllegalAccessException or an InvocationTargetException.
    • run

      public static Object run(Object o, Method m)
      Runs the given method of the given object with no parameters.
      Parameters:
      o - the target object
      m - the method reference
      Returns:
      the return value
      Throws:
      ReflectionException - if a ReflectiveOperationException occurs
      See Also:
    • runStatic

      public static Object runStatic(Method m, Object... param)
      Runs the given static method using the given parameters.
      Parameters:
      m - the method reference
      param - the parameters
      Returns:
      the return value
      Throws:
      ReflectionException - if a ReflectiveOperationException occurs
      See Also:
    • runStatic

      public static Object runStatic(Method m)
      Runs the given static method using no parameters.
      Parameters:
      m - the method reference
      Returns:
      the return value
      Throws:
      ReflectionException - if a ReflectiveOperationException occurs
      See Also:
    • get

      public static <T> T get(Object o, Class<?> fieldClass, String fieldName)
      Get the current value of the field of the given class and name in the given object. For efficiency, use get(Object, Field) instead.
      Type Parameters:
      T - the returned type, automatically casted
      Parameters:
      o - the target object
      fieldClass - the field class
      fieldName - the field name
      Returns:
      the value of the found field
      Throws:
      ReflectionException - if no such field is found or an IllegalAccessException occurs
    • set

      public static void set(Object o, Class<?> fieldClass, String fieldName, Object value)
      Set the current value of the field of the given class and name in the given object. For efficiency, use set(Object, Field, Object) instead.
      Parameters:
      o - the target object
      fieldClass - the field class
      fieldName - the field name
      value - the value for the found field
      Throws:
      ReflectionException - if no such field is found or an IllegalAccessException occurs
    • get

      public static <T> T get(Object o, String fieldName)
      Get the current value of the field of the given name in the given object. For efficiency, use get(Object, Field) instead.
      Type Parameters:
      T - the returned type, automatically casted
      Parameters:
      o - the target object
      fieldName - the name of the field
      Returns:
      the value of the found field
      Throws:
      ReflectionException - if no such field is found or an IllegalAccessException occurs
    • set

      public static void set(Object o, String fieldName, Object value)
      Set the current value of the field of the given name in the given object. For efficiency, use set(Object, Field, Object) instead.
      Parameters:
      o - the target object
      fieldName - the name of the field
      value - the value for the found field
      Throws:
      ReflectionException - if no such field is found or an IllegalAccessException occurs
    • getStatic

      public static <T> T getStatic(Class<?> c, String fieldName)
      Get the current value of the static field of the given class and name. For efficiency, use getStatic(Field) instead.
      Type Parameters:
      T - the returned type, automatically casted
      Parameters:
      c - the field class
      fieldName - the field name
      Returns:
      the value of the found field
      Throws:
      ReflectionException - if no such field is found or an IllegalAccessException occurs
    • setStatic

      public static void setStatic(Class<?> c, String fieldName, Object value)
      Set the current value of the static field of the given class and name. For efficiency, use setStatic(Field, Object) instead.
      Parameters:
      c - the field class
      fieldName - the field name
      value - the value for the found field
      Throws:
      ReflectionException - if no such field is found or an IllegalAccessException occurs
    • getStatic

      public static <T> T getStatic(String className, String fieldName)
      Get the current value of the static field of the given class name and field name. For efficiency, use getStatic(Field) instead.
      Type Parameters:
      T - the returned type, automatically casted
      Parameters:
      className - the field class name
      fieldName - the field name
      Returns:
      the value of the found field
      Throws:
      ReflectionException - if no such class or field is found or an IllegalAccessException occurs
    • setStatic

      public static void setStatic(String className, String fieldName, Object value)
      Set the current value of the static field of the given class name and field name. For efficiency, use setStatic(Field, Object) instead.
      Parameters:
      className - the field class name
      fieldName - the field name
      value - the value for the found field
      Throws:
      ReflectionException - if no such class or field is found or an IllegalAccessException occurs
    • construct

      public static <T> T construct(Class<?> c, Object... param)
      Constructs a new object of the given class using a matching constructor. For efficiency, use construct(Constructor, Object...) instead.
      Type Parameters:
      T - the returned type, automatically casted
      Parameters:
      c - the class to be instantiated
      param - the constructor parameters
      Returns:
      the new object
      Throws:
      ReflectionException - if no matching constructor is found or a ReflectiveOperationException occurs
      See Also:
    • construct

      public static <T> T construct(String className, Object... param)
      Constructs a new object of the given class name using a matching constructor. For efficiency, use construct(Constructor, Object...) instead.
      Type Parameters:
      T - the returned type, automatically casted
      Parameters:
      className - the name of the class to be instantiated
      param - the constructor parameters
      Returns:
      the new object
      Throws:
      ReflectionException - if no such class or no matching constructor is found or an ReflectiveOperationException occurs
      See Also:
    • run

      public static <T> T run(Object o, String methodName, Object... param)
      Runs a matching method of the given object and name. For efficiency, use run(Object, Method, Object...) instead.
      Type Parameters:
      T - the returned type, automatically casted
      Parameters:
      o - the target object
      methodName - the name of the method
      param - the method parameters
      Returns:
      the return value
      Throws:
      ReflectionException - if no matching method is found or a ReflectiveOperationException occurs
      See Also:
    • runStatic

      public static <T> T runStatic(Class<?> c, String methodName, Object... param)
      Runs a matching method of the given class and name. For efficiency, use runStatic(Method, Object...) instead.
      Type Parameters:
      T - the returned type, automatically casted
      Parameters:
      c - the method class
      methodName - the method name
      param - the method parameters
      Returns:
      the return value
      Throws:
      ReflectionException - if no matching method is found or a ReflectiveOperationException occurs
      See Also:
    • runStatic

      public static <T> T runStatic(String className, String methodName, Object... param)
      Runs a matching method of the given class name and method name. For efficiency, use runStatic(Method, Object...) instead.
      Type Parameters:
      T - the returned type, automatically casted
      Parameters:
      className - the method class
      methodName - the method name
      param - the method parameters
      Returns:
      the return value
      Throws:
      ReflectionException - if no such class or matching method is found or a ReflectiveOperationException occurs
      See Also:
    • get

      public static Object get(Object object, Field field)
      Get the current value of the given field in the given object as an Object, wraps Field.get(Object).
      Parameters:
      object - the object
      field - the field
      Returns:
      the value as an Object
      Throws:
      ReflectionException - if an IllegalAccessException occurs
    • set

      public static void set(Object object, Field field, Object value)
      Set the current value of the given field in the given object to an Object, wraps Field.set(Object, Object).
      Parameters:
      object - the object
      field - the field
      value - the value as an Object
      Throws:
      ReflectionException - if an IllegalAccessException occurs
    • getStatic

      public static Object getStatic(Field field)
      Get the current value of the given static field as an Object.
      Parameters:
      field - the field
      Returns:
      the value as an Object
      Throws:
      ReflectionException - if an IllegalAccessException occurs or the given field is not static
    • setStatic

      public static void setStatic(Field field, Object value)
      Set the current value of the given static field to an Object.
      Parameters:
      field - the field
      value - the value as an Object
      Throws:
      ReflectionException - if an IllegalAccessException occurs or the given field is not static
    • getBoolean

      public static boolean getBoolean(Object object, Field field)
      Get the current value of the given field in the given object as a boolean, wraps Field.getBoolean(Object).
      Parameters:
      object - the object
      field - the field
      Returns:
      the value as a boolean
      Throws:
      ReflectionException - if an IllegalAccessException occurs
    • setBoolean

      public static void setBoolean(Object object, Field field, boolean value)
      Set the current value of the given field in the given object to a boolean, wraps Field.setBoolean(Object, boolean).
      Parameters:
      object - the object
      field - the field
      value - the value as a boolean
      Throws:
      ReflectionException - if an IllegalAccessException occurs
    • getStaticBoolean

      public static boolean getStaticBoolean(Field field)
      Get the current value of the given static field as a boolean.
      Parameters:
      field - the field
      Returns:
      the value as a boolean
      Throws:
      ReflectionException - if an IllegalAccessException occurs or the given field is not static
    • setStaticBoolean

      public static void setStaticBoolean(Field field, boolean value)
      Set the current value of the given static field to a boolean.
      Parameters:
      field - the field
      value - the value as a boolean
      Throws:
      ReflectionException - if an IllegalAccessException occurs or the given field is not static
    • getChar

      public static char getChar(Object object, Field field)
      Get the current value of the given field in the given object as a char, wraps Field.getChar(Object).
      Parameters:
      object - the object
      field - the field
      Returns:
      the value as a char
      Throws:
      ReflectionException - if an IllegalAccessException occurs
    • setChar

      public static void setChar(Object object, Field field, char value)
      Set the current value of the given field in the given object to a char, wraps Field.setChar(Object, char).
      Parameters:
      object - the object
      field - the field
      value - the value as a char
      Throws:
      ReflectionException - if an IllegalAccessException occurs
    • getStaticChar

      public static char getStaticChar(Field field)
      Get the current value of the given static field as a char.
      Parameters:
      field - the field
      Returns:
      the value as a char
      Throws:
      ReflectionException - if an IllegalAccessException occurs or the given field is not static
    • setStaticChar

      public static void setStaticChar(Field field, char value)
      Set the current value of the given static field to a char.
      Parameters:
      field - the field
      value - the value as a char
      Throws:
      ReflectionException - if an IllegalAccessException occurs or the given field is not static
    • getByte

      public static byte getByte(Object object, Field field)
      Get the current value of the given field in the given object as a byte, wraps Field.getByte(Object).
      Parameters:
      object - the object
      field - the field
      Returns:
      the value as a byte
      Throws:
      ReflectionException - if an IllegalAccessException occurs
    • setByte

      public static void setByte(Object object, Field field, byte value)
      Set the current value of the given field in the given object to a byte, wraps Field.setByte(Object, byte).
      Parameters:
      object - the object
      field - the field
      value - the value as a byte
      Throws:
      ReflectionException - if an IllegalAccessException occurs
    • getStaticByte

      public static byte getStaticByte(Field field)
      Get the current value of the given static field as a byte.
      Parameters:
      field - the field
      Returns:
      the value as a byte
      Throws:
      ReflectionException - if an IllegalAccessException occurs or the given field is not static
    • setStaticByte

      public static void setStaticByte(Field field, byte value)
      Set the current value of the given static field to a byte.
      Parameters:
      field - the field
      value - the value as a byte
      Throws:
      ReflectionException - if an IllegalAccessException occurs or the given field is not static
    • getShort

      public static short getShort(Object object, Field field)
      Get the current value of the given field in the given object as a short, wraps Field.getShort(Object).
      Parameters:
      object - the object
      field - the field
      Returns:
      the value as a short
      Throws:
      ReflectionException - if an IllegalAccessException occurs
    • setShort

      public static void setShort(Object object, Field field, short value)
      Set the current value of the given field in the given object to a short, wraps Field.setShort(Object, short).
      Parameters:
      object - the object
      field - the field
      value - the value as a short
      Throws:
      ReflectionException - if an IllegalAccessException occurs
    • getStaticShort

      public static short getStaticShort(Field field)
      Get the current value of the given static field as a short.
      Parameters:
      field - the field
      Returns:
      the value as a short
      Throws:
      ReflectionException - if an IllegalAccessException occurs or the given field is not static
    • setStaticShort

      public static void setStaticShort(Field field, short value)
      Set the current value of the given static field to a short.
      Parameters:
      field - the field
      value - the value as a short
      Throws:
      ReflectionException - if an IllegalAccessException occurs or the given field is not static
    • getInt

      public static int getInt(Object object, Field field)
      Get the current value of the given field in the given object as an int, wraps Field.getInt(Object).
      Parameters:
      object - the object
      field - the field
      Returns:
      the value as an int
      Throws:
      ReflectionException - if an IllegalAccessException occurs
    • setInt

      public static void setInt(Object object, Field field, int value)
      Set the current value of the given field in the given object to an int, wraps Field.setInt(Object, int).
      Parameters:
      object - the object
      field - the field
      value - the value as an int
      Throws:
      ReflectionException - if an IllegalAccessException occurs
    • getStaticInt

      public static int getStaticInt(Field field)
      Get the current value of the given static field as an int.
      Parameters:
      field - the field
      Returns:
      the value as an int
      Throws:
      ReflectionException - if an IllegalAccessException occurs or the given field is not static
    • setStaticInt

      public static void setStaticInt(Field field, int value)
      Set the current value of the given static field to an int.
      Parameters:
      field - the field
      value - the value as an int
      Throws:
      ReflectionException - if an IllegalAccessException occurs or the given field is not static
    • getLong

      public static long getLong(Object object, Field field)
      Get the current value of the given field in the given object as a long, wraps Field.getLong(Object).
      Parameters:
      object - the object
      field - the field
      Returns:
      the value as a long
      Throws:
      ReflectionException - if an IllegalAccessException occurs
    • setLong

      public static void setLong(Object object, Field field, long value)
      Set the current value of the given field in the given object to a long, wraps Field.setLong(Object, long).
      Parameters:
      object - the object
      field - the field
      value - the value as a long
      Throws:
      ReflectionException - if an IllegalAccessException occurs
    • getStaticLong

      public static long getStaticLong(Field field)
      Get the current value of the given static field as a long.
      Parameters:
      field - the field
      Returns:
      the value as a long
      Throws:
      ReflectionException - if an IllegalAccessException occurs or the given field is not static
    • setStaticLong

      public static void setStaticLong(Field field, long value)
      Set the current value of the given static field to a long.
      Parameters:
      field - the field
      value - the value as a long
      Throws:
      ReflectionException - if an IllegalAccessException occurs or the given field is not static
    • getFloat

      public static float getFloat(Object object, Field field)
      Get the current value of the given field in the given object as a float, wraps Field.getFloat(Object).
      Parameters:
      object - the object
      field - the field
      Returns:
      the value as a float
      Throws:
      ReflectionException - if an IllegalAccessException occurs
    • setFloat

      public static void setFloat(Object object, Field field, float value)
      Set the current value of the given field in the given object to a float, wraps Field.setFloat(Object, float).
      Parameters:
      object - the object
      field - the field
      value - the value as a float
      Throws:
      ReflectionException - if an IllegalAccessException occurs
    • getStaticFloat

      public static float getStaticFloat(Field field)
      Get the current value of the given static field as a float.
      Parameters:
      field - the field
      Returns:
      the value as a float
      Throws:
      ReflectionException - if an IllegalAccessException occurs or the given field is not static
    • setStaticFloat

      public static void setStaticFloat(Field field, float value)
      Set the current value of the given static field to a float.
      Parameters:
      field - the field
      value - the value as a float
      Throws:
      ReflectionException - if an IllegalAccessException occurs or the given field is not static
    • getDouble

      public static double getDouble(Object object, Field field)
      Get the current value of the given field in the given object as a double, wraps Field.getDouble(Object).
      Parameters:
      object - the object
      field - the field
      Returns:
      the value as a double
      Throws:
      ReflectionException - if an IllegalAccessException occurs
    • setDouble

      public static void setDouble(Object object, Field field, double value)
      Set the current value of the given field in the given object to a double, wraps Field.setDouble(Object, double).
      Parameters:
      object - the object
      field - the field
      value - the value as a double
      Throws:
      ReflectionException - if an IllegalAccessException occurs
    • getStaticDouble

      public static double getStaticDouble(Field field)
      Get the current value of the given static field as a double.
      Parameters:
      field - the field
      Returns:
      the value as a double
      Throws:
      ReflectionException - if an IllegalAccessException occurs or the given field is not static
    • setStaticDouble

      public static void setStaticDouble(Field field, double value)
      Set the current value of the given static field to a double.
      Parameters:
      field - the field
      value - the value as a double
      Throws:
      ReflectionException - if an IllegalAccessException occurs or the given field is not static