GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [java] UnnecessaryCast: ContextedRuntimeException when parsing switch expression with lambdas · Issue #6237 · pmd/pmd

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

New issue
Copy link
New issue
Copy link
Closed
#6295
Closed
[java] UnnecessaryCast: ContextedRuntimeException when parsing switch expression with lambdas#6237
#6295
Copy link
Assignees
oowekyala
Labels
a:bugPMD crashes or fails to analyse a file.
Milestone
7.20.0

Description

@Ledmington
Ledmington
opened on Nov 17, 2025
Issue body actions

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. Ledmington
    added
    a:bugPMD crashes or fails to analyse a file.
    on Nov 17, 2025
  2. Ledmington
    changed the title [-][java][/-] [+][java] `ContextedRuntimeException` when parsing switch expression with lambdas[/+] on Nov 17, 2025
  3. oowekyala
    self-assigned this
    on Nov 17, 2025
  4. Ledmington commented on Nov 18, 2025

    @Ledmington
    Ledmington
    on Nov 18, 2025
    Author
    More actions

    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
    Ledmington
    on Nov 30, 2025
    Author
    More actions

    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
        }
    }
    Reacted by Clément Fournier
  6. oowekyala commented on Nov 30, 2025

    @oowekyala
    oowekyala
    on Nov 30, 2025
    Member
    More actions

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

    Reacted by Filippo Barbari
  7. oowekyala
    added this to the 7.20.0 milestone on Dec 1, 2025
  8. oowekyala
    added a commit that references this issue on Dec 1, 2025

    [java] Fix pmd#6237 - error in UnnecessaryCast rule

    This issue will close once commit 5fb6417 is merged into the 'main' branch.
    5fb6417
  9. adangel
    changed the title [-][java] `ContextedRuntimeException` when parsing switch expression with lambdas[/-] [+][java] ContextedRuntimeException when parsing switch expression with lambdas[/+] on Dec 21, 2025
  10. adangel
    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. adangel
    closed this as completedin 5fb6417on Dec 21, 2025
  12. adangel
    added 2 commits that reference this issue on Dec 21, 2025

    [doc] Update release notes (#6237, #6295)

    e53d1c8

    [java] Fix #6237: UnnecessaryCast error with switch expr returning la…

    678ac9f
  13. Ledmington commented on Jan 2, 2026

    @Ledmington
    Ledmington
    on Jan 2, 2026
    Author
    More actions

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

    Reacted by Clément Fournier
    Reacted by Andreas Dangel
  14. wener-tiobe
    added a commit that references this issue on Feb 10, 2026

    RM-37163: CPD 7.21.0 upgrade (#8)

    6a1959a
  15. UncleOwen
    added a commit that references this issue on Nov 11, 2026

    [java] Fix #6237: Deprecate AddEmptyString without replacement (#7311)

    This issue will close once commit 84ed9dc is merged into the 'main' branch.
    84ed9dc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @oowekyala
    oowekyala

Labels

a:bugPMD crashes or fails to analyse a file.

Type

No type

Projects

No projects

    Milestone

    • 7.20.0

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402775 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.