GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [java] Fix #7214: `ClassWithOnlyPrivateConstructorsShouldBeFinal`: consider Lombok private constructors by Pankraz76 · Pull Request #7231 · pmd/pmd

Conversation

@Pankraz76

@Pankraz76 Pankraz76 commented Oct 23, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Describe the PR

ClassWithOnlyPrivateConstructorsShouldBeFinal missed classes whose only constructor is generated by Lombok:

@NoArgsConstructor(access = AccessLevel.PRIVATE) // now reported, same as `private Foo() {}`
public class Foo {
    public static Foo get() { return null; }
}

Root cause: AstClassSym did not know about Lombok-generated constructors and always added an implicit default constructor with the visibility of the class, i.e. a public no-arg constructor here.

Following the hint in the issue, this PR desugars @NoArgsConstructor, @RequiredArgsConstructor and @AllArgsConstructor into constructor symbols in AstClassSym#processLombok() (next to the existing @Getter / @Slf4j support), via a new ImplicitMemberSymbols#lombokConstructor. The semantics follow Lombok's HandleConstructor:

  • access is honored; AccessLevel.NONE generates nothing, MODULE is package-private
  • staticName = "..." and enums always produce a private constructor (docs: "the generated constructor is always private")
  • parameters: @AllArgsConstructor takes all non-static fields except initialized final ones; @RequiredArgsConstructor takes uninitialized final or @NonNull fields; $-prefixed fields are skipped
  • if 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 (SymbolTableResolver#desugarLombokMembers did not cross find boundaries, so this also fixes @Getter / @Slf4j there).

The rule then simply checks that all constructors of the class symbol are private, so its own Lombok annotation parsing (#4188) is removed. Classes annotated with @lombok.experimental.UtilityClass (made final by Lombok, like @lombok.Value) or @lombok.experimental.SuperBuilder (generates a protected constructor that is not modelled) are skipped. As a side effect, new Foo(a, b) calls to Lombok-generated constructors now resolve in type inference.

Not in scope: the staticName factory method and the constructors implied by @Data / @Value / @Builder / @SuperBuilder are not modelled yet.

Related issues

  • Fixes [java] ClassWithOnlyPrivateConstructorsShouldBeFinal: False negative for Lombok generated private constructor #7214

Ready?

  • Added unit tests for fixed bug/feature
  • Passing all unit tests
  • Complete build ./mvnw clean verify passes (checked automatically by github actions)
  • Added (in-code) documentation (if needed)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@Pankraz76 Pankraz76 changed the title [java] ClassWithOnlyPrivateConstructorsShouldBeFinal: consider Lombok… [java] ClassWithOnlyPrivateConstructorsShouldBeFinal: consider Lombok private constructors (#7214) Oct 23, 2026
@Pankraz76 Pankraz76 mentioned this pull request Oct 23, 2026
[java] ClassWithOnlyPrivateConstructorsShouldBeFinal: False negative for Lombok generated private constructor #7214
Open
@Pankraz76
Pankraz76 force-pushed the fix/7214-lombok-private-ctor-final-v2 branch from e735903 to 4b74116 Compare October 23, 2026 18:30
@Pankraz76
Pankraz76 marked this pull request as ready for review October 23, 2026 18:49
@pmd-actions-helper

pmd-actions-helper Bot commented Oct 23, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Documentation Preview

Compared to main:
This changeset changes 0 violations,
introduces 0 new violations, 0 new errors and 0 new configuration errors,
removes 0 violations, 0 errors and 0 configuration errors.
There are 0 changed duplications, 0 new duplications and 0 removed duplications.
There are 0 changed CPD errors, 0 new CPD errors and 0 removed CPD errors.

Regression Tester Report

(comment created at 2026-10-24 10:56:48+00:00 for f3c1fd4)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@claude
[java] ClassWithOnlyPrivateConstructorsShouldBeFinal: consider Lombok…
f3c1fd4
… 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
@Pankraz76
Pankraz76 force-pushed the fix/7214-lombok-private-ctor-final-v2 branch from 4b74116 to f3c1fd4 Compare October 24, 2026 10:33
@Pankraz76

Pankraz76 commented Oct 24, 2026

Copy link
Copy Markdown
Contributor Author

@UncleOwen Thanks for the pointer, the constructors are now modelled in AstClassSym / ImplicitMemberSymbols, and the rule just checks getConstructors().

One change worth a second look: crossFindBoundaries() in SymbolTableResolver#desugarLombokMembers. It makes Lombok desugaring (including @Getter / @Slf4j) also apply to deeply nested, local and anonymous classes. Happy to split it into a separate PR if you prefer.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Pankraz76 Pankraz76 changed the title [java] ClassWithOnlyPrivateConstructorsShouldBeFinal: consider Lombok private constructors (#7214) [java] Fix #7214: ClassWithOnlyPrivateConstructorsShouldBeFinal: consider Lombok private constructors Oct 26, 2026
@Pankraz76 Pankraz76 mentioned this pull request Oct 26, 2026
[java] Better Lombok support #3119
Open
@UncleOwen UncleOwen mentioned this pull request Nov 10, 2026
[java] Fix #5072: ClassWithOnlyPrivateConstructorsShouldBeFinal and lombok staticName #7254
Closed
3 of 4 tasks
adangel
adangel requested changes Nov 13, 2026
View reviewed changes

@adangel adangel left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for 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.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread ...sourceforge/pmd/lang/java/rule/design/ClassWithOnlyPrivateConstructorsShouldBeFinalRule.java
.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())));

@adangel adangel Nov 13, 2026

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for 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.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread docs/pages/release_notes.md
* 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)

@adangel adangel Nov 13, 2026

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've simplified the issue title a bit:

Suggested change
* [#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.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symbols/internal/ast/AstClassSym.java

/**
* Returns the fields that are parameters of a lombok-generated constructor.
* Static fields and fields whose name starts with {@code $} are never included.

@adangel adangel Nov 13, 2026

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for 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.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/types/internal/infer/LombokTest.kt
private static int count;
private final String title;
private final int edition = 1;
@NonNull private String author;

@adangel adangel Nov 13, 2026

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for 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.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@adangel adangel added the needs:user-input Maintainers are waiting for feedback from author label Nov 13, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@adangel adangel adangel requested changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

needs:user-input Maintainers are waiting for feedback from author

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

[java] ClassWithOnlyPrivateConstructorsShouldBeFinal: False negative for Lombok generated private constructor

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@Pankraz76 @adangel @claude
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.