Class GenericTypeReflector

java.lang.Object
org.spockframework.gentyref.GenericTypeReflector

public class GenericTypeReflector extends Object
Utility class for doing reflection on types.
Author:
Wouter Coekaerts invalid input: '<'wouter@coekaerts.be>
  • Constructor Details

    • GenericTypeReflector

      public GenericTypeReflector()
  • Method Details

    • 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)
      With type a supertype of searchClass, returns the exact supertype of the given class, including type parameters. For example, with class StringList implements List<String>, getExactSuperType(StringList.class, Collection.class) returns a ParameterizedType representing Collection<String>.
      • Returns null if searchClass is not a superclass of type.
      • Returns an instance of Class if type if it is a raw type, or has no type parameters
      • Returns an instance of ParameterizedType if the type does have parameters
      • Returns an instance of GenericArrayType if searchClass is an array type, and the actual type has type parameters
    • 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.
    • getExactParameterTypes

      public static Type[] getExactParameterTypes(Method m, Type type)
    • 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, of 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, of 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 invalid input: '&' 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.