Browse Source
Dropping GenericCollectionTypeResolver in favor of direct ResolvableType usage. Issue: SPR-15160pull/1261/merge
13 changed files with 279 additions and 523 deletions
@ -1,274 +0,0 @@
@@ -1,274 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2013 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.Field; |
||||
import java.lang.reflect.Method; |
||||
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). |
||||
* |
||||
* @author Juergen Hoeller |
||||
* @author Phillip Webb |
||||
* @since 2.0 |
||||
* @see ResolvableType |
||||
*/ |
||||
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} if none |
||||
*/ |
||||
@SuppressWarnings("rawtypes") |
||||
public static Class<?> getCollectionType(Class<? extends Collection> collectionClass) { |
||||
return ResolvableType.forClass(collectionClass).asCollection().resolveGeneric(); |
||||
} |
||||
|
||||
/** |
||||
* 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} if none |
||||
*/ |
||||
@SuppressWarnings("rawtypes") |
||||
public static Class<?> getMapKeyType(Class<? extends Map> mapClass) { |
||||
return ResolvableType.forClass(mapClass).asMap().resolveGeneric(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} if none |
||||
*/ |
||||
@SuppressWarnings("rawtypes") |
||||
public static Class<?> getMapValueType(Class<? extends Map> mapClass) { |
||||
return ResolvableType.forClass(mapClass).asMap().resolveGeneric(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} if none |
||||
*/ |
||||
public static Class<?> getCollectionFieldType(Field collectionField) { |
||||
return ResolvableType.forField(collectionField).asCollection().resolveGeneric(); |
||||
} |
||||
|
||||
/** |
||||
* 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} if none |
||||
*/ |
||||
public static Class<?> getCollectionFieldType(Field collectionField, int nestingLevel) { |
||||
return ResolvableType.forField(collectionField).getNested(nestingLevel).asCollection().resolveGeneric(); |
||||
} |
||||
|
||||
/** |
||||
* 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) |
||||
* @param typeIndexesPerLevel Map keyed by nesting level, with each value |
||||
* expressing the type index for traversal at that level |
||||
* @return the generic type, or {@code null} if none |
||||
* @deprecated as of 4.0, in favor of using {@link ResolvableType} for arbitrary nesting levels |
||||
*/ |
||||
@Deprecated |
||||
public static Class<?> getCollectionFieldType(Field collectionField, int nestingLevel, Map<Integer, Integer> typeIndexesPerLevel) { |
||||
return ResolvableType.forField(collectionField).getNested(nestingLevel, typeIndexesPerLevel).asCollection().resolveGeneric(); |
||||
} |
||||
|
||||
/** |
||||
* Determine the generic key type of the given Map field. |
||||
* @param mapField the map field to introspect |
||||
* @return the generic type, or {@code null} if none |
||||
*/ |
||||
public static Class<?> getMapKeyFieldType(Field mapField) { |
||||
return ResolvableType.forField(mapField).asMap().resolveGeneric(0); |
||||
} |
||||
|
||||
/** |
||||
* 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} if none |
||||
*/ |
||||
public static Class<?> getMapKeyFieldType(Field mapField, int nestingLevel) { |
||||
return ResolvableType.forField(mapField).getNested(nestingLevel).asMap().resolveGeneric(0); |
||||
} |
||||
|
||||
/** |
||||
* 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) |
||||
* @param typeIndexesPerLevel Map keyed by nesting level, with each value |
||||
* expressing the type index for traversal at that level |
||||
* @return the generic type, or {@code null} if none |
||||
* @deprecated as of 4.0, in favor of using {@link ResolvableType} for arbitrary nesting levels |
||||
*/ |
||||
@Deprecated |
||||
public static Class<?> getMapKeyFieldType(Field mapField, int nestingLevel, Map<Integer, Integer> typeIndexesPerLevel) { |
||||
return ResolvableType.forField(mapField).getNested(nestingLevel, typeIndexesPerLevel).asMap().resolveGeneric(0); |
||||
} |
||||
|
||||
/** |
||||
* Determine the generic value type of the given Map field. |
||||
* @param mapField the map field to introspect |
||||
* @return the generic type, or {@code null} if none |
||||
*/ |
||||
public static Class<?> getMapValueFieldType(Field mapField) { |
||||
return ResolvableType.forField(mapField).asMap().resolveGeneric(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} if none |
||||
*/ |
||||
public static Class<?> getMapValueFieldType(Field mapField, int nestingLevel) { |
||||
return ResolvableType.forField(mapField).getNested(nestingLevel).asMap().resolveGeneric(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) |
||||
* @param typeIndexesPerLevel Map keyed by nesting level, with each value |
||||
* expressing the type index for traversal at that level |
||||
* @return the generic type, or {@code null} if none |
||||
* @deprecated as of 4.0, in favor of using {@link ResolvableType} for arbitrary nesting levels |
||||
*/ |
||||
@Deprecated |
||||
public static Class<?> getMapValueFieldType(Field mapField, int nestingLevel, Map<Integer, Integer> typeIndexesPerLevel) { |
||||
return ResolvableType.forField(mapField).getNested(nestingLevel, typeIndexesPerLevel).asMap().resolveGeneric(1); |
||||
} |
||||
|
||||
/** |
||||
* Determine the generic element type of the given Collection parameter. |
||||
* @param methodParam the method parameter specification |
||||
* @return the generic type, or {@code null} if none |
||||
*/ |
||||
public static Class<?> getCollectionParameterType(MethodParameter methodParam) { |
||||
return ResolvableType.forMethodParameter(methodParam).asCollection().resolveGeneric(); |
||||
} |
||||
|
||||
/** |
||||
* Determine the generic key type of the given Map parameter. |
||||
* @param methodParam the method parameter specification |
||||
* @return the generic type, or {@code null} if none |
||||
*/ |
||||
public static Class<?> getMapKeyParameterType(MethodParameter methodParam) { |
||||
return ResolvableType.forMethodParameter(methodParam).asMap().resolveGeneric(0); |
||||
} |
||||
|
||||
/** |
||||
* Determine the generic value type of the given Map parameter. |
||||
* @param methodParam the method parameter specification |
||||
* @return the generic type, or {@code null} if none |
||||
*/ |
||||
public static Class<?> getMapValueParameterType(MethodParameter methodParam) { |
||||
return ResolvableType.forMethodParameter(methodParam).asMap().resolveGeneric(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} if none |
||||
*/ |
||||
public static Class<?> getCollectionReturnType(Method method) { |
||||
return ResolvableType.forMethodReturnType(method).asCollection().resolveGeneric(); |
||||
} |
||||
|
||||
/** |
||||
* 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} if none |
||||
*/ |
||||
public static Class<?> getCollectionReturnType(Method method, int nestingLevel) { |
||||
return ResolvableType.forMethodReturnType(method).getNested(nestingLevel).asCollection().resolveGeneric(); |
||||
} |
||||
|
||||
/** |
||||
* 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} if none |
||||
*/ |
||||
public static Class<?> getMapKeyReturnType(Method method) { |
||||
return ResolvableType.forMethodReturnType(method).asMap().resolveGeneric(0); |
||||
} |
||||
|
||||
/** |
||||
* 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} if none |
||||
*/ |
||||
public static Class<?> getMapKeyReturnType(Method method, int nestingLevel) { |
||||
return ResolvableType.forMethodReturnType(method).getNested(nestingLevel).asMap().resolveGeneric(0); |
||||
} |
||||
|
||||
/** |
||||
* Determine the generic value type of the given Map return type. |
||||
* @param method the method to check the return type for |
||||
* @return the generic type, or {@code null} if none |
||||
*/ |
||||
public static Class<?> getMapValueReturnType(Method method) { |
||||
return ResolvableType.forMethodReturnType(method).asMap().resolveGeneric(1); |
||||
} |
||||
|
||||
/** |
||||
* Determine the generic value 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} if none |
||||
*/ |
||||
public static Class<?> getMapValueReturnType(Method method, int nestingLevel) { |
||||
return ResolvableType.forMethodReturnType(method).getNested(nestingLevel).asMap().resolveGeneric(1); |
||||
} |
||||
|
||||
} |
@ -1,188 +0,0 @@
@@ -1,188 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2014 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.Method; |
||||
import java.lang.reflect.Type; |
||||
import java.util.AbstractMap; |
||||
import java.util.AbstractSet; |
||||
import java.util.Date; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
import java.util.Set; |
||||
|
||||
import org.junit.Before; |
||||
import org.junit.Test; |
||||
|
||||
import org.springframework.core.io.Resource; |
||||
import org.springframework.tests.sample.objects.GenericObject; |
||||
|
||||
import static org.junit.Assert.*; |
||||
|
||||
/** |
||||
* @author Serge Bogatyrjov |
||||
* @author Juergen Hoeller |
||||
* @author Sam Brannen |
||||
*/ |
||||
public class GenericCollectionTypeResolverTests { |
||||
|
||||
protected Class<?> targetClass; |
||||
|
||||
protected String[] methods; |
||||
|
||||
protected Type[] expectedResults; |
||||
|
||||
@Before |
||||
public void setUp() throws Exception { |
||||
this.targetClass = Foo.class; |
||||
this.methods = new String[] { "a", "b", "b2", "b3", "c", "d", "d2", "d3", "e", |
||||
"e2", "e3" }; |
||||
this.expectedResults = new Class[] { Integer.class, null, Set.class, Set.class, |
||||
null, Integer.class, Integer.class, Integer.class, Integer.class, |
||||
Integer.class, Integer.class }; |
||||
} |
||||
|
||||
protected void executeTest(String methodName) throws NoSuchMethodException { |
||||
for (int i = 0; i < this.methods.length; i++) { |
||||
if (methodName.equals(this.methods[i])) { |
||||
Method method = this.targetClass.getMethod(methodName); |
||||
Type type = getType(method); |
||||
assertEquals(this.expectedResults[i], type); |
||||
return; |
||||
} |
||||
} |
||||
throw new IllegalStateException("Bad test data"); |
||||
} |
||||
|
||||
protected Type getType(Method method) { |
||||
return GenericCollectionTypeResolver.getMapValueReturnType(method); |
||||
} |
||||
|
||||
@Test |
||||
public void a() throws Exception { |
||||
executeTest("a"); |
||||
} |
||||
|
||||
@Test |
||||
public void b() throws Exception { |
||||
executeTest("b"); |
||||
} |
||||
|
||||
@Test |
||||
public void b2() throws Exception { |
||||
executeTest("b2"); |
||||
} |
||||
|
||||
@Test |
||||
public void b3() throws Exception { |
||||
executeTest("b3"); |
||||
} |
||||
|
||||
@Test |
||||
public void c() throws Exception { |
||||
executeTest("c"); |
||||
} |
||||
|
||||
@Test |
||||
public void d() throws Exception { |
||||
executeTest("d"); |
||||
} |
||||
|
||||
@Test |
||||
public void d2() throws Exception { |
||||
executeTest("d2"); |
||||
} |
||||
|
||||
@Test |
||||
public void d3() throws Exception { |
||||
executeTest("d3"); |
||||
} |
||||
|
||||
@Test |
||||
public void e() throws Exception { |
||||
executeTest("e"); |
||||
} |
||||
|
||||
@Test |
||||
public void e2() throws Exception { |
||||
executeTest("e2"); |
||||
} |
||||
|
||||
@Test |
||||
public void e3() throws Exception { |
||||
executeTest("e3"); |
||||
} |
||||
|
||||
@Test |
||||
public void programmaticListIntrospection() throws Exception { |
||||
Method setter = GenericObject.class.getMethod("setResourceList", List.class); |
||||
assertEquals( |
||||
Resource.class, |
||||
GenericCollectionTypeResolver.getCollectionParameterType(new MethodParameter( |
||||
setter, 0))); |
||||
|
||||
Method getter = GenericObject.class.getMethod("getResourceList"); |
||||
assertEquals(Resource.class, |
||||
GenericCollectionTypeResolver.getCollectionReturnType(getter)); |
||||
} |
||||
|
||||
@Test |
||||
public void classResolution() { |
||||
assertEquals(String.class, |
||||
GenericCollectionTypeResolver.getCollectionType(CustomSet.class)); |
||||
assertEquals(String.class, |
||||
GenericCollectionTypeResolver.getMapKeyType(CustomMap.class)); |
||||
assertEquals(Integer.class, |
||||
GenericCollectionTypeResolver.getMapValueType(CustomMap.class)); |
||||
} |
||||
|
||||
private static abstract class CustomSet<T> extends AbstractSet<String> { |
||||
} |
||||
|
||||
private static abstract class CustomMap<T> extends AbstractMap<String, Integer> { |
||||
} |
||||
|
||||
private static abstract class OtherCustomMap<T> implements Map<String, Integer> { |
||||
} |
||||
|
||||
@SuppressWarnings("rawtypes") |
||||
private static interface Foo { |
||||
|
||||
Map<String, Integer> a(); |
||||
|
||||
Map<?, ?> b(); |
||||
|
||||
Map<?, ? extends Set> b2(); |
||||
|
||||
Map<?, ? super Set> b3(); |
||||
|
||||
Map c(); |
||||
|
||||
CustomMap<Date> d(); |
||||
|
||||
CustomMap<?> d2(); |
||||
|
||||
CustomMap d3(); |
||||
|
||||
OtherCustomMap<Date> e(); |
||||
|
||||
OtherCustomMap<?> e2(); |
||||
|
||||
OtherCustomMap e3(); |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue