Repository navigation
Closed
Description
Affects PMD Version:
7.28.0
Description:
PMD 7.28.0 throws an exception when evaluating the NonThreadSafeSingleton rule against a Java record that null checks its own fields. The issue seems to be here, a record's fields will be under ASTRecordComponent instead of ASTFieldDeclaration.
Exception Stacktrace:
[WARN] Exception applying rule NonThreadSafeSingleton on file /data/code/pmd/repro-7120/Foo.java, continuing with next rule
org.apache.commons.lang3.exception.ContextedRuntimeException: java.util.NoSuchElementException: Empty node stream
Exception Context:
[1:Rule applied on node=!debug only! [MethodDeclaration:2:10]void baz() {
if (field == null) {
field.toString();
}
}]
---------------------------------
at net.sourceforge.pmd.util.AssertionUtil.contexted(AssertionUtil.java:265)
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 java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:572)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:317)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1144)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:642)
at java.base/java.lang.Thread.run(Thread.java:1583)
Caused by: java.util.NoSuchElementException: Empty node stream
at net.sourceforge.pmd.lang.ast.NodeStream.firstOrThrow(NodeStream.java:782)
at net.sourceforge.pmd.lang.java.rule.multithreading.NonThreadSafeSingletonRule.shouldConsiderField(NonThreadSafeSingletonRule.java:66)
at net.sourceforge.pmd.lang.java.rule.multithreading.NonThreadSafeSingletonRule.isNullCheckOnField(NonThreadSafeSingletonRule.java:132)
at net.sourceforge.pmd.lang.java.rule.multithreading.NonThreadSafeSingletonRule.visit(NonThreadSafeSingletonRule.java:79)
at net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration.acceptVisitor(ASTMethodDeclaration.java:57)
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.internal.RuleApplicator.applyOnIndex(RuleApplicator.java:77)
... 9 more
[WARN] Exception occurred on node !debug only! [MethodDeclaration:2:10]void baz() {
if (field == null) {
field.toString();
}
}
Foo.java - ContextedRuntimeException: java.util.NoSuchElementException: Empty node stream
Exception Context:
[1:Rule applied on node=!debug only! [MethodDeclaration:2:10]void baz() {
if (field == null) {
field.toString();
}
}]
---------------------------------
Code Sample demonstrating the issue:
record Foo(Bar field) {
void baz() {
if (field == null) {
field.toString();
}
}
}Steps to reproduce:
Run PMD with NonThreadSafeSingleton enabled against a Java record that checks if a field is null using == null in any method.
Running PMD through:
Gradle / CLI
Reacted by Gili Tzabari
Activity
[-][java] NPE thrown when evaluating NonThreadSafeSingleton on a record that null checks its own fields[/-][+][java] NoSuchElementException thrown when evaluating NonThreadSafeSingleton on a record that null checks its own fields[/+][-][java] NoSuchElementException thrown when evaluating NonThreadSafeSingleton on a record that null checks its own fields[/-][+][java] NonThreadSafeSingleton: throws NoSuchElementException on a record that null checks its own fields[/+][java] Fix pmd#7120: NonThreadSafeSingleton NoSuchElementException (p…
build(deps): align PMD modules at 7.28.0
NoSuchElementException: Empty node stream inNonThreadSafeSingletonrule #7295 as a duplicate of this issue