Package org.spockframework.gentyref
Class GenericTypeReflector
java.lang.Object
org.spockframework.gentyref.GenericTypeReflector
Utility class for doing reflection on types.
- Author:
- Wouter Coekaerts <wouter@coekaerts.be>
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic Type
addWildcardParameters
(Class<?> clazz) Returns a type representing the class, with all type parameters the unbound wildcard ("?").static Type
Applies capture conversion to the given type.static Class<?>
Returns the erasure of the given type.static Type
getArrayComponentType
(Type type) If type is an array type, returns the type of the component of the array.static Type
getExactFieldType
(Field f, Type type) Returns the exact type of the given field in the given type.static Type[]
getExactParameterTypes
(Method m, Type type) static Type
getExactReturnType
(Method m, Type type) Returns the exact return type of the given method in the given type.static Type
getExactSuperType
(Type type, Class<?> searchClass) With type a supertype of searchClass, returns the exact supertype of the given class, including type parameters.static String
getTypeName
(Type type) Returns the display name of a Type.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.Returns list of classes and interfaces that are supertypes of the given type.static boolean
isSuperType
(Type superType, Type subType) Checks if the capture of subType is a subtype of superType
-
Constructor Details
-
GenericTypeReflector
public GenericTypeReflector()
-
-
Method Details
-
erase
Returns the erasure of the given type. -
addWildcardParameters
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
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 aParameterizedType
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
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
Checks if the capture of subType is a subtype of superType -
getArrayComponentType
If type is an array type, returns the type of the component of the array. Otherwise, returns null. -
getExactParameterTypes
-
getExactReturnType
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
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
Applies capture conversion to the given type. -
getTypeName
Returns the display name of a Type. -
getUpperBoundClassAndInterfaces
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.
-