Class ReflectionUtils


  • public final class ReflectionUtils
    extends Object
    Various Reflection utilities.
    Since:
    2.1RC1
    Version:
    $Id: 0975b8ebed177070bfea1855c2e2e4cece89ff2a $
    • Method Detail

      • getAllFields

        public static Collection<Field> getAllFields​(Class<?> clazz)
        Parameters:
        clazz - the class for which to return all fields
        Returns:
        all fields declared by the passed class and its superclasses
      • getField

        public static Field getField​(Class<?> clazz,
                                     String fieldName)
                              throws NoSuchFieldException
        Parameters:
        clazz - the class for which to return all fields
        fieldName - the name of the field to get
        Returns:
        the field specified from either the passed class or its superclasses
        Throws:
        NoSuchFieldException - if the field doesn't exist in the class or superclasses
      • getTypeClass

        public static Class getTypeClass​(Type type)
        Extract the main class from the passed Type.
        Parameters:
        type - the generic Type
        Returns:
        the main Class of the generic Type
        Since:
        4.0M1
      • setFieldValue

        public static void setFieldValue​(Object instanceContainingField,
                                         String fieldName,
                                         Object fieldValue)
        Sets a value to a field using reflection even if the field is private.
        Parameters:
        instanceContainingField - the object containing the field
        fieldName - the name of the field in the object
        fieldValue - the value to set for the provided field
      • getLastGenericFieldType

        public static Class<?> getLastGenericFieldType​(Field field)
        Extract the last generic type from the passed field. For example private List<A, B> field would return the B class.
        Parameters:
        field - the field from which to extract the generic type
        Returns:
        the class of the last generic type or null if the field doesn't have a generic type
      • getLastFieldGenericArgument

        public static Type getLastFieldGenericArgument​(Field field)
        Extract the last generic type from the passed field. For example private List<A, B> field would return the B class.
        Parameters:
        field - the field from which to extract the generic type
        Returns:
        the type of the last generic type or null if the field doesn't have a generic type
        Since:
        4.0M1
      • getLastTypeGenericArgument

        public static Type getLastTypeGenericArgument​(Type type)
        Extract the last generic type from the passed Type. For example private List<A, B> field would return the B class.
        Parameters:
        type - the type from which to extract the generic type
        Returns:
        the type of the last generic type or null if the field doesn't have a generic type
        Since:
        4.0M1
      • getLastGenericClassType

        public static Class<?> getLastGenericClassType​(Class clazz,
                                                       Class filterClass)
        Extract the last generic type from the passed class. For example public Class MyClass implements FilterClass<A, B>, SomeOtherClass<C> will return B.
        Parameters:
        clazz - the class to extract from
        filterClass - the class of the generic type we're looking for
        Returns:
        the last generic type from the interfaces of the passed class, filtered by the passed filter class
      • getGenericClassType

        public static Type getGenericClassType​(Class clazz,
                                               Class filterClass)
        Extract the real Type from the passed class. For example public Class MyClass implements FilterClass<A, B>, SomeOtherClass<C> will return FilterClass<A, B>, SomeOtherClass<C>.
        Parameters:
        clazz - the class to extract from
        filterClass - the class of the generic type we're looking for
        Returns:
        the real Type from the interfaces of the passed class, filtered by the passed filter class
        Since:
        4.0M1
      • resolveSuperArguments

        public static Type[] resolveSuperArguments​(Type[] parameters,
                                                   Type childType)
        Parameters:
        parameters - the parameters of a direct superclass or interface
        childType - a extending class as Type
        Returns:
        the actual parameters of the direct superclass or interface, return null if it's impossible to resolve
      • resolveSuperArguments

        public static Type[] resolveSuperArguments​(Type[] parameters,
                                                   Class childClass,
                                                   Type[] childParameters)
        Parameters:
        parameters - the parameters of a direct superclass or interface
        childClass - an extending class
        childParameters - the actual parameters of the extending class
        Returns:
        the actual parameters of the direct superclass or interface, return null if it's impossible to resolve
      • resolveType

        public static Type resolveType​(Type type,
                                       Map<TypeVariable,​Type> typeMapping)
        Parameters:
        type - the type to resolve
        typeMapping - the mapping between TypeVariable and real type
        Returns:
        the resolved type, the passed type it does not need to be resolved or null if it can't be resolved
      • resolveType

        public static Type resolveType​(Type targetType,
                                       Type rootType)
        Find and replace the generic parameters with the real types.
        Parameters:
        targetType - the type for which to resolve the parameters
        rootType - an extending class as Type
        Returns:
        the Type with resolved parameters
      • unserializeType

        public static Type unserializeType​(String serializedType,
                                           ClassLoader classLoader)
                                    throws ClassNotFoundException
        Retrieve a Type object from it's serialized form.
        Parameters:
        serializedType - the serialized form of the Type to retrieve
        classLoader - the ClassLoader to look into to find the given Type
        Returns:
        the type built from the given String
        Throws:
        ClassNotFoundException - if no class corresponding to the passed serialized type can be found
      • getDirectAnnotation

        public static <T extends Annotation> T getDirectAnnotation​(Class<T> annotationClass,
                                                                   AnnotatedElement element)
        Get the first found annotation with the provided class directly assigned to the provided AnnotatedElement .
        Type Parameters:
        T - the type of the annotation
        Parameters:
        annotationClass - the annotation class
        element - the class on which annotation are assigned
        Returns:
        the found annotation or null if there is none
      • getDirectTypes

        public static List<Type> getDirectTypes​(Type type)
        Parameters:
        type - the type from which to extract super type and interfaces
        Returns:
        the direct super type and interfaces for the provided type
      • serializeType

        public static String serializeType​(Type type)
        Serialize a type in a String using a standard definition.
        Parameters:
        type - the type to serialize.
        Returns:
        a string representing this type.
        Since:
        11.2RC1
      • getAllMethods

        public static Collection<Method> getAllMethods​(Class<?> clazz)
        Find all methods (public, private, package private, protected) in the current class and in all its superclasses.
        Parameters:
        clazz - the class for which to return all the methods
        Returns:
        the collections of methods (in the defined order from current to superclasses)
        Since:
        12.5RC1