Repository navigation
Closed
Description
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.
git clone https://github.com/Ledmington/emu.gitcd emugit checkout 9ec3ef3f4982f7e779e0e8ea1812fb034a4919e1./gradlew clean :core:pmdMain
Running PMD through: Gradle
Activity
[-][java][/-][+][java] `ContextedRuntimeException` when parsing switch expression with lambdas[/+]Ledmington commentedon Nov 18, 2025
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):
and the error, meaning PMD crashing, went away. It still complained about the unnecessary casts.
Ledmington commentedon Nov 30, 2025
Minor double update.
This bug affects also 7.19.0.
While trying to mess around with the PMD codebase to open a PR, I found a smaller reproducer:
oowekyala commentedon Nov 30, 2025
Thank you for the complete reproducer. I'm working on it
[java] Fix pmd#6237 - error in UnnecessaryCast rule
[-][java] `ContextedRuntimeException` when parsing switch expression with lambdas[/-][+][java] ContextedRuntimeException when parsing switch expression with lambdas[/+][-][java] ContextedRuntimeException when parsing switch expression with lambdas[/-][+][java] UnnecessaryCast: ContextedRuntimeException when parsing switch expression with lambdas[/+][doc] Update release notes (#6237, #6295)
[java] Fix #6237: UnnecessaryCast error with switch expr returning la…
Ledmington commentedon Jan 2, 2026
Thank you very much for the fix @adangel and @oowekyala. Happy new Year!
RM-37163: CPD 7.21.0 upgrade (#8)
[java] Fix #6237: Deprecate AddEmptyString without replacement (#7311)