Skip to content

[java] UnnecessaryCast: ContextedRuntimeException when parsing switch expression with lambdas #6237

Closed

Description

@Ledmington

Affects PMD Version: 7.18.0

Rule: UnnecessaryCast

Description:
When launching pmd, it crashes with a ContextedRuntimeException, caused by a ClassCastException, when applying the rule UnnecessaryCast. By reading through the error message, it seems to me that the problem is that I am using a switch expression where the function expects a lambda.

Exception Stacktrace:

...
Exception applying rule UnnecessaryCast on file /home/fbarbari/prova/emu/core/src/main/java/com/ledmington/emu/X86Cpu.java, continuing with next rule
org.apache.commons.lang3.exception.ContextedRuntimeException: java.lang.ClassCastException: class net.sourceforge.pmd.lang.java.ast.ASTSwitchExpression cannot be cast to class net.sourceforge.pmd.lang.java.ast.InvocationNode (net.sourceforge.pmd.lang.java.ast.ASTSwitchExpression and net.sourceforge.pmd.lang.java.ast.InvocationNode are in unnamed module of loader org.gradle.internal.classloader.VisitableURLClassLoader @5fe469d4)
Exception Context:
        [1:Rule applied on node=!debug only! [CastExpression:574:23](long) imm.asInt()]
---------------------------------
        at net.sourceforge.pmd.util.AssertionUtil.contexted(AssertionUtil.java:257)
        at net.sourceforge.pmd.lang.rule.internal.RuleApplicator.applyOnIndex(RuleApplicator.java:79)
        at net.sourceforge.pmd.lang.rule.internal.RuleApplicator.apply(RuleApplicator.java:57)
        at net.sourceforge.pmd.lang.rule.internal.RuleSets.apply(RuleSets.java:183)
        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)
Caused by: java.lang.ClassCastException: class net.sourceforge.pmd.lang.java.ast.ASTSwitchExpression cannot be cast to class net.sourceforge.pmd.lang.java.ast.InvocationNode (net.sourceforge.pmd.lang.java.ast.ASTSwitchExpression and net.sourceforge.pmd.lang.java.ast.InvocationNode are in unnamed module of loader org.gradle.internal.classloader.VisitableURLClassLoader @5fe469d4)
        at net.sourceforge.pmd.lang.java.rule.codestyle.UnnecessaryCastRule.isCastDeterminingReturnOfLambda(UnnecessaryCastRule.java:281)
        at net.sourceforge.pmd.lang.java.rule.codestyle.UnnecessaryCastRule.isCastUnnecessary(UnnecessaryCastRule.java:143)
        at net.sourceforge.pmd.lang.java.rule.codestyle.UnnecessaryCastRule.visit(UnnecessaryCastRule.java:111)
        at net.sourceforge.pmd.lang.java.ast.ASTCastExpression.acceptVisitor(ASTCastExpression.java:33)
        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:413)
        at net.sourceforge.pmd.lang.rule.internal.RuleApplicator.applyOnIndex(RuleApplicator.java:77)
        ... 9 more
Exception occurred on node !debug only! [CastExpression:574:23](long) imm.asInt()
Exception applying rule UnnecessaryCast on file /home/fbarbari/prova/emu/core/src/main/java/com/ledmington/emu/X86Cpu.java, continuing with next rule
org.apache.commons.lang3.exception.ContextedRuntimeException: java.lang.ClassCastException: class net.sourceforge.pmd.lang.java.ast.ASTSwitchExpression cannot be cast to class net.sourceforge.pmd.lang.java.ast.InvocationNode (net.sourceforge.pmd.lang.java.ast.ASTSwitchExpression and net.sourceforge.pmd.lang.java.ast.InvocationNode are in unnamed module of loader org.gradle.internal.classloader.VisitableURLClassLoader @5fe469d4)
Exception Context:
        [1:Rule applied on node=!debug only! [CastExpression:573:22](long) imm.asByte()]
---------------------------------
        at net.sourceforge.pmd.util.AssertionUtil.contexted(AssertionUtil.java:257)
        at net.sourceforge.pmd.lang.rule.internal.RuleApplicator.applyOnIndex(RuleApplicator.java:79)
        at net.sourceforge.pmd.lang.rule.internal.RuleApplicator.apply(RuleApplicator.java:57)
        at net.sourceforge.pmd.lang.rule.internal.RuleSets.apply(RuleSets.java:183)
        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)
Caused by: java.lang.ClassCastException: class net.sourceforge.pmd.lang.java.ast.ASTSwitchExpression cannot be cast to class net.sourceforge.pmd.lang.java.ast.InvocationNode (net.sourceforge.pmd.lang.java.ast.ASTSwitchExpression and net.sourceforge.pmd.lang.java.ast.InvocationNode are in unnamed module of loader org.gradle.internal.classloader.VisitableURLClassLoader @5fe469d4)
        at net.sourceforge.pmd.lang.java.rule.codestyle.UnnecessaryCastRule.isCastDeterminingReturnOfLambda(UnnecessaryCastRule.java:281)
        at net.sourceforge.pmd.lang.java.rule.codestyle.UnnecessaryCastRule.isCastUnnecessary(UnnecessaryCastRule.java:143)
        at net.sourceforge.pmd.lang.java.rule.codestyle.UnnecessaryCastRule.visit(UnnecessaryCastRule.java:111)
        at net.sourceforge.pmd.lang.java.ast.ASTCastExpression.acceptVisitor(ASTCastExpression.java:33)
        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:413)
        at net.sourceforge.pmd.lang.rule.internal.RuleApplicator.applyOnIndex(RuleApplicator.java:77)
        ... 9 more
