Browse Source

Support variable resolution of wildcard types

Update `ResolvableType` so that variable referenced can be resolved
against wildcard types. Prior to this commit, given a type:

	Map<String, ? extends List<? extends CharSequence>>

Calling `type.getGeneric(1).asCollection().resolveGeneric()` would
return `null`. This was because the `List` variable `E` referenced a
wildcard type which `resolveVariable` did not support.

Closes gh-24145
pull/24155/head
Phillip Webb 5 years ago committed by Juergen Hoeller
parent
commit
7c84695333
  1. 6
      spring-core/src/main/java/org/springframework/core/ResolvableType.java
  2. 9
      spring-core/src/test/java/org/springframework/core/ResolvableTypeTests.java

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

@ -873,6 +873,12 @@ public class ResolvableType implements Serializable { @@ -873,6 +873,12 @@ public class ResolvableType implements Serializable {
return forType(ownerType, this.variableResolver).resolveVariable(variable);
}
}
if (this.type instanceof WildcardType) {
ResolvableType resolved = resolveType().resolveVariable(variable);
if (resolved != null) {
return resolved;
}
}
if (this.variableResolver != null) {
return this.variableResolver.resolveVariable(variable);
}

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

@ -688,6 +688,13 @@ class ResolvableTypeTests { @@ -688,6 +688,13 @@ class ResolvableTypeTests {
assertThat(type.resolve()).isEqualTo(CharSequence.class);
}
@Test
void resolveBoundedTypeVariableWildcardResult() throws Exception {
ResolvableType type = ResolvableType.forMethodReturnType(Methods.class.getMethod("boundedTypeVaraibleWildcardResult"));
assertThat(type.getGeneric(1).asCollection().resolveGeneric()).isEqualTo(CharSequence.class);
}
@Test
void resolveVariableNotFound() throws Exception {
ResolvableType type = ResolvableType.forMethodReturnType(Methods.class.getMethod("typedReturn"));
@ -1417,6 +1424,8 @@ class ResolvableTypeTests { @@ -1417,6 +1424,8 @@ class ResolvableTypeTests {
<R extends CharSequence & Serializable> R boundedTypeVaraibleResult();
Map<String, ? extends List<? extends CharSequence>> boundedTypeVaraibleWildcardResult();
void nested(Map<Map<String, Integer>, Map<Byte, Long>> p);
void typedParameter(T p);

Loading…
Cancel
Save