Skip to content

Commit 7c84695

Browse files
philwebbjhoeller
authored andcommitted
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
1 parent a4fa6a7 commit 7c84695

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -873,6 +873,12 @@ private ResolvableType resolveVariable(TypeVariable<?> variable) {
873873
return forType(ownerType, this.variableResolver).resolveVariable(variable);
874874
}
875875
}
876+
if (this.type instanceof WildcardType) {
877+
ResolvableType resolved = resolveType().resolveVariable(variable);
878+
if (resolved != null) {
879+
return resolved;
880+
}
881+
}
876882
if (this.variableResolver != null) {
877883
return this.variableResolver.resolveVariable(variable);
878884
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,13 @@ void resolveBoundedTypeVariableResult() throws Exception {
688688
assertThat(type.resolve()).isEqualTo(CharSequence.class);
689689
}
690690

691+
692+
@Test
693+
void resolveBoundedTypeVariableWildcardResult() throws Exception {
694+
ResolvableType type = ResolvableType.forMethodReturnType(Methods.class.getMethod("boundedTypeVaraibleWildcardResult"));
695+
assertThat(type.getGeneric(1).asCollection().resolveGeneric()).isEqualTo(CharSequence.class);
696+
}
697+
691698
@Test
692699
void resolveVariableNotFound() throws Exception {
693700
ResolvableType type = ResolvableType.forMethodReturnType(Methods.class.getMethod("typedReturn"));
@@ -1417,6 +1424,8 @@ interface Methods<T> {
14171424

14181425
<R extends CharSequence & Serializable> R boundedTypeVaraibleResult();
14191426

1427+
Map<String, ? extends List<? extends CharSequence>> boundedTypeVaraibleWildcardResult();
1428+
14201429
void nested(Map<Map<String, Integer>, Map<Byte, Long>> p);
14211430

14221431
void typedParameter(T p);

0 commit comments

Comments
 (0)