Exception occurred on node !debug only! [CastExpression:573:22](long) imm.asByte()

> Task :core:pmdMain FAILED
/home/fbarbari/prova/emu/core/src/main/java/com/ledmington/emu/X86Cpu.java      -       ContextedRuntimeException: java.lang.ClassCastException: class net.sourceforge.pmd.lang.java.ast.ASTSwitchExpression cannot be cast to class net.sourceforge.pmd.lang.java.ast.InvocationNode (net.sourceforge.pmd.lang.java.ast.ASTSwitchExpression and net.sourceforge.pmd.lang.java.ast.InvocationNode are in unnamed module of loader org.gradle.internal.classloader.VisitableURLClassLoader @5fe469d4)
Exception Context:
        [1:Rule applied on node=!debug only! [CastExpression:574:23](long) imm.asInt()]
---------------------------------
/home/fbarbari/prova/emu/core/src/main/java/com/ledmington/emu/X86Cpu.java      -       ContextedRuntimeException: java.lang.ClassCastException: class net.sourceforge.pmd.lang.java.ast.ASTSwitchExpression cannot be cast to class net.sourceforge.pmd.lang.java.ast.InvocationNode (net.sourceforge.pmd.lang.java.ast.ASTSwitchExpression and net.sourceforge.pmd.lang.java.ast.InvocationNode are in unnamed module of loader org.gradle.internal.classloader.VisitableURLClassLoader @5fe469d4)
Exception Context:
        [1:Rule applied on node=!debug only! [CastExpression:573:22](long) imm.asByte()]
---------------------------------

Code Sample demonstrating the issue:
This is the problematic part, in particular the switch returning lambdas (at core/src/main/java/com/ledmington/emu/X86Cpu.java, line 568).

private void opSX(final Register64 op1, final Immediate imm, final BiFunction<Long, Long, Long> task) {
	op(
			() -> rf.get(op1),
			// FIXME: ugly
			switch (imm.bits()) {
				case 8 -> () -> (long) imm.asByte();
				case 32 -> () -> (long) imm.asInt();
				default -> throw new IllegalArgumentException(String.format("Unknown immediate: %s.", imm));
			},
			task,
			result -> rf.set(op1, result),
			true);
}

Steps to reproduce:

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

  1. git clone https://github.com/Ledmington/emu.git
  2. cd emu
  3. git checkout 9ec3ef3f4982f7e779e0e8ea1812fb034a4919e1
  4. ./gradlew clean :core:pmdMain

Running PMD through: Gradle

Activity

  1. added
    a:bug
    on Nov 17, 2025
  2. changed the title [-][java][/-] [+][java] `ContextedRuntimeException` when parsing switch expression with lambdas[/+] on Nov 17, 2025
  3. self-assigned this
    on Nov 17, 2025
  4. Ledmington commented on Nov 18, 2025

    @Ledmington
    Author

    If it may be of any help, I tried changing that switch into this (a lambda with a switch instead of a switch returning lambdas):

    ...
    () -> switch (imm.bits()) {
    	case 8 -> (long) imm.asByte();
    	case 32 -> (long) imm.asInt();
    	default -> throw new IllegalArgumentException(String.format("Unknown immediate: %s.", imm));
    },
    ...

    and the error, meaning PMD crashing, went away. It still complained about the unnecessary casts.

  5. Ledmington commented on Nov 30, 2025

    @Ledmington
    Author

    Minor double update.

    1. This bug affects also 7.19.0.

    2. While trying to mess around with the PMD codebase to open a PR, I found a smaller reproducer:

    import java.util.function.Supplier;
    public class Foo {
        public void bar(int x) {
            foo(
                switch(x) {
                    case 0 -> () -> (long) 1;
                    case 1 -> () -> (long) 2;
                    default -> throw new Error();
                }
            );
        }
    
        public void foo(Supplier<Long> sup) {
            // intentionally empty
        }
    }
  6. oowekyala commented on Nov 30, 2025

    @oowekyala
    Member

    Thank you for the complete reproducer. I'm working on it

  7. added this to the 7.20.0 milestone on Dec 1, 2025
  8. added a commit that references this issue on Dec 1, 2025
    5fb6417
  9. changed the title [-][java] `ContextedRuntimeException` when parsing switch expression with lambdas[/-] [+][java] ContextedRuntimeException when parsing switch expression with lambdas[/+] on Dec 21, 2025
  10. changed the title [-][java] ContextedRuntimeException when parsing switch expression with lambdas[/-] [+][java] UnnecessaryCast: ContextedRuntimeException when parsing switch expression with lambdas[/+] on Dec 21, 2025
  11. added 2 commits that reference this issue on Dec 21, 2025
  12. Ledmington commented on Jan 2, 2026

    @Ledmington
    Author

    Thank you very much for the fix @adangel and @oowekyala. Happy new Year!

  13. added a commit that references this issue on Feb 10, 2026
  14. added a commit that references this issue on Nov 11, 2026
    84ed9dc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

a:bug

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions