Class GenericTypeReflector


  • public class GenericTypeReflector
    extends Object
    Utility class for doing reflection on types.
    Author:
    Wouter Coekaerts
    • Method Detail

      • erase

        public static Class<?> erase​(Type type)
        Returns the erasure of the given type.
      • addWildcardParameters

        public static Type addWildcardParameters​(Class<?> clazz)
        Returns a type representing the class, with all type parameters the unbound wildcard ("?"). For example, addWildcardParameters(Map.class) returns a type representing Map<?,?>.
        Returns:
        • If clazz is a class or interface without type parameters, clazz itself is returned.
        • If clazz is a class or interface with type parameters, an instance of ParameterizedType is returned.
        • if clazz is an array type, an array type is returned with unbound wildcard parameters added in the the component type.
      • getExactSuperType

        public static Type getExactSuperType​(Type type,
                                             Class<?> searchClass)
        Finds the most specific supertype of type whose erasure is searchClass. In other words, returns a type representing the class searchClass plus its exact type parameters in type.
        • Returns an instance of ParameterizedType if searchClass is a real class or interface and type has parameters for it
        • Returns an instance of GenericArrayType if searchClass is an array type, and type has type parameters for it
        • Returns an instance of Class if type is a raw type, or has no type parameters for searchClass
        • Returns null if searchClass is not a superclass of type.

        For example, with class StringList implements List<String>, getExactSuperType(StringList.class, Collection.class) returns a ParameterizedType representing Collection<String>.

      • getTypeParameter

        public static Type getTypeParameter​(Type type,
                                            TypeVariable<? extends Class<?>> variable)
        Gets the type parameter for a given type that is the value for a given type variable. For example, with class StringList implements List<String>, getTypeParameter(StringList.class, Collection.class.getTypeParameters()[0]) returns String.
        Parameters:
        type - The type to inspect.
        variable - The type variable to find the value for.
        Returns:
        The type parameter for the given variable. Or null if type is not a subtype of the type that declares the variable, or if the variable isn't known (because of raw types).
      • isSuperType

        public static boolean isSuperType​(Type superType,
                                          Type subType)
        Checks if the capture of subType is a subtype of superType
      • getArrayComponentType

        public static Type getArrayComponentType​(Type type)
        If type is an array type, returns the type of the component of the array. Otherwise, returns null.
      • getExactReturnType

        public static Type getExactReturnType​(Method m,
                                              Type type)
        Returns the exact return type of the given method in the given type. This may be different from m.getGenericReturnType() when the method was declared in a superclass, or type has a type parameter that is used in the return type, or type is a raw type.
      • getExactFieldType

        public static Type getExactFieldType​(Field f,
                                             Type type)
        Returns the exact type of the given field in the given type. This may be different from f.getGenericType() when the field was declared in a superclass, or type has a type parameter that is used in the type of the field, or type is a raw type.
      • getExactParameterTypes

        public static Type[] getExactParameterTypes​(Method m,
                                                    Type type)
        Returns the exact parameter types of the given method in the given type. This may be different from m.getGenericParameterTypes() when the method was declared in a superclass, or type has a type parameter that is used in one of the parameters, or type is a raw type.
      • capture

        public static Type capture​(Type type)
        Applies capture conversion to the given type.
      • getTypeName

        public static String getTypeName​(Type type)
        Returns the display name of a Type.
      • getUpperBoundClassAndInterfaces

        public static List<Class<?>> getUpperBoundClassAndInterfaces​(Type type)
        Returns list of classes and interfaces that are supertypes of the given type. For example given this class: class Foo<A extends Number & Iterable<A>, B extends A>
        calling this method on type parameters B (Foo.class.getTypeParameters()[1]) returns a list containing Number and Iterable.

        This is mostly useful if you get a type from one of the other methods in GenericTypeReflector, but you don't want to deal with all the different sorts of types, and you are only really interested in concrete classes and interfaces.

        Returns:
        A List of classes, each of them a supertype of the given type. If the given type is a class or interface itself, returns a List with just the given type. The list contains no duplicates, and is ordered in the order the upper bounds are defined on the type.