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] Bad intersection, unrelated class types java.lang.Object[] and java.lang.Number #4980

Closed
boris-petrov opened this issue Apr 26, 2024 · 1 comment · Fixed by #4994
Closed
Assignees
Labels
a:bug PMD crashes or fails to analyse a file. in:type-resolution Affects the type resolution code
Milestone

Comments

@boris-petrov
Copy link
Contributor

Affects PMD Version:

7.1.0.

Description:

This code:

public <T extends Number> T foo() {
	return null;
}

public Stream<Number> bar() {
	return Stream.of(foo());
}

Leads to:

Exception Stacktrace:

org.apache.commons.lang3.exception.ContextedRuntimeException: java.lang.IllegalArgumentException: Bad intersection, unrelated class types java.lang.Object[] and java.lang.Number in [java.lang.Number, java.lang.Object[]]
Exception Context:
        [1:Resolving type of=!debug only! [MethodCall:81:10]Stream.of(foo())]
        [2:Rule applied on node=!debug only! [MethodCall:81:10]Stream.of(foo())]
---------------------------------
        at net.sourceforge.pmd.util.AssertionUtil.contexted(AssertionUtil.java:236)
        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:30)
        at net.sourceforge.pmd.lang.rule.RuleReference.apply(RuleReference.java:415)
        at net.sourceforge.pmd.lang.rule.internal.RuleApplicator.applyOnIndex(RuleApplicator.java:77)
        at net.sourceforge.pmd.lang.rule.internal.RuleApplicator.apply(RuleApplicator.java:57)
        at net.sourceforge.pmd.lang.rule.internal.RuleSets.apply(RuleSets.java:155)
        at net.sourceforge.pmd.lang.impl.PmdRunnable.processSource(PmdRunnable.java:140)
        at net.sourceforge.pmd.lang.impl.PmdRunnable.run(PmdRunnable.java:80)
        at [email protected]/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:572)
        at [email protected]/java.util.concurrent.FutureTask.run(FutureTask.java:317)
        at [email protected]/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1144)
        at [email protected]/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:642)
        at [email protected]/java.lang.Thread.run(Thread.java:1583)

Code Sample demonstrating the issue:

See above.

Steps to reproduce:

Run PMD on that code.

Running PMD through: [CLI | Ant | Maven | Gradle | Designer | Other]

Gradle.

@boris-petrov boris-petrov added the a:bug PMD crashes or fails to analyse a file. label Apr 26, 2024
@boris-petrov boris-petrov changed the title [java] Bad intersection, unr elated class types java.lang.Object[] and java.lang.Number [java] Bad intersection, unrelated class types java.lang.Object[] and java.lang.Number Apr 26, 2024
@jsotuyod jsotuyod added this to the 7.2.0 milestone Apr 27, 2024
@jsotuyod jsotuyod self-assigned this Apr 27, 2024
jsotuyod added a commit to Monits/pmd that referenced this issue Apr 27, 2024
jsotuyod added a commit to Monits/pmd that referenced this issue Apr 27, 2024
 - When an applicability test fails (ie: during LUB) we don't want that
   to bubble up and fail the process, simply to discard the candidate
   and move forward. If no matching candidate is found, the inference
   will fail anyway.
@jsotuyod jsotuyod added in:type-resolution Affects the type resolution code has:pr labels Apr 27, 2024
@jsotuyod
Copy link
Member

@boris-petrov thanks for pointing this one out. It's effectively a bug, I'm able to reproduce it and have a basic fix in place. Still some work to do, but should be ok for 7.2.0

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. in:type-resolution Affects the type resolution code
Projects
None yet
3 participants