Skip to content

[java] PMD crashes while using generics and wildcards #4753

@soyo114

Description

@soyo114

Affects PMD Version:7.0.0-rc4

Description:

I have encountered a crash while using generics and wildcards in the provided code snippet. The usage of these features seems to trigger an unexpected behavior, resulting in an exception when analyzed by PMD.

Code Sample demonstrating the issue:

public class SubClass<T> {
   public <C extends Collection<? super T>> C into(C collection) {
       List<T> list = Arrays.asList((T[]) toArray());
       collection.addAll(list);
       return collection;
   }
}

Steps to reproduce:

Please provide detailed steps for how we can reproduce the bug.

  1. pmd check -d SubClass.java -R category/java/bestpractices.xml -f text

Running PMD through: [CLI]

Exception Stacktrace:

org.apache.commons.lang3.exception.ContextedRuntimeException: java.lang.IllegalArgumentException: <? super T> cannot be a wildcard bound
Exception Context:
        [1:Resolving type of=!debug only! [MethodCall:11:9]collection.addAll(list)]
        [2:Rule applied on node=!debug only! [MethodCall:11:9]collection.addAll(list)]
---------------------------------
        at net.sourceforge.pmd.util.AssertionUtil.contexted(AssertionUtil.java:232)
        at net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode.getTypeMirror(AbstractJavaTypeNode.java:54)
        at net.sourceforge.pmd.lang.java.ast.ASTMethodCall.getTypeMirror(ASTMethodCall.java:22)
        at net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode.getTypeMirror(AbstractJavaTypeNode.java:39)
        at net.sourceforge.pmd.lang.java.ast.ASTMethodCall.getTypeMirror(ASTMethodCall.java:22)
        at net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode.forceTypeResolution(AbstractJavaTypeNode.java:29)
        at net.sourceforge.pmd.lang.java.ast.AbstractInvocationExpr.getOverloadSelectionInfo(AbstractInvocationExpr.java:27)
        at net.sourceforge.pmd.lang.java.ast.ASTMethodCall.getOverloadSelectionInfo(ASTMethodCall.java:22)
        at net.sourceforge.pmd.lang.java.ast.InvocationNode.getMethodType(InvocationNode.java:48)
        at net.sourceforge.pmd.lang.java.rule.internal.TestFrameworksUtil.isCallOnAssertionContainer(TestFrameworksUtil.java:166)
        at net.sourceforge.pmd.lang.java.rule.bestpractices.JUnitAssertionsShouldIncludeMessageRule.visit(JUnitAssertionsShouldIncludeMessageRule.java:37)
        at net.sourceforge.pmd.lang.java.ast.ASTMethodCall.acceptVisitor(ASTMethodCall.java:75)
        at net.sourceforge.pmd.lang.java.ast.AbstractJavaNode.acceptVisitor(AbstractJavaNode.java:38)
        at net.sourceforge.pmd.lang.java.rule.AbstractJavaRule.apply(AbstractJavaRule.java:24)
        at net.sourceforge.pmd.lang.rule.internal.RuleApplicator.applyOnIndex(RuleApplicator.java:75)
        at net.sourceforge.pmd.lang.rule.internal.RuleApplicator.apply(RuleApplicator.java:55)
        at net.sourceforge.pmd.RuleSets.apply(RuleSets.java:158)
        at net.sourceforge.pmd.lang.impl.PmdRunnable.processSource(PmdRunnable.java:140)
        at net.sourceforge.pmd.lang.impl.PmdRunnable.run(PmdRunnable.java:80)
        at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
        at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
        at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
        at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
        at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.IllegalArgumentException: <? super T> cannot be a wildcard bound
        at net.sourceforge.pmd.lang.java.types.TypeSystem.wildcard(TypeSystem.java:640)
        at net.sourceforge.pmd.lang.java.types.WildcardTypeImpl.subst(WildcardTypeImpl.java:36)
        at net.sourceforge.pmd.lang.java.types.WildcardTypeImpl.subst(WildcardTypeImpl.java:17)
        at net.sourceforge.pmd.lang.java.types.TypeOps.lambda$subst$0(TypeOps.java:926)
        at net.sourceforge.pmd.lang.java.types.TypeOps.mapPreservingSelf(TypeOps.java:953)
        at net.sourceforge.pmd.lang.java.types.TypeOps.subst(TypeOps.java:926)
        at net.sourceforge.pmd.lang.java.types.JClassType.subst(JClassType.java:76)
        at net.sourceforge.pmd.lang.java.types.JClassType.subst(JClassType.java:52)
        at net.sourceforge.pmd.lang.java.types.TypeOps.lambda$subst$0(TypeOps.java:926)
        at net.sourceforge.pmd.lang.java.types.TypeOps.mapPreservingSelf(TypeOps.java:953)
        at net.sourceforge.pmd.lang.java.types.TypeOps.subst(TypeOps.java:926)
        at net.sourceforge.pmd.lang.java.symbols.internal.asm.ExecutableStub.getFormalParameterTypes(ExecutableStub.java:101)
        at net.sourceforge.pmd.lang.java.types.ClassMethodSigImpl.getFormalParameters(ClassMethodSigImpl.java:115)
        at net.sourceforge.pmd.lang.java.types.internal.infer.Infer.isPotentiallyApplicable(Infer.java:973)
        at net.sourceforge.pmd.lang.java.types.internal.infer.Infer.computeCompileTimeDecl(Infer.java:272)
        at net.sourceforge.pmd.lang.java.types.internal.infer.Infer.getCompileTimeDecl(Infer.java:249)
        at net.sourceforge.pmd.lang.java.types.internal.infer.Infer.goToInvocationWithFallback(Infer.java:182)
        at net.sourceforge.pmd.lang.java.types.internal.infer.Infer.inferInvocationRecursively(Infer.java:171)
        at net.sourceforge.pmd.lang.java.types.ast.PolyResolution.inferInvocation(PolyResolution.java:251)
        at net.sourceforge.pmd.lang.java.types.ast.PolyResolution.polyTypeOtherCtx(PolyResolution.java:123)
        at net.sourceforge.pmd.lang.java.types.ast.PolyResolution.computePolyType(PolyResolution.java:113)
        at net.sourceforge.pmd.lang.java.types.ast.LazyTypeResolver.handlePoly(LazyTypeResolver.java:268)
        at net.sourceforge.pmd.lang.java.types.ast.LazyTypeResolver.visit(LazyTypeResolver.java:273)
        at net.sourceforge.pmd.lang.java.types.ast.LazyTypeResolver.visit(LazyTypeResolver.java:95)
        at net.sourceforge.pmd.lang.java.ast.ASTMethodCall.acceptVisitor(ASTMethodCall.java:75)
        at net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode.getTypeMirror(AbstractJavaTypeNode.java:51)
        ... 22 more

Metadata

Metadata

Assignees

Labels

a:bugPMD crashes or fails to analyse a file.

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions