Class ReflectionHelper

java.lang.Object
clazzfish.monitor.util.ReflectionHelper

public class ReflectionHelper extends Object
This class is a helper class to access some fields via reflection. Normally you should avoid reflection. Handle with care if you use it.

Originally this class was part of the PatternTesting project.

Author:
oliver
  • Method Details

    • getField

      public static Field getField(Class<?> cl, String name) throws NoSuchFieldException
      Tries to get the wanted field.
      Parameters:
      cl - the class
      name - the field name
      Returns:
      the wanted field
      Throws:
      NoSuchFieldException - if the field was not found
    • hasField

      public static boolean hasField(Object obj, String name)
      Returns true if the given object have a field with the given name.
      Parameters:
      obj - the obj
      name - the name
      Returns:
      true, if successful
      Since:
      1.5
    • hasField

      public static boolean hasField(Class<?> clazz, String name)
      Returns true if the given class have a field with the given name.
      Parameters:
      clazz - the clazz
      name - the name
      Returns:
      true, if successful
      Since:
      1.5
    • getFieldValue

      public static Object getFieldValue(Object target, String name) throws ReflectiveOperationException
      If it can it returns the value of the given field. If not it throws an exception.
      Parameters:
      target - the target object
      name - the name
      Returns:
      the field value
      Throws:
      ReflectiveOperationException - the reflective operation exception
    • setFieldValue

      public static void setFieldValue(Object target, String name, String value) throws ReflectiveOperationException
      Sets the field value of the given target. With this method you can set private attributes, e.g. for testing.
      Parameters:
      target - the target
      name - the name
      value - the value
      Throws:
      ReflectiveOperationException - the reflective operation exception
      Since:
      1.6
    • getUninitializedNonStaticFields

      public static Collection<Field> getUninitializedNonStaticFields(Object obj)
      To get all uninitialized field you can call this method.
      Parameters:
      obj - the object where to get the fields from
      Returns:
      a collecion of unitialized fields (can be empty)
    • isStatic

      public static boolean isStatic(Field field)
      Checks if the given field is static.
      Parameters:
      field - the field
      Returns:
      true, if is static
    • toShortString

      public static String toShortString(Field field)
      To short string.
      Parameters:
      field - the field
      Returns:
      the string
    • toShortString

      public static String toShortString(Collection<Field> fields)
      To short string.
      Parameters:
      fields - the fields
      Returns:
      the string
    • getMethod

      public static Method getMethod(Class<?> cl, String name, Object... args) throws NoSuchMethodException
      Gets the method.
      Parameters:
      cl - the cl
      name - the name
      args - the args
      Returns:
      the method
      Throws:
      NoSuchMethodException - the no such method exception
    • getMethod

      public static Method getMethod(Class<?> cl, String name, Class<?>... parameterTypes) throws NoSuchMethodException
      Gets the method.
      Parameters:
      cl - e.g. class java.lang.ClassLoader
      name - e.g. "getPackages"
      parameterTypes - the parameter types
      Returns:
      a Method object
      Throws:
      NoSuchMethodException - the no such method exception
    • invokeMethod

      public static Object invokeMethod(Object target, String name, Object... args)
      Invoke method. This could be also a protected or private method.
      Parameters:
      target - the target
      name - the method name
      args - the args for the method
      Returns:
      the result of the method.
    • hasId

      public static boolean hasId(Object obj)
      Checks if the given object has something which looks like an id.
      Parameters:
      obj - the obj
      Returns:
      true, if successful
      Since:
      1.6 (01.06.2015)
    • hasId

      public static boolean hasId(Class<?> clazz)
      Checks if the given class has something which looks like an id attribute.
      Parameters:
      clazz - the clazz
      Returns:
      true, if successful
    • getId

      public static Object getId(Object obj)
      Gets the id of the given object.
      Parameters:
      obj - the obj
      Returns:
      the id
      Since:
      1.6 (01.06.2015)