Skip to content

Commit 53091c7

Browse files
committed
Convenient forType methods for ParameterizedTypeReference
Issue: SPR-16054
1 parent d4677be commit 53091c7

File tree

4 files changed

+72
-11
lines changed

4 files changed

+72
-11
lines changed

spring-core/src/main/java/org/springframework/core/ParameterizedTypeReference.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ protected ParameterizedTypeReference() {
5353
this.type = parameterizedType.getActualTypeArguments()[0];
5454
}
5555

56+
private ParameterizedTypeReference(Type type) {
57+
this.type = type;
58+
}
59+
5660

5761
public Type getType() {
5862
return this.type;
@@ -75,6 +79,19 @@ public String toString() {
7579
}
7680

7781

82+
/**
83+
* Build a {@code ParameterizedTypeReference} wrapping the given type.
84+
* @param type a generic type (possibly obtained via reflection,
85+
* e.g. from {@link java.lang.reflect.Method#getGenericReturnType()})
86+
* @return a corresponding reference which may be passed into
87+
* {@code ParameterizedTypeReference}-accepting methods
88+
* @since 4.3.12
89+
*/
90+
public static <T> ParameterizedTypeReference<T> forType(Type type) {
91+
return new ParameterizedTypeReference<T>(type) {
92+
};
93+
}
94+
7895
private static Class<?> findParameterizedTypeReferenceSubclass(Class<?> child) {
7996
Class<?> parent = child.getSuperclass();
8097
if (Object.class == parent) {

spring-core/src/main/java/org/springframework/core/ResolvableType.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1340,6 +1340,19 @@ public static ResolvableType forType(@Nullable Type type, @Nullable ResolvableTy
13401340
return forType(type, variableResolver);
13411341
}
13421342

1343+
1344+
/**
1345+
* Return a {@link ResolvableType} for the specified {@link ParameterizedTypeReference}.
1346+
* Note: The resulting {@link ResolvableType} may not be {@link Serializable}.
1347+
* @param typeReference the reference to obtain the source type from
1348+
* @return a {@link ResolvableType} for the specified {@link ParameterizedTypeReference}
1349+
* @since 4.3.12
1350+
* @see #forType(Type)
1351+
*/
1352+
public static ResolvableType forType(ParameterizedTypeReference<?> typeReference) {
1353+
return forType(typeReference.getType(), null ,null);
1354+
}
1355+
13431356
/**
13441357
* Return a {@link ResolvableType} for the specified {@link Type} backed by a given
13451358
* {@link VariableResolver}.

spring-core/src/test/java/org/springframework/core/ParameterizedTypeReferenceTests.java

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -33,25 +33,40 @@
3333
public class ParameterizedTypeReferenceTests {
3434

3535
@Test
36-
public void map() throws NoSuchMethodException {
36+
public void stringTypeReference() {
37+
ParameterizedTypeReference<String> typeReference = new ParameterizedTypeReference<String>() {};
38+
assertEquals(String.class, typeReference.getType());
39+
}
40+
41+
@Test
42+
public void mapTypeReference() throws Exception {
3743
Type mapType = getClass().getMethod("mapMethod").getGenericReturnType();
38-
ParameterizedTypeReference<Map<Object,String>> mapTypeReference = new ParameterizedTypeReference<Map<Object,String>>() {};
39-
assertEquals(mapType, mapTypeReference.getType());
44+
ParameterizedTypeReference<Map<Object,String>> typeReference = new ParameterizedTypeReference<Map<Object,String>>() {};
45+
assertEquals(mapType, typeReference.getType());
4046
}
4147

4248
@Test
43-
public void list() throws NoSuchMethodException {
44-
Type mapType = getClass().getMethod("listMethod").getGenericReturnType();
45-
ParameterizedTypeReference<List<String>> mapTypeReference = new ParameterizedTypeReference<List<String>>() {};
46-
assertEquals(mapType, mapTypeReference.getType());
49+
public void listTypeReference() throws Exception {
50+
Type listType = getClass().getMethod("listMethod").getGenericReturnType();
51+
ParameterizedTypeReference<List<String>> typeReference = new ParameterizedTypeReference<List<String>>() {};
52+
assertEquals(listType, typeReference.getType());
4753
}
4854

4955
@Test
50-
public void string() {
51-
ParameterizedTypeReference<String> typeReference = new ParameterizedTypeReference<String>() {};
52-
assertEquals(String.class, typeReference.getType());
56+
public void reflectiveTypeReferenceWithSpecificDeclaration() throws Exception{
57+
Type listType = getClass().getMethod("listMethod").getGenericReturnType();
58+
ParameterizedTypeReference<List<String>> typeReference = ParameterizedTypeReference.forType(listType);
59+
assertEquals(listType, typeReference.getType());
5360
}
5461

62+
@Test
63+
public void reflectiveTypeReferenceWithGenericDeclaration() throws Exception{
64+
Type listType = getClass().getMethod("listMethod").getGenericReturnType();
65+
ParameterizedTypeReference<?> typeReference = ParameterizedTypeReference.forType(listType);
66+
assertEquals(listType, typeReference.getType());
67+
}
68+
69+
5570
public static Map<Object, String> mapMethod() {
5671
return null;
5772
}

spring-core/src/test/java/org/springframework/core/ResolvableTypeTests.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -870,6 +870,22 @@ public void resolveTypeWithCustomVariableResolver() throws Exception {
870870
assertThat(this.typeVariableCaptor.getValue().getName(), equalTo("T"));
871871
}
872872

873+
@Test
874+
public void resolveTypeVariableFromReflectiveParameterizedTypeReference() throws Exception {
875+
Type sourceType = Methods.class.getMethod("typedReturn").getGenericReturnType();
876+
ResolvableType type = ResolvableType.forType(ParameterizedTypeReference.forType(sourceType));
877+
assertThat(type.resolve(), nullValue());
878+
assertThat(type.getType().toString(), equalTo("T"));
879+
}
880+
881+
@Test
882+
public void resolveTypeVariableFromDeclaredParameterizedTypeReference() throws Exception {
883+
Type sourceType = Methods.class.getMethod("charSequenceReturn").getGenericReturnType();
884+
ResolvableType reflectiveType = ResolvableType.forType(sourceType);
885+
ResolvableType declaredType = ResolvableType.forType(new ParameterizedTypeReference<List<CharSequence>>() {});
886+
assertEquals(reflectiveType, declaredType);
887+
}
888+
873889
@Test
874890
public void toStrings() throws Exception {
875891
assertThat(ResolvableType.NONE.toString(), equalTo("?"));

0 commit comments

Comments
 (0)