Skip to content

Commit

Permalink
Support variable resolution of wildcard types
Browse files Browse the repository at this point in the history
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
  • Loading branch information
philwebb authored and jhoeller committed Dec 6, 2019
1 parent a4fa6a7 commit 7c84695
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
Expand Up @@ -873,6 +873,12 @@ private ResolvableType resolveVariable(TypeVariable<?> variable) {
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);
}
Expand Down
Expand Up @@ -688,6 +688,13 @@ void resolveBoundedTypeVariableResult() throws Exception {
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"));
Expand Down Expand Up @@ -1417,6 +1424,8 @@ interface Methods<T> {

<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);
Expand Down

0 comments on commit 7c84695

Please sign in to comment.