Class ReflectionUtils


  • public final class ReflectionUtils
    extends java.lang.Object
    Convenience methods dealing with java reflection api.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <K> java.lang.Class<K> extractGenericParameterType​(java.lang.Class<?> clazz, int position)
      static <P,​C extends P,​K>
      java.lang.Class<K>
      extractGenericParameterType​(java.lang.Class<C> clazz, java.lang.Class<P> baseClass)
      static <P,​C extends P,​K>
      java.lang.Class<K>
      extractGenericParameterType​(java.lang.Class<C> clazz, java.lang.Class<P> baseClass, int position)
      Helper method to fetch generic argument's type of a class.
      static java.lang.reflect.Field[] getAllDeclaredFields​(java.lang.Class<?> clazz)
      Getting all declared fields from the specified class and all its superclasses.
      static java.lang.reflect.Field getDeclaredField​(java.lang.Class<?> clazz, java.lang.String fieldName)
      Looks for declared field in specified class and all its superclasses.
      static <V> V getField​(java.lang.reflect.Field field, java.lang.Object object)
      Getting a fields value regardless its accessibility.
      static void setField​(java.lang.reflect.Field field, java.lang.Object object, java.lang.Object value)
      Setting a fields value regardless its accessibility.
      static void setField​(java.lang.reflect.Field field, java.lang.Object object, java.lang.Object value, java.lang.Class<? extends java.lang.annotation.Annotation> annotationConstraint)
      Setting a fields value regardless its accessibility.
      • Methods inherited from class java.lang.Object

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

      • getAllDeclaredFields

        public static java.lang.reflect.Field[] getAllDeclaredFields​(java.lang.Class<?> clazz)
        Getting all declared fields from the specified class and all its superclasses.
        See Also:
        Class.getDeclaredFields()
      • getDeclaredField

        public static java.lang.reflect.Field getDeclaredField​(java.lang.Class<?> clazz,
                                                               java.lang.String fieldName)
                                                        throws java.lang.NoSuchFieldException
        Looks for declared field in specified class and all its superclasses.
        Throws:
        java.lang.NoSuchFieldException
        See Also:
        Class.getDeclaredFields()
      • getField

        public static <V> V getField​(java.lang.reflect.Field field,
                                     java.lang.Object object)
        Getting a fields value regardless its accessibility.
        Parameters:
        field - The field which value should be read.
        object - The object at which the field value should be read
        Throws:
        java.lang.IllegalArgumentException
        See Also:
        Field.get(Object)
      • setField

        public static void setField​(java.lang.reflect.Field field,
                                    java.lang.Object object,
                                    java.lang.Object value)
                             throws java.lang.IllegalArgumentException
        Setting a fields value regardless its accessibility.
        Parameters:
        field - The field which value should be set.
        object - The object at which the field value should be set
        value - The value
        Throws:
        java.lang.IllegalArgumentException
        java.lang.IllegalAccessException
        See Also:
        Field.set(Object, Object)
      • setField

        public static void setField​(java.lang.reflect.Field field,
                                    java.lang.Object object,
                                    java.lang.Object value,
                                    java.lang.Class<? extends java.lang.annotation.Annotation> annotationConstraint)
        Setting a fields value regardless its accessibility.
        Parameters:
        field - The field which value should be set.
        object - The object at which the field value should be set
        value - The value
        annotationConstraint - A annotation class serving as constraint, i.e. the field value will only be set, if this annotation is present.
        See Also:
        Field.set(Object, Object)
      • extractGenericParameterType

        public static <K> java.lang.Class<K> extractGenericParameterType​(java.lang.Class<?> clazz,
                                                                         int position)
        Simplified version of method extractGenericParameterType(Class, Class, int). A superclass of provided class is taken as a baseClass.
      • extractGenericParameterType

        public static <P,​C extends P,​K> java.lang.Class<K> extractGenericParameterType​(java.lang.Class<C> clazz,
                                                                                                   java.lang.Class<P> baseClass)
        Simplified version of method extractGenericParameterType(Class, Class, int). First generic parameter is assumed (position 0).
      • extractGenericParameterType

        public static <P,​C extends P,​K> java.lang.Class<K> extractGenericParameterType​(java.lang.Class<C> clazz,
                                                                                                   java.lang.Class<P> baseClass,
                                                                                                   int position)
        Helper method to fetch generic argument's type of a class. For example when invoked on a class that extends from ArrayList<String>:

        ReflectionUtils.extractGenericParameterType(new ArrayList<String>(){}.getClass(), 0)

        returns class java.lang.String.

        Method may resolve only those parameters that are already set in class definition. If a class defines a generic parameter by itself, then i may not be resolved, i.e.

        ReflectionUtils.extractGenericParameterType(new ArrayList<String>().getClass(), 0)

        returns null.
        Parameters:
        clazz - type to be scanned
        baseClass - type which parameter is to be found. Ff a class implements multiple generic interfaces, or deeper class hesitancy is involved, then parameter is used to implicitly specify whose generic parameter is to be extracted
        position - generic argument's position
        Returns:
        generic argument's type or null if argument cannot be resolved