This commit is contained in:
Clément Fournier
2023-11-28 17:48:59 +01:00
parent f030d7f8d7
commit b7b26f01f5
2 changed files with 40 additions and 0 deletions

View File

@ -144,6 +144,13 @@ public final class TypeConversion {
* @return The capture conversion of t
*/
public static JTypeMirror capture(JTypeMirror t) {
if (t instanceof JTypeVar) {
// Need to capture the upper bound because that bound contributes the methods of the tvar.
// Eg in `<C extends Collection<? super X>>` the methods available in C may mention the type
// parameter of collection. But this is a wildcard `? super X` that needs to be captured.
JTypeVar tv = (JTypeVar) t;
return tv.withUpperBound(capture(tv.getUpperBound()));
}
return t instanceof JClassType ? capture((JClassType) t) : t;
}

View File

@ -458,6 +458,39 @@ class Foo {
}
}
parserTest("PMD crashes while using generics and wildcards #4753") {
val (acu, spy) = parser.parseWithTypeInferenceSpy(
"""
import java.util.Collection;
import java.util.List;
import java.util.Arrays;
public class SubClass<T> {
public <C extends Collection<? super T>> C into(C collection) {
List<T> list = null;
collection.addAll(list);
return collection;
}
}
""".trimIndent()
)
val cvar = acu.typeVar("C")
val tvar = acu.typeVar("T")
spy.shouldBeOk {
val call = acu.firstMethodCall()
call.methodType.shouldMatchMethod(
named = "addAll",
withFormals = listOf(java.util.Collection::class[`?` extends captureMatcher(`?` `super` tvar)]),
returning = boolean
)
call.overloadSelectionInfo::isFailed shouldBe false
}
}
})