Skip to content

Commit

Permalink
[java] AccessorClassGeneration - handle default ctors
Browse files Browse the repository at this point in the history
Fixes #5106
  • Loading branch information
adangel committed Jul 25, 2024
1 parent 5d39923 commit 41393d4
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/pages/release_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ This is a {{ site.pmd.release_type }} release.
* apex-performance
* [#635](https://github.com/pmd/pmd/issues/635): \[apex] New Rule: Avoid soql/sosl queries without a where clause or limit statement
* java-bestpractices
* [#5106](https://github.com/pmd/pmd/issues/5106): \[java] AccessorClassGeneration: Node was null for default constructor
* [#5110](https://github.com/pmd/pmd/issues/5110): \[java] UnusedPrivateMethod for method referenced by lombok.Builder.ObtainVia
* [#5117](https://github.com/pmd/pmd/issues/5117): \[java] UnusedPrivateMethod for methods annotated with jakarta.annotation.PostConstruct or PreDestroy
* java-errorprone
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRulechainRule;
import net.sourceforge.pmd.lang.java.symbols.JAccessibleElementSymbol;
import net.sourceforge.pmd.lang.java.symbols.JClassSymbol;
import net.sourceforge.pmd.lang.java.symbols.JConstructorSymbol;
import net.sourceforge.pmd.lang.java.symbols.JFieldSymbol;
import net.sourceforge.pmd.lang.java.symbols.JMethodSymbol;
import net.sourceforge.pmd.lang.java.symbols.JVariableSymbol;
Expand Down Expand Up @@ -74,6 +75,10 @@ static void checkMemberAccess(RuleContext ruleContext, JavaNode refExpr, JAccess
refExpr.getEnclosingType().getSymbol())) {

JavaNode node = sym.tryGetNode();
if (node == null && JConstructorSymbol.CTOR_NAME.equals(sym.getSimpleName())) {
// might be a default constructor, implicitly defined and not explicitly in the compilation unit
node = sym.getEnclosingClass().tryGetNode();
}
assert node != null : "Node should be in the same compilation unit";
if (reportedNodes.add(node)) {
ruleContext.addViolation(node, stripPackageName(refExpr.getEnclosingType().getSymbol()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<test-code>
<description>inner class has private constructor</description>
<expected-problems>1</expected-problems>
<expected-linenumbers>3</expected-linenumbers>
<code><![CDATA[
public class Foo1 {
public class InnerClass {
Expand Down Expand Up @@ -191,4 +192,20 @@ public class Foo {
]]></code>
<source-type>java 10</source-type>
</test-code>

<test-code>
<description>#5106 [java] AccessorClassGeneration: Node was null for default constructor</description>
<expected-problems>1</expected-problems>
<expected-linenumbers>2</expected-linenumbers>
<code><![CDATA[
public class Foo1 {
private static class InnerClass {
}
void method() {
new InnerClass(); //Causes generation of accessor
}
}
]]></code>
<source-type>java 10</source-type>
</test-code>
</test-data>

0 comments on commit 41393d4

Please sign in to comment.