GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing github.com — [java] Fix #7214: `ClassWithOnlyPrivateConstructorsShouldBeFinal`: consider Lombok private constructors by Pankraz76 · Pull Request #7231 · pmd/pmd Conversation
|
|
Compared to main: (comment created at 2026-10-24 10:56:48+00:00 for f3c1fd4) |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
… private constructors (pmd#7214) Lombok's @NoArgsConstructor, @requiredargsconstructor and @AllArgsConstructor are now desugared into constructor symbols in AstClassSym#processLombok, alongside the existing @Getter and @slf4j support. The access level is honored (NONE generates nothing, MODULE is package-private), staticName and enums force a private constructor, and the parameters follow Lombok's field selection rules. When Lombok generates a constructor, the implicit default constructor is dropped. Lombok desugaring now also visits nested types at any depth, as well as local and anonymous classes. Before, only top-level types and their direct member types were processed. ClassWithOnlyPrivateConstructorsShouldBeFinal now simply checks that all constructors of the class symbol are private, replacing its own Lombok annotation parsing. Classes annotated with @UtilityClass (made final by Lombok) or @SuperBuilder (which generates a protected constructor) are skipped. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_016vn4ZWXKBMVjr52oRtWTTA
4b74116 to
f3c1fd4
Compare
|
@UncleOwen Thanks for the pointer, the constructors are now modelled in One change worth a second look: |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
ClassWithOnlyPrivateConstructorsShouldBeFinal: consider Lombok private constructors (#7214)ClassWithOnlyPrivateConstructorsShouldBeFinal: consider Lombok private constructors
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR! Modelling the Lombok constructors as symbols is much better than the AST-based check in the rule.
I checked the PR against #7214: the issue's exact code is now reported (@NoArgsConstructor(access = AccessLevel.PRIVATE) → the implicit public default ctor is replaced by a private one, so all ctors are private). The new rule tests and LombokTest pass locally, as does a full ./mvnw -pl pmd-java verify.
0. Please update the PR with main first
I merged #7198 (@Builder support) into main yesterday, which touched the same code in AstClassSym (processLombok, getLombokAccessModifier) and SymbolTableResolver#desugarLombokMembers. I already merged main locally before the review, so please update your branch as well. I'm going through the backlog of PRs one by one.
1. Move the Lombok logic out of AstClassSym (requested)
With #7198 and this PR, AstClassSym now contains a lot of Lombok-specific code: getters, @Slf4j, @Builder, and now constructor generation, field selection for @RequiredArgsConstructor/@AllArgsConstructor, non-null annotation detection, staticName and access level parsing. #7242 already proposes moving this into a package-private helper LombokUtil in symbols/internal/ast, and the two PRs now conflict in AstClassSym and ImplicitMemberSymbols.
Could you please move the new constructor handling (processLombokConstructors, addLombokConstructor, hasLombokStaticName, lombokConstructorFields, hasNonNullAnnotation, LOMBOK_NON_NULL_SIMPLE_NAMES, and getLombokAccessModifier) into such a helper, ideally the same LombokUtil as in #7242? That way AstClassSym#processLombok only delegates to it. Whichever PR is merged second then only needs a simple rebase.
2. Behaviour with the lombok language property disabled
Before this PR, hasPublicLombokConstructors worked regardless of the lombok language property. Now the rule only looks at the symbol's constructors, so with lombok=false a class like this becomes a false positive (I verified this locally):
@AllArgsConstructor public class Foo { int x; private Foo() { } }
I think this is the right trade-off and we shouldn't keep an AST-based fallback. The rule description already says the Lombok support requires the lombok property. Please make the consequence explicit, e.g.:
If the property is disabled, constructors generated by Lombok are not seen, and the rule might report false positives for classes that only have private explicit constructors but non-private Lombok-generated ones.
3. Please move the crossFindBoundaries() change to a separate PR
The change in SymbolTableResolver#desugarLombokMembers looks like a real bug fix, but it isn't needed to fix #7214 itself, and it changes behaviour for all Lombok features (@Getter, @Slf4j, @Builder in nested, local and anonymous classes), not just this rule. A separate PR would let us look at the implications on their own (e.g. the extra symbol table rebuild and disambiguation retry for every local and anonymous class), add its own release notes entry, and add tests outside this rule (e.g. LombokTest cases with @Getter on a doubly nested class and on a local class). The test case "Lombok private constructors in nested and local classes" would then move to that PR too.
4. Nits
LOMBOK_NON_NULL_SIMPLE_NAMES is declared between the instance fields (at least how I merged it...). Please move it to the other constants (or into the helper from 1.).getLombokAccessModifier: the comment // special value for NONE, we'll use -1 as a flag or handle it is outdated now that both callers handle -1.Not blocking / follow-ups, as you already mentioned: constructors implied by @Data/@Value/@Builder/@SuperBuilder, the staticName factory method, and fields made final by @Value/@FieldDefaults(makeFinal = true) (these change the @RequiredArgsConstructor parameters). Doing these in a separate PR is fine.
🤖 Review assisted by Claude Code
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| .filter(it -> TypeTestUtil.isA("lombok.NoArgsConstructor", it) | ||
| || TypeTestUtil.isA("lombok.RequiredArgsConstructor", it) | ||
| || TypeTestUtil.isA("lombok.AllArgsConstructor", it)) | ||
| .any(it -> it.getFlatValue("access").filterIs(ASTNamedReferenceExpr.class).none(ref -> "PRIVATE".equals(ref.getName()))); |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I'm wondering, why this line didn't work out? It looks like it handles the case from #7214 explicitly...
Anyway, moving the lombok impl to the symbol table code is a better solution moving forward.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| * java-design | ||
| * [#4815](https://github.com/pmd/pmd/issues/4815): \[java] ExceptionAsFlowControl false-positive on Lambda/asynchronous (7.0.0-rc4) | ||
| * [#7117](https://github.com/pmd/pmd/issues/7117): \[java] ExceptionAsFlowControl: false negative when the lambda is invoked by the method it is passed to | ||
| * [#7214](https://github.com/pmd/pmd/issues/7214): \[java] ClassWithOnlyPrivateConstructorsShouldBeFinal: false negative when the private no-arg constructor is produced by Lombok @<!-- -->NoArgsConstructor(access = AccessLevel.PRIVATE) |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I've simplified the issue title a bit:
| * [#7214](https://github.com/pmd/pmd/issues/7214): \[java] ClassWithOnlyPrivateConstructorsShouldBeFinal: false negative when the private no-arg constructor is produced by Lombok @<!-- -->NoArgsConstructor(access = AccessLevel.PRIVATE) | |
| * [#7214](https://github.com/pmd/pmd/issues/7214): \[java] ClassWithOnlyPrivateConstructorsShouldBeFinal: False negative for Lombok generated private constructor |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
|
||
| /** | ||
| * Returns the fields that are parameters of a lombok-generated constructor. | ||
| * Static fields and fields whose name starts with {@code $} are never included. |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Fields that start with "$"?
Hm... that's not explicitly documented here: https://projectlombok.org/features/constructor
The behavior is described here: https://projectlombok.org/features/experimental/FieldDefaults but that's a different annotation...
Can you verify this?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| private static int count; | ||
| private final String title; | ||
| private final int edition = 1; | ||
| @NonNull private String author; |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
What does this @NonNull annotation do on a field declaration? This is use case is not described on https://projectlombok.org/features/NonNull .
Should we use here one of the other annotations instead? (see LOMBOK_NON_NULL_SIMPLE_NAMES - whatever their fully qualified names are)
Update: Ok, the use case is explained on https://projectlombok.org/features/constructor ...
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
[java] ClassWithOnlyPrivateConstructorsShouldBeFinal: False negative for Lombok generated private constructor
There was an error while loading. Please reload this page.