org.springframework.core
Class GenericCollectionTypeResolver

java.lang.Object
  extended by org.springframework.core.GenericCollectionTypeResolver

public abstract class GenericCollectionTypeResolver
extends java.lang.Object

Helper class for determining element types of collections and maps.

Mainly intended for usage within the framework, determining the target type of values to be added to a collection or map (to be able to attempt type conversion if appropriate).

Since:
2.0
Author:
Juergen Hoeller

Constructor Summary
GenericCollectionTypeResolver()
           
 
Method Summary
private static java.lang.Class<?> extractType(MethodParameter methodParam, java.lang.reflect.Type type, java.lang.Class<?> source, int typeIndex, int nestingLevel, int currentLevel)
          Extract the generic type from the given Type object.
private static java.lang.Class<?> extractTypeFromClass(java.lang.Class<?> clazz, java.lang.Class<?> source, int typeIndex)
          Extract the generic type from the given Class object.
private static java.lang.Class<?> extractTypeFromClass(MethodParameter methodParam, java.lang.Class<?> clazz, java.lang.Class<?> source, int typeIndex, int nestingLevel, int currentLevel)
          Extract the generic type from the given Class object.
private static java.lang.Class<?> extractTypeFromParameterizedType(MethodParameter methodParam, java.lang.reflect.ParameterizedType ptype, java.lang.Class<?> source, int typeIndex, int nestingLevel, int currentLevel)
          Extract the generic type from the given ParameterizedType object.
static java.lang.Class<?> getCollectionFieldType(java.lang.reflect.Field collectionField)
          Determine the generic element type of the given Collection field.
static java.lang.Class<?> getCollectionFieldType(java.lang.reflect.Field collectionField, int nestingLevel)
          Determine the generic element type of the given Collection field.
static java.lang.Class<?> getCollectionParameterType(MethodParameter methodParam)
          Determine the generic element type of the given Collection parameter.
static java.lang.Class<?> getCollectionReturnType(java.lang.reflect.Method method)
          Determine the generic element type of the given Collection return type.
static java.lang.Class<?> getCollectionReturnType(java.lang.reflect.Method method, int nestingLevel)
          Determine the generic element type of the given Collection return type.
static java.lang.Class<?> getCollectionType(java.lang.Class<? extends java.util.Collection> collectionClass)
          Determine the generic element type of the given Collection class (if it declares one through a generic superclass or generic interface).
private static java.lang.Class<?> getGenericFieldType(java.lang.reflect.Field field, java.lang.Class<?> source, int typeIndex, int nestingLevel)
          Extract the generic type from the given field.
private static java.lang.Class<?> getGenericParameterType(MethodParameter methodParam, java.lang.Class<?> source, int typeIndex)
          Extract the generic parameter type from the given method or constructor.
private static java.lang.Class<?> getGenericReturnType(java.lang.reflect.Method method, java.lang.Class<?> source, int typeIndex, int nestingLevel)
          Extract the generic return type from the given method.
static java.lang.Class<?> getMapKeyFieldType(java.lang.reflect.Field mapField)
          Determine the generic key type of the given Map field.
static java.lang.Class<?> getMapKeyFieldType(java.lang.reflect.Field mapField, int nestingLevel)
          Determine the generic key type of the given Map field.
static java.lang.Class<?> getMapKeyParameterType(MethodParameter methodParam)
          Determine the generic key type of the given Map parameter.
static java.lang.Class<?> getMapKeyReturnType(java.lang.reflect.Method method)
          Determine the generic key type of the given Map return type.
static java.lang.Class<?> getMapKeyReturnType(java.lang.reflect.Method method, int nestingLevel)
          Determine the generic key type of the given Map return type.
static java.lang.Class<?> getMapKeyType(java.lang.Class<? extends java.util.Map> mapClass)
          Determine the generic key type of the given Map class (if it declares one through a generic superclass or generic interface).
static java.lang.Class<?> getMapValueFieldType(java.lang.reflect.Field mapField)
          Determine the generic value type of the given Map field.
static java.lang.Class<?> getMapValueFieldType(java.lang.reflect.Field mapField, int nestingLevel)
          Determine the generic value type of the given Map field.
static java.lang.Class<?> getMapValueParameterType(MethodParameter methodParam)
          Determine the generic value type of the given Map parameter.
static java.lang.Class<?> getMapValueReturnType(java.lang.reflect.Method method)
          Determine the generic value type of the given Map return type.
static java.lang.Class<?> getMapValueReturnType(java.lang.reflect.Method method, int nestingLevel)
          Determine the generic value type of the given Map return type.
static java.lang.Class<?> getMapValueType(java.lang.Class<? extends java.util.Map> mapClass)
          Determine the generic value type of the given Map class (if it declares one through a generic superclass or generic interface).
private static boolean isIntrospectionCandidate(java.lang.Class clazz)
          Determine whether the given class is a potential candidate that defines generic collection or map types.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

GenericCollectionTypeResolver

