org.apache.tapestry5.ioc.internal.util
Class GenericsUtils

java.lang.Object
  extended by org.apache.tapestry5.ioc.internal.util.GenericsUtils

public class GenericsUtils
extends Object

Static methods related to the use of JDK 1.5 generics.


Constructor Summary
GenericsUtils()
           
 
Method Summary
static Class asClass(Type actualType)
          Get the class represented by the reflected type.
static Type extractActualType(Type containingType, Field field)
          Analyzes the method in the context of containingClass and returns the Class that is represented by the method's generic return type.
static Type extractActualType(Type containingType, Method method)
          Analyzes the method in the context of containingClass and returns the Class that is represented by the method's generic return type.
static Class extractGenericFieldType(Class containingClass, Field field)
          Analyzes the field in the context of containingClass and returns the Class that is represented by the field's generic type.
static Class<?> extractGenericReturnType(Class<?> containingClass, Method method)
          Analyzes the method in the context of containingClass and returns the Class that is represented by the method's generic return type.
static boolean isAssignableFrom(Type suspectedSuperType, Type suspectedSubType)
          Determines if the suspected super type is assignable from the suspected sub type.
static Type resolve(Type type, Type containingType)
          Resolves the type parameter based on the context of the containingType.
static String toString(Type type)
          Convert the type into a string.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

GenericsUtils

public GenericsUtils()
Method Detail

extractGenericReturnType

public static Class<?> extractGenericReturnType(Class<?> containingClass,
                                                Method method)
Analyzes the method in the context of containingClass and returns the Class that is represented by the method's generic return type. Any parameter information in the generic return type is lost. If you want to preserve the type parameters of the return type consider using extractActualType(java.lang.reflect.Type, java.lang.reflect.Method).

Parameters:
containingClass - class which either contains or inherited the method
method - method from which to extract the return type
Returns:
the class represented by the methods generic return type, resolved based on the context .
See Also:
extractActualType(java.lang.reflect.Type, java.lang.reflect.Method), resolve(java.lang.reflect.Type,java.lang.reflect.Type), asClass(java.lang.reflect.Type)

extractGenericFieldType

public static Class extractGenericFieldType(Class containingClass,
                                            Field field)
Analyzes the field in the context of containingClass and returns the Class that is represented by the field's generic type. Any parameter information in the generic type is lost, if you want to preserve the type parameters of the return type consider using getTypeVariableIndex(java.lang.reflect.TypeVariable).

Parameters:
containingClass - class which either contains or inherited the field
field - field from which to extract the type
Returns:
the class represented by the field's generic type, resolved based on the containingClass.
See Also:
extractActualType(java.lang.reflect.Type, java.lang.reflect.Field), resolve(java.lang.reflect.Type,java.lang.reflect.Type), asClass(java.lang.reflect.Type)

extractActualType

public static Type extractActualType(Type containingType,
                                     Method method)
Analyzes the method in the context of containingClass and returns the Class that is represented by the method's generic return type. Any parameter information in the generic return type is lost.

Parameters:
containingType - Type which is/represents the class that either contains or inherited the method
method - method from which to extract the generic return type
Returns:
the generic type represented by the methods generic return type, resolved based on the containingType.
See Also:
resolve(java.lang.reflect.Type,java.lang.reflect.Type)

extractActualType

public static Type extractActualType(Type containingType,
                                     Field field)
Analyzes the method in the context of containingClass and returns the Class that is represented by the method's generic return type. Any parameter information in the generic return type is lost.

Parameters:
containingType - Type which is/represents the class that either contains or inherited the field
field - field from which to extract the generic return type
Returns:
the generic type represented by the methods generic return type, resolved based on the containingType.
See Also:
resolve(java.lang.reflect.Type,java.lang.reflect.Type)

resolve

public static Type resolve(Type type,
                           Type containingType)
Resolves the type parameter based on the context of the containingType.

TypeVariable will be unwrapped to the type argument resolved form the class hierarchy. This may be something other than a simple Class if the type argument is a ParameterizedType for instance (e.g. List<E>; List<Map<Long, String>>, E would be returned as a ParameterizedType with the raw type Map and type arguments Long and String.

Parameters:
type - the generic type (ParameterizedType, GenericArrayType, WildcardType, TypeVariable) to be resolved
containingType - the type which his
Returns:
the type resolved to the best of our ability.
Since:
5.2.?

isAssignableFrom

public static boolean isAssignableFrom(Type suspectedSuperType,
                                       Type suspectedSubType)
Determines if the suspected super type is assignable from the suspected sub type.

Parameters:
suspectedSuperType - e.g. GenericDAO<Pet, String>
suspectedSubType - e.g. PetDAO extends GenericDAO<Pet,String>
Returns:
true if (sourceType)targetClass is a valid cast

asClass

public static Class asClass(Type actualType)
Get the class represented by the reflected type. This method is lossy; You cannot recover the type information from the class that is returned.

TypeVariable the first bound is returned. If your type variable extends multiple interfaces that information is lost.

WildcardType the first lower bound is returned. If the wildcard is defined with upper bounds then Object is returned.

Parameters:
actualType - a Class, ParameterizedType, GenericArrayType
Returns:
the un-parameterized class associated with the type.

toString

public static String toString(Type type)
Convert the type into a string. The string representation approximates the code that would be used to define the type.

Parameters:
type - - the type.
Returns:
a string representation of the type, similar to how it was declared.


Copyright © 2003-2012 The Apache Software Foundation.