diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/util/ClassTypeInformation.java b/spring-data-commons-core/src/main/java/org/springframework/data/util/ClassTypeInformation.java index a66412696..773f00ff0 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/util/ClassTypeInformation.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/util/ClassTypeInformation.java @@ -28,6 +28,7 @@ import java.util.Map; import java.util.Set; import java.util.WeakHashMap; +import org.springframework.core.GenericTypeResolver; import org.springframework.util.Assert; /** diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/util/GenericTypeResolver.java b/spring-data-commons-core/src/main/java/org/springframework/data/util/GenericTypeResolver.java deleted file mode 100644 index 20e5edd8e..000000000 --- a/spring-data-commons-core/src/main/java/org/springframework/data/util/GenericTypeResolver.java +++ /dev/null @@ -1,325 +0,0 @@ -/* - * Copyright 2002-2010 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.data.util; - -import java.lang.ref.Reference; -import java.lang.ref.WeakReference; -import java.lang.reflect.Array; -import java.lang.reflect.GenericArrayType; -import java.lang.reflect.ParameterizedType; -import java.lang.reflect.Type; -import java.lang.reflect.TypeVariable; -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; -import java.util.WeakHashMap; - -import org.springframework.core.MethodParameter; -import org.springframework.util.Assert; - -/** - * Copy of Spring's {@link org.springframework.core.GenericTypeResolver}. Needed until - * {@link #getTypeVariableMap(Class)} gets public. - *
- * TODO: remove that class, as soon as Spring 3.0.6 gets released. - * - * @see SPR-8005 - */ -@SuppressWarnings({ "rawtypes", "unchecked" }) -abstract class GenericTypeResolver { - - /** - * Cache from Class to TypeVariable Map - */ - private static final Mapnull if not resolvable
- */
- public static Class> resolveTypeArgument(Class clazz, Class genericIfc) {
- Class[] typeArgs = resolveTypeArguments(clazz, genericIfc);
- if (typeArgs == null) {
- return null;
- }
- if (typeArgs.length != 1) {
- throw new IllegalArgumentException("Expected 1 type argument on generic interface [" + genericIfc.getName()
- + "] but found " + typeArgs.length);
- }
- return typeArgs[0];
- }
-
- /**
- * Resolve the type arguments of the given generic interface against the given target class which is assumed to
- * implement the generic interface and possibly declare concrete types for its type variables.
- *
- * @param clazz the target class to check against
- * @param genericIfc the generic interface or superclass to resolve the type argument from
- * @return the resolved type of each argument, with the array size matching the number of actual type arguments, or
- * null if not resolvable
- */
- public static Class[] resolveTypeArguments(Class clazz, Class genericIfc) {
- return doResolveTypeArguments(clazz, clazz, genericIfc);
- }
-
- private static Class[] doResolveTypeArguments(Class ownerClass, Class classToIntrospect, Class genericIfc) {
- while (classToIntrospect != null) {
- if (genericIfc.isInterface()) {
- Type[] ifcs = classToIntrospect.getGenericInterfaces();
- for (Type ifc : ifcs) {
- Class[] result = doResolveTypeArguments(ownerClass, ifc, genericIfc);
- if (result != null) {
- return result;
- }
- }
- } else {
- Class[] result = doResolveTypeArguments(ownerClass, classToIntrospect.getGenericSuperclass(), genericIfc);
- if (result != null) {
- return result;
- }
- }
- classToIntrospect = classToIntrospect.getSuperclass();
- }
- return null;
- }
-
- private static Class[] doResolveTypeArguments(Class ownerClass, Type ifc, Class genericIfc) {
- if (ifc instanceof ParameterizedType) {
- ParameterizedType paramIfc = (ParameterizedType) ifc;
- Type rawType = paramIfc.getRawType();
- if (genericIfc.equals(rawType)) {
- Type[] typeArgs = paramIfc.getActualTypeArguments();
- Class[] result = new Class[typeArgs.length];
- for (int i = 0; i < typeArgs.length; i++) {
- Type arg = typeArgs[i];
- result[i] = extractClass(ownerClass, arg);
- }
- return result;
- } else if (genericIfc.isAssignableFrom((Class) rawType)) {
- return doResolveTypeArguments(ownerClass, (Class) rawType, genericIfc);
- }
- } else if (genericIfc.isAssignableFrom((Class) ifc)) {
- return doResolveTypeArguments(ownerClass, (Class) ifc, genericIfc);
- }
- return null;
- }
-
- /**
- * Extract a class instance from given Type.
- */
- private static Class extractClass(Class ownerClass, Type arg) {
- if (arg instanceof ParameterizedType) {
- return extractClass(ownerClass, ((ParameterizedType) arg).getRawType());
- } else if (arg instanceof GenericArrayType) {
- GenericArrayType gat = (GenericArrayType) arg;
- Type gt = gat.getGenericComponentType();
- Class> componentClass = extractClass(ownerClass, gt);
- return Array.newInstance(componentClass, 0).getClass();
- } else if (arg instanceof TypeVariable) {
- TypeVariable tv = (TypeVariable) arg;
- arg = getTypeVariableMap(ownerClass).get(tv);
- if (arg == null) {
- arg = extractBoundForTypeVariable(tv);
- } else {
- arg = extractClass(ownerClass, arg);
- }
- }
- return (arg instanceof Class ? (Class) arg : Object.class);
- }
-
- /**
- * Resolve the specified generic type against the given TypeVariable map.
- *
- * @param genericType the generic type to resolve
- * @param typeVariableMap the TypeVariable Map to resolved against
- * @return the type if it resolves to a Class, or Object.class otherwise
- */
- static Class resolveType(Type genericType, Map