Skip to content

Commit 41393d4

Browse files
committed
[java] AccessorClassGeneration - handle default ctors
Fixes #5106
1 parent 5d39923 commit 41393d4

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

docs/pages/release_notes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ This is a {{ site.pmd.release_type }} release.
3939
* apex-performance
4040
* [#635](https://github.com/pmd/pmd/issues/635): \[apex] New Rule: Avoid soql/sosl queries without a where clause or limit statement
4141
* java-bestpractices
42+
* [#5106](https://github.com/pmd/pmd/issues/5106): \[java] AccessorClassGeneration: Node was null for default constructor
4243
* [#5110](https://github.com/pmd/pmd/issues/5110): \[java] UnusedPrivateMethod for method referenced by lombok.Builder.ObtainVia
4344
* [#5117](https://github.com/pmd/pmd/issues/5117): \[java] UnusedPrivateMethod for methods annotated with jakarta.annotation.PostConstruct or PreDestroy
4445
* java-errorprone

pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/AccessorMethodGenerationRule.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRulechainRule;
1818
import net.sourceforge.pmd.lang.java.symbols.JAccessibleElementSymbol;
1919
import net.sourceforge.pmd.lang.java.symbols.JClassSymbol;
20+
import net.sourceforge.pmd.lang.java.symbols.JConstructorSymbol;
2021
import net.sourceforge.pmd.lang.java.symbols.JFieldSymbol;
2122
import net.sourceforge.pmd.lang.java.symbols.JMethodSymbol;
2223
import net.sourceforge.pmd.lang.java.symbols.JVariableSymbol;
@@ -74,6 +75,10 @@ static void checkMemberAccess(RuleContext ruleContext, JavaNode refExpr, JAccess
7475
refExpr.getEnclosingType().getSymbol())) {
7576

7677
JavaNode node = sym.tryGetNode();
78+
if (node == null && JConstructorSymbol.CTOR_NAME.equals(sym.getSimpleName())) {
79+
// might be a default constructor, implicitly defined and not explicitly in the compilation unit
80+
node = sym.getEnclosingClass().tryGetNode();
81+
}
7782
assert node != null : "Node should be in the same compilation unit";
7883
if (reportedNodes.add(node)) {
7984
ruleContext.addViolation(node, stripPackageName(refExpr.getEnclosingType().getSymbol()));

pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/bestpractices/xml/AccessorClassGeneration.xml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<test-code>
88
<description>inner class has private constructor</description>
99
<expected-problems>1</expected-problems>
10+
<expected-linenumbers>3</expected-linenumbers>
1011
<code><![CDATA[
1112
public class Foo1 {
1213
public class InnerClass {
@@ -191,4 +192,20 @@ public class Foo {
191192
]]></code>
192193
<source-type>java 10</source-type>
193194
</test-code>
195+
196+
<test-code>
197+
<description>#5106 [java] AccessorClassGeneration: Node was null for default constructor</description>
198+
<expected-problems>1</expected-problems>
199+
<expected-linenumbers>2</expected-linenumbers>
200+
<code><![CDATA[
201+
public class Foo1 {
202+
private static class InnerClass {
203+
}
204+
void method() {
205+
new InnerClass(); //Causes generation of accessor
206+
}
207+
}
208+
]]></code>
209+
<source-type>java 10</source-type>
210+
</test-code>
194211
</test-data>

0 commit comments

Comments
 (0)