Class ReflectionUtil

java.lang.Object
nl.pim16aap2.testing.reflection.ReflectionUtil

public final class ReflectionUtil extends Object
Utility class for reflection.
  • Method Details

    • setField

      public static void setField(Object obj, String fieldName, @Nullable Object value)
      Sets the value of a field in an object.
      Parameters:
      obj - The object to set the field in.
      fieldName - The name of the field to set.
      value - The value to set the field to.
      Throws:
      RuntimeException - If the field could not be found or accessed.
    • setField

      public static void setField(Class<?> source, Object obj, String fieldName, @Nullable Object value)
      Sets the value of a field in an object.
      Parameters:
      source - The class to get the field from.
      obj - The object to set the field in. This should be an instance of source or a subclass of it.
      fieldName - The name of the field to set.
      value - The value to set the field to.
      Throws:
      RuntimeException - If the field could not be found or accessed.
    • getField

      public static Field getField(Class<?> clazz, String fieldName)
      Gets a named field from a class.
      Parameters:
      clazz - The class to get the field from.
      fieldName - The name of the field to get.
      Returns:
      The field. The field is set to accessible.
    • getField

      public static Field getField(Object obj, String fieldName)
      Gets a named field from an object.
      Parameters:
      obj - The object to get the field from.
      fieldName - The name of the field to get.
      Returns:
      The field. The field is set to accessible.
    • getMethod

      public static Method getMethod(Class<?> clazz, String methodName)
      Gets a named method from a class.
      Parameters:
      clazz - The class to get the method from.
      methodName - The name of the method to get.
      Returns:
      The method. The method is set to accessible.
    • getMethod

      public static Method getMethod(Object obj, String methodName)
      Gets a named method from an object.
      Parameters:
      obj - The object to get the method from.
      methodName - The name of the method to get.
      Returns:
      The method. The method is set to accessible.