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" for Constable[] and Enum[] #4902

Closed
andygoossens opened this issue Mar 28, 2024 · 1 comment · Fixed by #4933
Closed

[java] "Bad intersection, unrelated class types" for Constable[] and Enum[] #4902

andygoossens opened this issue Mar 28, 2024 · 1 comment · Fixed by #4933
Assignees
Labels
a:bug PMD crashes or fails to analyse a file. in:type-resolution Affects the type resolution code
Milestone

Comments

@andygoossens
Copy link

Affects PMD Version:

7.0.0

Description:

PMD does not properly handle code that combines values from 2 enums which share the same interface.

Exception Stacktrace:

org.apache.commons.lang3.exception.ContextedRuntimeException: java.lang.IllegalArgumentException: Bad intersection, unrelated class types java.lang.constant.Constable[] and java.lang.Enum[] in [java.lang.Enum[], java.lang.constant.Constable[], BadIntersection$Animal[]]
Exception Context:
        [1:Resolving type of=!debug only! [MethodCall:22:12]Stream.of(
            Bird.values(),
            Fish.values()
        )]
        [2:Rule applied on node=!debug only! [MethodCall:22:12]Stream.of(
            Bird.values(),
            Fish.values()
        )
        .flatMap(Arrays::stream)]
---------------------------------
        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.types.TypeTestUtil.isA(TypeTestUtil.java:65)
        at net.sourceforge.pmd.lang.java.rule.bestpractices.UseCollectionIsEmptyRule.visit(UseCollectionIsEmptyRule.java:29)
        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 java.base@17.0.10/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:539)
        at java.base@17.0.10/java.util.concurrent.FutureTask.run(FutureTask.java:264)
        at java.base@17.0.10/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
        at java.base@17.0.10/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
        at java.base@17.0.10/java.lang.Thread.run(Thread.java:840)
Caused by: java.lang.IllegalArgumentException: Bad intersection, unrelated class types java.lang.constant.Constable[] and java.lang.Enum[] in [java.lang.Enum[], java.lang.constant.Constable[], BadIntersection$Animal[]]
        at net.sourceforge.pmd.lang.java.types.Lub.glb(Lub.java:337)
        at net.sourceforge.pmd.lang.java.types.TypeSystem.glb(TypeSystem.java:715)
        at net.sourceforge.pmd.lang.java.types.Lub$LubJudge.lub(Lub.java:164)
        at net.sourceforge.pmd.lang.java.types.Lub.lub(Lub.java:35)
        at net.sourceforge.pmd.lang.java.types.TypeSystem.lub(TypeSystem.java:680)
        at net.sourceforge.pmd.lang.java.types.internal.infer.ReductionStep$2.solve(ReductionStep.java:41)
        at net.sourceforge.pmd.lang.java.types.internal.infer.InferenceContext.solveBatchProgressed(InferenceContext.java:444)
        at net.sourceforge.pmd.lang.java.types.internal.infer.InferenceContext.solve(InferenceContext.java:424)
        at net.sourceforge.pmd.lang.java.types.internal.infer.InferenceContext.solve(InferenceContext.java:400)
        at net.sourceforge.pmd.lang.java.types.internal.infer.Infer.instantiateImpl(Infer.java:576)
        at net.sourceforge.pmd.lang.java.types.internal.infer.Infer.instantiateMaybeNoInfer(Infer.java:541)
        at net.sourceforge.pmd.lang.java.types.internal.infer.Infer.instantiateMethod(Infer.java:385)
        at net.sourceforge.pmd.lang.java.types.internal.infer.Infer.instantiateMethodOrCtor(Infer.java:365)
        at net.sourceforge.pmd.lang.java.types.internal.infer.Infer.logInference(Infer.java:348)
        at net.sourceforge.pmd.lang.java.types.internal.infer.Infer.computeCompileTimeDecl(Infer.java:288)
        at net.sourceforge.pmd.lang.java.types.internal.infer.Infer.getCompileTimeDecl(Infer.java:250)
        at net.sourceforge.pmd.lang.java.types.internal.infer.Infer.goToInvocationWithFallback(Infer.java:183)
        at net.sourceforge.pmd.lang.java.types.internal.infer.Infer.inferInvocationRecursively(Infer.java:172)
        at net.sourceforge.pmd.lang.java.types.ast.internal.PolyResolution.inferInvocation(PolyResolution.java:254)
        at net.sourceforge.pmd.lang.java.types.ast.internal.PolyResolution.polyTypeOtherCtx(PolyResolution.java:126)
        at net.sourceforge.pmd.lang.java.types.ast.internal.PolyResolution.computePolyType(PolyResolution.java:116)
        at net.sourceforge.pmd.lang.java.types.ast.internal.LazyTypeResolver.handlePoly(LazyTypeResolver.java:267)
        at net.sourceforge.pmd.lang.java.types.ast.internal.LazyTypeResolver.visit(LazyTypeResolver.java:272)
        at net.sourceforge.pmd.lang.java.types.ast.internal.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)
        ... 19 more
Exception occurred on node !debug only! [MethodCall:22:12]Stream.of(
            Bird.values(),
            Fish.values()
        )
        .flatMap(Arrays::stream)

Code Sample demonstrating the issue:

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class BadIntersection {

  interface Animal {
  }

  enum Bird implements Animal {
    PARROT,
    CHICKEN
  }

  enum Fish implements Animal {
    GOLDFISH,
    MACKEREL
  }

  private static List<Animal> combineAnimals() {
    return Stream.of(
            Bird.values(),
            Fish.values()
        )
        .flatMap(Arrays::stream)
        .collect(Collectors.toList());
  }

  public static void main(String[] args) {
    List<Animal> animals = combineAnimals();
    for (Animal animal : animals) {
      System.out.println("animal = " + animal);
    }
  }
}

Steps to reproduce:

  1. Copy the code sample above.
  2. As I was using Gradle 8.5, I configured PMD this way:
  pmd {
    toolVersion = '7.0.0'
  }

  dependencies {
    pmd 'net.sourceforge.pmd:pmd-ant:7.0.0'
    pmd 'net.sourceforge.pmd:pmd-java:7.0.0'
  }
  1. Run ./gradlew pmdMain

Running PMD through: Gradle

Happens on OpenJDK versions 11, 17, and 21.

@andygoossens andygoossens added the a:bug PMD crashes or fails to analyse a file. label Mar 28, 2024
@oowekyala
Copy link
Member

Interesting, thanks for the report

@jsotuyod jsotuyod added the in:type-resolution Affects the type resolution code label Mar 29, 2024
@jsotuyod jsotuyod added this to the 7.0.1 milestone Mar 29, 2024
@jsotuyod jsotuyod added the needs:pmd7-revalidation The issue hasn't yet been retested vs PMD 7 and may be stale label Apr 2, 2024
@oowekyala oowekyala self-assigned this Apr 5, 2024
oowekyala added a commit to oowekyala/pmd that referenced this issue Apr 5, 2024
@oowekyala oowekyala removed the needs:pmd7-revalidation The issue hasn't yet been retested vs PMD 7 and may be stale label Apr 6, 2024
jsotuyod added a commit that referenced this issue Apr 7, 2024
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
Development

Successfully merging a pull request may close this issue.

3 participants