public GenericCollectionTypeResolver()
Method Detail

getCollectionType

public static java.lang.Class<?> getCollectionType(java.lang.Class<? extends java.util.Collection> collectionClass)
Determine the generic element type of the given Collection class (if it declares one through a generic superclass or generic interface).

Parameters:
collectionClass - the collection class to introspect
Returns:
the generic type, or null if none

getMapKeyType

public static java.lang.Class<?> getMapKeyType(java.lang.Class<? extends java.util.Map> mapClass)
Determine the generic key type of the given Map class (if it declares one through a generic superclass or generic interface).

Parameters:
mapClass - the map class to introspect
Returns:
the generic type, or null if none

getMapValueType

public static java.lang.Class<?> getMapValueType(java.lang.Class<? extends java.util.Map> mapClass)
Determine the generic value type of the given Map class (if it declares one through a generic superclass or generic interface).

Parameters:
mapClass - the map class to introspect
Returns:
the generic type, or null if none

getCollectionFieldType

public static java.lang.Class<?> getCollectionFieldType(java.lang.reflect.Field collectionField)
Determine the generic element type of the given Collection field.

Parameters:
collectionField - the collection field to introspect
Returns:
the generic type, or null if none

getCollectionFieldType

public static java.lang.Class<?> getCollectionFieldType(java.lang.reflect.Field collectionField,
                                                        int nestingLevel)
Determine the generic element type of the given Collection field.

Parameters:
collectionField - the collection field to introspect
nestingLevel - the nesting level of the target type (typically 1; e.g. in case of a List of Lists, 1 would indicate the nested List, whereas 2 would indicate the element of the nested List)
Returns:
the generic type, or null if none

getMapKeyFieldType

public static java.lang.Class<?> getMapKeyFieldType(java.lang.reflect.Field mapField)
Determine the generic key type of the given Map field.

Parameters:
mapField - the map field to introspect
Returns:
the generic type, or null if none

getMapKeyFieldType

public static java.lang.Class<?> getMapKeyFieldType(java.lang.reflect.Field mapField,
                                                    int nestingLevel)
Determine the generic key type of the given Map field.

Parameters:
mapField - the map field to introspect
nestingLevel - the nesting level of the target type (typically 1; e.g. in case of a List of Lists, 1 would indicate the nested List, whereas 2 would indicate the element of the nested List)
Returns:
the generic type, or null if none

getMapValueFieldType

public static java.lang.Class<?> getMapValueFieldType(java.lang.reflect.Field mapField)
Determine the generic value type of the given Map field.

Parameters:
mapField - the map field to introspect
Returns:
the generic type, or null if none

getMapValueFieldType

public static java.lang.Class<?> getMapValueFieldType(java.lang.reflect.Field mapField,
                                                      int nestingLevel)
Determine the generic value type of the given Map field.

Parameters:
mapField - the map field to introspect
nestingLevel - the nesting level of the target type (typically 1; e.g. in case of a List of Lists, 1 would indicate the nested List, whereas 2 would indicate the element of the nested List)
Returns:
the generic type, or null if none

getCollectionParameterType

public static java.lang.Class<?> getCollectionParameterType(MethodParameter methodParam)
Determine the generic element type of the given Collection parameter.

Parameters:
methodParam - the method parameter specification
Returns:
the generic type, or null if none

getMapKeyParameterType

public static java.lang.Class<?> getMapKeyParameterType(MethodParameter methodParam)
Determine the generic key type of the given Map parameter.

Parameters:
methodParam - the method parameter specification
Returns:
the generic type, or null if none

getMapValueParameterType

public static java.lang.Class<?> getMapValueParameterType(MethodParameter methodParam)
Determine the generic value type of the given Map parameter.

Parameters:
methodParam - the method parameter specification
Returns:
the generic type, or null if none

getCollectionReturnType

public static java.lang.Class<?> getCollectionReturnType(java.lang.reflect.Method method)
Determine the generic element type of the given Collection return type.

Parameters:
method - the method to check the return type for
Returns:
the generic type, or null if none

getCollectionReturnType

public static java.lang.Class<?> getCollectionReturnType(java.lang.reflect.Method method,
                                                         int nestingLevel)
Determine the generic element type of the given Collection return type.

If the specified nesting level is higher than 1, the element type of a nested Collection/Map will be analyzed.

Parameters:
method - the method to check the return type for
nestingLevel - the nesting level of the target type (typically 1; e.g. in case of a List of Lists, 1 would indicate the nested List, whereas 2 would indicate the element of the nested List)
Returns:
the generic type, or null if none

getMapKeyReturnType

public static java.lang.Class<?> getMapKeyReturnType(java.lang.reflect.Method method)
Determine the generic key type of the given Map return type.

Parameters:
method - the method to check the return type for
Returns:
the generic type, or null if none

getMapKeyReturnType

public static java.lang.Class<?> getMapKeyReturnType(java.lang.reflect.Method method,
                                                     int nestingLevel)
