Class ReflectionHelper


  • public class ReflectionHelper
    extends java.lang.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.
    Since:
    09.03.2009
    Version:
    $Revision: 1.25 $
    Author:
    oliver
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.reflect.Field getField​(java.lang.Class<?> cl, java.lang.String name)
      Tries to get the wanted field.
      static java.lang.Object getFieldValue​(java.lang.Object target, java.lang.String name)
      If it can it returns the value of the given field.
      static java.lang.Object getId​(java.lang.Object obj)
      Gets the id of the given object.
      static java.lang.reflect.Method getMethod​(java.lang.Class<?> cl, java.lang.String name, java.lang.Class<?>... parameterTypes)
      Gets the method.
      static java.lang.reflect.Method getMethod​(java.lang.Class<?> cl, java.lang.String name, java.lang.Object... args)
      Gets the method.
      static java.util.Collection<java.lang.reflect.Field> getUninitializedNonStaticFields​(java.lang.Object obj)
      To get all uninitialized field you can call this method.
      static boolean hasField​(java.lang.Class<?> clazz, java.lang.String name)
      Returns true if the given class have a field with the given name.
      static boolean hasField​(java.lang.Object obj, java.lang.String name)
      Returns true if the given object have a field with the given name.
      static boolean hasId​(java.lang.Class<?> clazz)
      Checks if the given class has something which looks like an id attribute.
      static boolean hasId​(java.lang.Object obj)
      Checks if the given object has something which looks like an id.
      static java.lang.Object invokeMethod​(java.lang.Object target, java.lang.String name, java.lang.Object... args)
      Invoke method.
      static boolean isStatic​(java.lang.reflect.Field field)
      Checks if the given field is static.
      static void setFieldValue​(java.lang.Object target, java.lang.String name, java.lang.String value)
      Sets the field value of the given target.
      static java.lang.String toShortString​(java.lang.reflect.Field field)
      To short string.
      static java.lang.String toShortString​(java.util.Collection<java.lang.reflect.Field> fields)
      To short string.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • getField

        public static java.lang.reflect.Field getField​(java.lang.Class<?> cl,
                                                       java.lang.String name)
                                                throws java.lang.NoSuchFieldException
        Tries to get the wanted field.
        Parameters:
        cl - the cl
        name - the name
        Returns:
        the wanted field
        Throws:
        java.lang.NoSuchFieldException - the no such field exception
      • hasField

        public static boolean hasField​(java.lang.Object obj,
                                       java.lang.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​(java.lang.Class<?> clazz,
                                       java.lang.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 java.lang.Object getFieldValue​(java.lang.Object target,
                                                     java.lang.String name)
                                              throws java.lang.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:
        java.lang.ReflectiveOperationException - the reflective operation exception
      • setFieldValue

        public static void setFieldValue​(java.lang.Object target,
                                         java.lang.String name,
                                         java.lang.String value)
                                  throws java.lang.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:
        java.lang.ReflectiveOperationException - the reflective operation exception
        Since:
        1.6
      • getUninitializedNonStaticFields

        public static java.util.Collection<java.lang.reflect.Field> getUninitializedNonStaticFields​(java.lang.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​(java.lang.reflect.Field field)
        Checks if the given field is static.
        Parameters:
        field - the field
        Returns:
        true, if is static
      • toShortString

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

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

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

        public static java.lang.reflect.Method getMethod​(java.lang.Class<?> cl,
                                                         java.lang.String name,
                                                         java.lang.Class<?>... parameterTypes)
                                                  throws java.lang.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:
        java.lang.NoSuchMethodException - the no such method exception
      • invokeMethod

        public static java.lang.Object invokeMethod​(java.lang.Object target,
                                                    java.lang.String name,
                                                    java.lang.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​(java.lang.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​(java.lang.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 java.lang.Object getId​(java.lang.Object obj)
        Gets the id of the given object.
        Parameters:
        obj - the obj
        Returns:
        the id
        Since:
        1.6 (01.06.2015)