org.spockframework.gentyref
Class GenericTypeReflector

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

public class GenericTypeReflector
extends java.lang.Object

Utility class for doing reflection on types.

Author:
Wouter Coekaerts

Constructor Summary
GenericTypeReflector()
           
 
Method Summary
static java.lang.reflect.Type addWildcardParameters(java.lang.Class<?> clazz)
          Returns a type representing the class, with all type parameters the unbound wildcard ("?").
static java.lang.reflect.Type capture(java.lang.reflect.Type type)
          Applies capture conversion to the given type.
static java.lang.Class<?> erase(java.lang.reflect.Type type)
          Returns the erasure of the given type.
static java.lang.reflect.Type getArrayComponentType(java.lang.reflect.Type type)
          If type is an array type, returns the type of the component of the array.
static java.lang.reflect.Type getExactFieldType(java.lang.reflect.Field f, java.lang.reflect.Type type)
          Returns the exact type of the given field in the given type.
static java.lang.reflect.Type[] getExactParameterTypes(java.lang.reflect.Method m, java.lang.reflect.Type type)
           
static java.lang.reflect.Type getExactReturnType(java.lang.reflect.Method m, java.lang.reflect.Type type)
          Returns the exact return type of the given method in the given type.
static java.lang.reflect.Type getExactSuperType(java.lang.reflect.Type type, java.lang.Class<?> searchClass)
          With type a supertype of searchClass, returns the exact supertype of the given class, including type parameters.
static java.lang.String getTypeName(java.lang.reflect.Type type)
          Returns the display name of a Type.
static java.lang.reflect.Type getTypeParameter(java.lang.reflect.Type type, java.lang.reflect.TypeVariable<? extends java.lang.Class<?>> variable)
          Gets the type parameter for a given type that is the value for a given type variable.
static java.util.List<java.lang.Class<?>> getUpperBoundClassAndInterfaces(java.lang.reflect.Type type)
          Returns list of classes and interfaces that are supertypes of the given type.
static boolean isSuperType(java.lang.reflect.Type superType, java.lang.reflect.Type subType)
          Checks if the capture of subType is a subtype of superType
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

GenericTypeReflector

public GenericTypeReflector()
Method Detail

erase

public static java.lang.Class<?> erase(java.lang.reflect.Type type)
Returns the erasure of the given type.


addWildcardParameters

public static java.lang.reflect.Type addWildcardParameters(java.lang.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 java.lang.reflect.Type getExactSuperType(java.lang.reflect.Type type,
                                                       java.lang.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>.


getTypeParameter

public static java.lang.reflect.Type getTypeParameter(java.lang.reflect.Type type,
                                                      java.lang.reflect.TypeVariable<? extends java.lang.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(java.lang.reflect.Type superType,
                                  java.lang.reflect.Type subType)
Checks if the capture of subType is a subtype of superType


getArrayComponentType

public static java.lang.reflect.Type getArrayComponentType(java.lang.reflect.Type type)
If type is an array type, returns the type of the component of the array. Otherwise, returns null.


getExactParameterTypes

public static java.lang.reflect.Type[] getExactParameterTypes(java.lang.reflect.Method m,
                                                              java.lang.reflect.Type type)

getExactReturnType

public static java.lang.reflect.Type getExactReturnType(java.lang.reflect.Method m,
                                                        java.lang.reflect.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 java.lang.reflect.Type getExactFieldType(java.lang.reflect.Field f,
                                                       java.lang.reflect.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 java.lang.reflect.Type capture(java.lang.reflect.Type type)
Applies capture conversion to the given type.


getTypeName

public static java.lang.String getTypeName(java.lang.reflect.Type type)
Returns the display name of a Type.


getUpperBoundClassAndInterfaces

public static java.util.List<java.lang.Class<?>> getUpperBoundClassAndInterfaces(java.lang.reflect.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.


Copyright © 2013. All rights reserved