Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
soyodream opened this issue Nov 27, 2023 · 0 comments · Fixed by #4755
Closed

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

soyodream opened this issue Nov 27, 2023 · 0 comments · Fixed by #4755
Assignees
Labels
a:bug PMD crashes or fails to analyse a file.
Milestone

Comments

@soyodream
Copy link

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
@soyodream soyodream added the a:bug PMD crashes or fails to analyse a file. label Nov 27, 2023
@oowekyala oowekyala self-assigned this Nov 28, 2023
@adangel adangel changed the title PMD crashes while using generics and wildcards [java] PMD crashes while using generics and wildcards Dec 1, 2023
@adangel adangel added this to the 7.0.0 milestone Dec 1, 2023
@adangel adangel closed this as completed in b7b26f0 Dec 1, 2023
adangel added a commit that referenced this issue Dec 1, 2023
adangel added a commit that referenced this issue Dec 1, 2023
[java] Fix #4753 - PMD crashes while using generics and wildcards #4755
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a:bug PMD crashes or fails to analyse a file.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants