Fix #4753
This commit is contained in:
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user