Class GenericTypeReflector

java.lang.Object
io.leangen.geantyref.GenericTypeReflector

public class GenericTypeReflector extends Object
Utility class for doing reflection on types.
  • Field Details

    • UNBOUND_WILDCARD

      private static final WildcardType UNBOUND_WILDCARD
    • BOX_TYPES

      private static final Map<Class<?>,Class<?>> BOX_TYPES
  • Constructor Details

    • GenericTypeReflector

      public GenericTypeReflector()
  • Method Details

    • erase

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

      public static Type box(Type type)
    • isBoxType

      public static boolean isBoxType(Type type)
    • isFullyBound

      public static boolean isFullyBound(Type type)
    • mapTypeParameters

      private static AnnotatedType mapTypeParameters(AnnotatedType toMapType, AnnotatedType typeAndParams)
      Maps type parameters in a type to their values.
      Parameters:
      toMapType - Type possibly containing type arguments
      typeAndParams - must be either ParameterizedType, or (in case there are no type arguments, or it's a raw type) Class
      Returns:
      toMapType, but with type parameters from typeAndParams replaced.
    • mapTypeParameters

      private static AnnotatedType mapTypeParameters(AnnotatedType toMapType, AnnotatedType typeAndParams, VarMap.MappingMode mappingMode)
    • resolveExactType

      public static AnnotatedType resolveExactType(AnnotatedType unresolved, AnnotatedType typeAndParams)
    • resolveExactType

      public static Type resolveExactType(Type unresolved, Type typeAndParams)
    • resolveType

      public static AnnotatedType resolveType(AnnotatedType unresolved, AnnotatedType typeAndParams)
    • resolveType

      public static Type resolveType(Type unresolved, Type typeAndParams)
    • resolveType

      private static AnnotatedType resolveType(AnnotatedType unresolved, AnnotatedType typeAndParams, VarMap.MappingMode mappingMode)
    • isMissingTypeParameters

      public static boolean isMissingTypeParameters(Type type)
      Checks if the given type is a class that is supposed to have type parameters, but doesn't. In other words, if it's a really raw 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 AnnotatedType getExactSuperType(AnnotatedType subType, Class<?> searchSuperClass)
      The equivalent of getExactSuperType(Type, Class) but works with AnnotatedTypes
      Parameters:
      subType - The type whose supertype is to be searched for
      searchSuperClass - The class of the supertype to search for
      Returns:
      The annotated type representing searchSuperClass with type parameters from subType
    • getExactSuperType

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

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

      Parameters:
      subType - The type whose supertype is to be searched for
      searchSuperClass - The class of the supertype to search for
      Returns:
      The type representing searchSuperClass with type parameters from subType
    • getExactSubType

      public static AnnotatedType getExactSubType(AnnotatedType superType, Class<?> searchSubClass)
      The equivalent of getExactSubType(Type, Class) but works with AnnotatedTypes
      Parameters:
      superType - The type whose subtype is to be searched for
      searchSubClass - The class of the subtype to search for
      Returns:
      The annotated type representing searchSubClass with type parameters from superType
    • getExactSubType

      public static Type getExactSubType(Type superType, Class<?> searchSubClass)
      Finds the most specific subtype of superType whose erasure is searchSubClass. In other words, returns a type representing the class searchSubClass plus its exact type parameters in superType, if they are possible to resolve.
      • Returns an instance of AnnotatedParameterizedType if searchSubClass is a real class or interface and superType has parameters for it
      • Returns an instance of AnnotatedArrayType if searchSubClass is an array type, and superType has type parameters for it
      • Returns an instance of AnnotatedType if superType is a raw type, or has no type parameters for searchSubClass
      • Returns null if searchSubClass is not a subclass of superType.

      For example, with getExactSubType(new TypeToken<List<String>>(){}.getAnnotatedType(), ArrayList.class) returns a AnnotatedParameterizedType representing ArrayList<String>.

    • getTypeParameter

      public static AnnotatedType getTypeParameter(AnnotatedType 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).
    • getTypeParameter

      public static Type getTypeParameter(Type type, TypeVariable<? extends Class<?>> variable)
    • isSuperType

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

      private static boolean isArraySupertype(Type arraySuperType, Type subType)
    • getArrayComponentType

      public static AnnotatedType getArrayComponentType(AnnotatedType type)
      If type is an array type, returns the annotated type of the component of the array. Otherwise, returns null.
    • 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.
    • contains

      private static boolean contains(Type containingType, Type containedType)
    • extractVariables

      private static void extractVariables(AnnotatedParameterizedType resolvedTyped, AnnotatedParameterizedType unresolvedType, Class<?> declaringClass, VarMap variables)
    • getExactDirectSuperTypes

      private static AnnotatedType[] getExactDirectSuperTypes(AnnotatedType type)
      Returns the direct supertypes of the given type. Resolves type parameters.
    • getArrayExactDirectSuperTypes

      private static AnnotatedType[] getArrayExactDirectSuperTypes(AnnotatedType arrayType)
    • getExactReturnType

      public static AnnotatedType getExactReturnType(Method m, AnnotatedType declaringType)
      Resolves the exact return type of the given method in the given type. This may be different from m.getAnnotatedReturnType() when the method was declared in a superclass, or declaringType has a type parameter that is used in the return type, or declaringType is a raw type.
    • getExactReturnType

      public static Type getExactReturnType(Method m, Type declaringType)
      Resolves 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 declaringType has a type parameter that is used in the return type, or declaringType is a raw type.
    • getReturnType

      public static AnnotatedType getReturnType(Method m, AnnotatedType declaringType)
      Resolves the return type of the given method in the given type. Any unresolvable variables will be kept (in contrast to getExactReturnType(Method, AnnotatedType)). This may be different from m.getAnnotatedReturnType() when the method was declared in a superclass, or declaringType has a type parameter that is used in the return type, or declaringType is a raw type.
    • getReturnType

      public static Type getReturnType(Method m, Type declaringType)
      Resolves the return type of the given method in the given type. Any unresolvable variables will be kept (in contrast to getExactReturnType(Method, Type)). This may be different from m.getGenericReturnType() when the method was declared in a superclass, or declaringType has a type parameter that is used in the return type, or declaringType is a raw type.
    • getReturnType

      private static AnnotatedType getReturnType(Method m, AnnotatedType declaringType, VarMap.MappingMode mappingMode)
    • getExactFieldType

      public static AnnotatedType getExactFieldType(Field f, AnnotatedType declaringType)
      Resolves the exact type of the given field in the given type. This may be different from f.getAnnotatedType() when the field was declared in a superclass, or declaringType has a type parameter that is used in the type of the field, or declaringType is a raw type.
    • getExactFieldType

      public static Type getExactFieldType(Field f, Type type)
      Resolves 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 declaringType has a type parameter that is used in the type of the field, or declaringType is a raw type.
    • getFieldType

      public static AnnotatedType getFieldType(Field f, AnnotatedType declaringType)
      Resolves the type of the given field in the given type. Any unresolvable variables will be kept (in contrast to getExactFieldType(Field, AnnotatedType)). This may be different from f.getAnnotatedType() when the field was declared in a superclass, or declaringType has a type parameter that is used in the type of the field, or declaringType is a raw type.
    • getFieldType

      public static Type getFieldType(Field f, Type type)
      Resolves the type of the given field in the given type. Any unresolvable variables will be kept (in contrast to getExactFieldType(Field, Type)). This may be different from f.getGenericType() when the field was declared in a superclass, or declaringType has a type parameter that is used in the type of the field, or declaringType is a raw type.
    • getFieldType

      private static AnnotatedType getFieldType(Field f, AnnotatedType declaringType, VarMap.MappingMode mappingMode)
    • getExactParameterTypes

      public static AnnotatedType[] getExactParameterTypes(Executable exe, AnnotatedType declaringType)
      Resolves the exact annotated parameter types of the given method/constructor in the given type. This may be different from exe.getAnnotatedParameterTypes() when the method was declared in a superclass, or declaringType has a type parameter that is used in one of the parameters, or declaringType is a raw type.
    • getExactParameterTypes

      public static Type[] getExactParameterTypes(Executable exe, Type declaringType)
      Resolves the exact parameter types of the given method/constructor in the given type. This may be different from exe.getParameterTypes() when the method was declared in a superclass, or declaringType has a type parameter that is used in one of the parameters, or declaringType is a raw type.
    • getParameterTypes

      public static AnnotatedType[] getParameterTypes(Executable exe, AnnotatedType declaringType)
    • getParameterTypes

      public static Type[] getParameterTypes(Executable exe, Type declaringType)
    • getParameterTypes

      private static AnnotatedType[] getParameterTypes(Executable exe, AnnotatedType declaringType, VarMap.MappingMode mappingMode)
    • capture

      public static AnnotatedType capture(AnnotatedType type)
      Applies capture conversion to the given type.
    • capture

      Applies capture conversion to the given type.
      See Also:
    • 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 {@literal 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.
    • annotate

      private static AnnotatedType annotate(Type type, boolean expandGenerics)
    • annotate

      public static AnnotatedType annotate(Type type)
      Recursively wraps a Type into an AnnotatedType using the annotations found on the erasure classes.
      Parameters:
      type - Type to annotate
      Returns:
      Type whose structure has been recursively annotated
    • annotate

      public static AnnotatedType annotate(Type type, Annotation[] annotations)
      Recursively wraps a Type into an AnnotatedType using the annotations found on the erasure classes, plus adding the provided annotations to the top level Type only.
      Parameters:
      type - Type to annotate
      Returns:
      Type whose structure has been recursively annotated, plus the provided annotation added at the top level

      See annotate(Type)

    • annotate

      private static AnnotatedType annotate(Type type, boolean expandGenerics, Map<GenericTypeReflector.CaptureCacheKey,AnnotatedType> cache)
      This is the method underlying both annotate(Type) and annotate(Type, Annotation[]). It goes recursively through the structure of the provided Type wrapping all type parameters, bounds etc. encountered into AnnotatedTypes using annotations found directly on the corresponding erasure class, with a special treatment for CaptureType which can have infinitely recursive structure by having itself as its upper bound.
      Parameters:
      type - The type to annotate
      cache - The cache for already encountered CaptureTypes. Necessary because CaptureTypes can have infinitely recursive structure.
      Returns:
      Type whose structure has been recursively annotated

      See annotate(Type)

      See annotate(Type, Annotation[])

      See GenericTypeReflector.CaptureCacheKey

      See CaptureType

    • replaceAnnotations

      public static <T extends AnnotatedType> T replaceAnnotations(T original, Annotation[] annotations)
      Creates a new AnnotatedType of the same type as the original, but with its annotations replaced with the provided ones.
      Parameters:
      original - The type whose structure is to be copied
      annotations - Annotations to use instead of the ones found on the original
      Returns:
      A type of the same structure as the original but with replaced annotations
    • updateAnnotations

      public static <T extends AnnotatedType> T updateAnnotations(T original, Annotation[] annotations)
      Creates a new AnnotatedType of the same structure as the original, but with its annotations replaced with the provided ones.
      Parameters:
      original - The type whose structure is to be copied
      annotations - Extra annotations to be added on top of the ones found on the original
      Returns:
      A type of the same structure as the original but with replaced annotations
    • mergeAnnotations

      public static <T extends AnnotatedType> T mergeAnnotations(T t1, T t2)
    • replaceParameters

      public static AnnotatedParameterizedType replaceParameters(AnnotatedParameterizedType type, AnnotatedType[] typeParameters)
      Creates a new AnnotatedParameterizedType of the same raw class as the provided type by with all of its type parameters replaced by typeParameters.
      Parameters:
      type - The original parameterized type from which the raw class is to be taken
      typeParameters - The new type parameters to use
      Returns:
      The new parameterized type
    • replaceParameters

      public static AnnotatedParameterizedType replaceParameters(AnnotatedParameterizedType type, AnnotatedType[] typeParameters, AnnotatedType ownerType)
    • replaceParameters

      private static AnnotatedParameterizedType replaceParameters(AnnotatedParameterizedType type, Annotation[] annotations, AnnotatedType[] typeParameters, AnnotatedType ownerType)
    • toCanonical

      public static <T extends AnnotatedType> T toCanonical(T type)
      Returns an AnnotatedType functionally identical to the given one, but in a canonical form that implements equals and hashCode.
      Parameters:
      type - The type to turn into the canonical form
      Returns:
      A type functionally equivalent to the given one, but in the canonical form
    • toCanonicalBoxed

      public static <T extends AnnotatedType> T toCanonicalBoxed(T type)
      Returns an AnnotatedType functionally identical to the given one, but in a canonical form that implements equals and hashCode and has all the primitives replaced by their boxed form.
      Parameters:
      type - The type to turn into the canonical form
      Returns:
      A type functionally equivalent to the given one, but in the canonical form
    • toCanonical

      private static <T extends AnnotatedType> T toCanonical(T type, Function<Type,Type> leafTransformer)
      This is the method underlying toCanonical(AnnotatedType). It recursively traverses the structure of the provided AnnotatedType turning all type parameters, bounds etc. into their canonical forms, with a special treatment for AnnotatedCaptureType which can have infinitely recursive structure by having itself as its upper bound.
      Parameters:
      type - The type to annotate
      leafTransformer - The transformer function to apply to leaf types (e.g. to box primitives)
      Returns:
      Type whose structure has been recursively annotated

      See toCanonical(AnnotatedType)

      See GenericTypeReflector.AnnotatedCaptureCacheKey

      See AnnotatedCaptureType

      See CaptureType

    • expandGenerics

      private static AnnotatedType expandGenerics(AnnotatedType type)
    • transform

      public static AnnotatedType transform(AnnotatedType type, TypeVisitor visitor)
      Recursively applies a transformation implemented by the provided TypeVisitor to the structure of the given type.
      Parameters:
      type - The type to transform
      visitor - Visitor implementing the transformation
      Returns:
      Type produced by recursively applying the transformation to the given type
    • reduceBounded

      public static AnnotatedType reduceBounded(AnnotatedType type)
      Recursively traverses the structure of the given type, reducing all bounded types (AnnotatedTypeVariable, AnnotatedWildcardType and AnnotatedCaptureType) to their first bound.
      Parameters:
      type - The type to transform
      Returns:
      Type produced by recursively reducing bounded types within the structure of the given type
    • expandClassGenerics

      private static AnnotatedParameterizedType expandClassGenerics(Class<?> type)
    • merge

      public static Annotation[] merge(Annotation[]... annotations)
      Merges an arbitrary number of annotations arrays, and removes duplicates.
      Parameters:
      annotations - Annotation arrays to merge and deduplicate
      Returns:
      An array containing all annotations from the given arrays, without duplicates
    • typeArraysEqual

      static boolean typeArraysEqual(AnnotatedType[] t1, AnnotatedType[] t2)
    • hashCode

      public static int hashCode(AnnotatedType... types)
    • hashCode

      static int hashCode(Stream<Annotation> annotations)
    • equals

      public static boolean equals(AnnotatedType t1, AnnotatedType t2)
      Checks whether the two provided types are of the same structure and annotations on all levels.
      Parameters:
      t1 - The first type to be compared
      t2 - The second type to be compared
      Returns:
      True if both types have the same structure and annotations on all levels
    • buildUpperBoundClassAndInterfaces

      private static void buildUpperBoundClassAndInterfaces(Type type, Set<Class<?>> result)
      Helper method for getUpperBoundClassAndInterfaces, adding the result to the given set.
    • mapArray

      private static <I, O> O[] mapArray(I[] array, IntFunction<O[]> resultCtor, Function<I,O> mapper)