Determine the generic key type of the given Map return type.

Parameters:
method - the method to check the return type for
nestingLevel - the nesting level of the target type (typically 1; e.g. in case of a List of Lists, 1 would indicate the nested List, whereas 2 would indicate the element of the nested List)
Returns:
the generic type, or null if none

getMapValueReturnType

public static java.lang.Class<?> getMapValueReturnType(java.lang.reflect.Method method)
Determine the generic value type of the given Map return type.

Parameters:
method - the method to check the return type for
Returns:
the generic type, or null if none

getMapValueReturnType

public static java.lang.Class<?> getMapValueReturnType(java.lang.reflect.Method method,
                                                       int nestingLevel)
Determine the generic value type of the given Map return type.

Parameters:
method - the method to check the return type for
nestingLevel - the nesting level of the target type (typically 1; e.g. in case of a List of Lists, 1 would indicate the nested List, whereas 2 would indicate the element of the nested List)
Returns:
the generic type, or null if none

getGenericParameterType

private static java.lang.Class<?> getGenericParameterType(MethodParameter methodParam,
                                                          java.lang.Class<?> source,
                                                          int typeIndex)
Extract the generic parameter type from the given method or constructor.

Parameters:
methodParam - the method parameter specification
source - the source class/interface defining the generic parameter types
typeIndex - the index of the type (e.g. 0 for Collections, 0 for Map keys, 1 for Map values)
Returns:
the generic type, or null if none

getGenericFieldType

private static java.lang.Class<?> getGenericFieldType(java.lang.reflect.Field field,
                                                      java.lang.Class<?> source,
                                                      int typeIndex,
                                                      int nestingLevel)
Extract the generic type from the given field.

Parameters:
field - the field to check the type for
source - the source class/interface defining the generic parameter types
typeIndex - the index of the type (e.g. 0 for Collections, 0 for Map keys, 1 for Map values)
nestingLevel - the nesting level of the target type
Returns:
the generic type, or null if none

getGenericReturnType

private static java.lang.Class<?> getGenericReturnType(java.lang.reflect.Method method,
                                                       java.lang.Class<?> source,
                                                       int typeIndex,
                                                       int nestingLevel)
Extract the generic return type from the given method.

Parameters:
method - the method to check the return type for
source - the source class/interface defining the generic parameter types
typeIndex - the index of the type (e.g. 0 for Collections, 0 for Map keys, 1 for Map values)
nestingLevel - the nesting level of the target type
Returns:
the generic type, or null if none

extractType

private static java.lang.Class<?> extractType(MethodParameter methodParam,
                                              java.lang.reflect.Type type,
                                              java.lang.Class<?> source,
                                              int typeIndex,
                                              int nestingLevel,
                                              int currentLevel)
Extract the generic type from the given Type object.

Parameters:
methodParam - the method parameter specification
type - the Type to check
source - the source collection/map Class that we check
typeIndex - the index of the actual type argument
nestingLevel - the nesting level of the target type
currentLevel - the current nested level
Returns:
the generic type as Class, or null if none

extractTypeFromParameterizedType

private static java.lang.Class<?> extractTypeFromParameterizedType(MethodParameter methodParam,
                                                                   java.lang.reflect.ParameterizedType ptype,
                                                                   java.lang.Class<?> source,
                                                                   int typeIndex,
                                                                   int nestingLevel,
                                                                   int currentLevel)
Extract the generic type from the given ParameterizedType object.

Parameters:
methodParam - the method parameter specification
ptype - the ParameterizedType to check
source - the expected raw source type (can be null)
typeIndex - the index of the actual type argument
nestingLevel - the nesting level of the target type
currentLevel - the current nested level
Returns:
the generic type as Class, or null if none

extractTypeFromClass

private static java.lang.Class<?> extractTypeFromClass(java.lang.Class<?> clazz,
                                                       java.lang.Class<?> source,
                                                       int typeIndex)
Extract the generic type from the given Class object.

Parameters:
clazz - the Class to check
source - the expected raw source type (can be null)
typeIndex - the index of the actual type argument
Returns:
the generic type as Class, or null if none

extractTypeFromClass

private static java.lang.Class<?> extractTypeFromClass(MethodParameter methodParam,
                                                       java.lang.Class<?> clazz,
                                                       java.lang.Class<?> source,
                                                       int typeIndex,
                                                       int nestingLevel,
                                                       int currentLevel)
Extract the generic type from the given Class object.

Parameters:
methodParam - the method parameter specification
clazz - the Class to check
source - the expected raw source type (can be null)
typeIndex - the index of the actual type argument
nestingLevel - the nesting level of the target type
currentLevel - the current nested level
Returns:
the generic type as Class, or null if none

isIntrospectionCandidate

private static boolean isIntrospectionCandidate(java.lang.Class clazz)
Determine whether the given class is a potential candidate that defines generic collection or map types.

Parameters:
clazz - the class to check
Returns:
whether the given class is assignable to Collection or Map