genericcollectiontyperesolver.java
来自「spring framework 2.5.4源代码」· Java 代码 · 共 425 行 · 第 1/2 页
JAVA
425 行
/*
* Copyright 2002-2008 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.core;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.GenericArrayType;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.lang.reflect.WildcardType;
import java.util.Collection;
import java.util.Map;
/**
* Helper class for determining element types of collections and maps.
*
* <p>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).
*
* <p>Only usable on Java 5. Use an appropriate JdkVersion check before
* calling this class, if a fallback for JDK 1.4 is desirable.
*
* @author Juergen Hoeller
* @since 2.0
* @see org.springframework.beans.BeanWrapperImpl
* @see JdkVersion
*/
public abstract class GenericCollectionTypeResolver {
/**
* Determine the generic element type of the given Collection class
* (if it declares one through a generic superclass or generic interface).
* @param collectionClass the collection class to introspect
* @return the generic type, or <code>null</code> if none
*/
public static Class getCollectionType(Class collectionClass) {
return extractTypeFromClass(collectionClass, Collection.class, 0);
}
/**
* Determine the generic key type of the given Map class
* (if it declares one through a generic superclass or generic interface).
* @param mapClass the map class to introspect
* @return the generic type, or <code>null</code> if none
*/
public static Class getMapKeyType(Class mapClass) {
return extractTypeFromClass(mapClass, Map.class, 0);
}
/**
* Determine the generic value type of the given Map class
* (if it declares one through a generic superclass or generic interface).
* @param mapClass the map class to introspect
* @return the generic type, or <code>null</code> if none
*/
public static Class getMapValueType(Class mapClass) {
return extractTypeFromClass(mapClass, Map.class, 1);
}
/**
* Determine the generic element type of the given Collection field.
* @param collectionField the collection field to introspect
* @return the generic type, or <code>null</code> if none
*/
public static Class getCollectionFieldType(Field collectionField) {
return getGenericFieldType(collectionField, Collection.class, 0, 1);
}
/**
* Determine the generic element type of the given Collection field.
* @param collectionField the collection field to introspect
* @param 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)
* @return the generic type, or <code>null</code> if none
*/
public static Class getCollectionFieldType(Field collectionField, int nestingLevel) {
return getGenericFieldType(collectionField, Collection.class, 0, nestingLevel);
}
/**
* Determine the generic key type of the given Map field.
* @param mapField the map field to introspect
* @return the generic type, or <code>null</code> if none
*/
public static Class getMapKeyFieldType(Field mapField) {
return getGenericFieldType(mapField, Map.class, 0, 1);
}
/**
* Determine the generic key type of the given Map field.
* @param mapField the map field to introspect
* @param 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)
* @return the generic type, or <code>null</code> if none
*/
public static Class getMapKeyFieldType(Field mapField, int nestingLevel) {
return getGenericFieldType(mapField, Map.class, 0, nestingLevel);
}
/**
* Determine the generic value type of the given Map field.
* @param mapField the map field to introspect
* @return the generic type, or <code>null</code> if none
*/
public static Class getMapValueFieldType(Field mapField) {
return getGenericFieldType(mapField, Map.class, 1, 1);
}
/**
* Determine the generic value type of the given Map field.
* @param mapField the map field to introspect
* @param 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)
* @return the generic type, or <code>null</code> if none
*/
public static Class getMapValueFieldType(Field mapField, int nestingLevel) {
return getGenericFieldType(mapField, Map.class, 1, nestingLevel);
}
/**
* Determine the generic element type of the given Collection parameter.
* @param methodParam the method parameter specification
* @return the generic type, or <code>null</code> if none
*/
public static Class getCollectionParameterType(MethodParameter methodParam) {
return getGenericParameterType(methodParam, Collection.class, 0);
}
/**
* Determine the generic key type of the given Map parameter.
* @param methodParam the method parameter specification
* @return the generic type, or <code>null</code> if none
*/
public static Class getMapKeyParameterType(MethodParameter methodParam) {
return getGenericParameterType(methodParam, Map.class, 0);
}
/**
* Determine the generic value type of the given Map parameter.
* @param methodParam the method parameter specification
* @return the generic type, or <code>null</code> if none
*/
public static Class getMapValueParameterType(MethodParameter methodParam) {
return getGenericParameterType(methodParam, Map.class, 1);
}
/**
* Determine the generic element type of the given Collection return type.
* @param method the method to check the return type for
* @return the generic type, or <code>null</code> if none
*/
public static Class getCollectionReturnType(Method method) {
return getGenericReturnType(method, Collection.class, 0, 1);
}
/**
* Determine the generic element type of the given Collection return type.
* <p>If the specified nesting level is higher than 1, the element type of
* a nested Collection/Map will be analyzed.
* @param method the method to check the return type for
* @param 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)
* @return the generic type, or <code>null</code> if none
*/
public static Class getCollectionReturnType(Method method, int nestingLevel) {
return getGenericReturnType(method, Collection.class, 0, nestingLevel);
}
/**
* Determine the generic key type of the given Map return type.
* @param method the method to check the return type for
* @return the generic type, or <code>null</code> if none
*/
public static Class getMapKeyReturnType(Method method) {
return getGenericReturnType(method, Map.class, 0, 1);
}
/**
* Determine the generic key type of the given Map return type.
* @param method the method to check the return type for
* @param 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)
* @return the generic type, or <code>null</code> if none
*/
public static Class getMapKeyReturnType(Method method, int nestingLevel) {
return getGenericReturnType(method, Map.class, 0, nestingLevel);
}
/**
* Determine the generic value type of the given Map return type.
* @param method the method to check the return type for
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?