Skip to content

[java] Fix #6731: Support lombok.Builder (fix UnnecessaryFullyQualifiedName false positive) - #7198

Merged
adangel merged 3 commits into
pmd:mainfrom
markjoin123:cursor/fix-ufqn-lombok-builder-7c90
Nov 12, 2026
Merged

adangel merged 3 commits into
pmd:mainfrom
markjoin123:cursor/fix-ufqn-lombok-builder-7c90

Conversation

@markjoin123

@markjoin123 markjoin123 commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Describe the PR

UnnecessaryFullyQualifiedName reports @lombok.Builder and @lombok.Builder.Default when lombok.Builder is already in scope. That is wrong when builderClassName is "Builder": Lombok generates a nested class of that name, so the simple name Builder no longer refers to the annotation. Since Lombok 1.18.48, @Builder(builderClassName = "Builder") is a compile error and the fully qualified annotation is required. The same clash makes @lombok.Builder.Default necessary inside the type.

Other builder class names are still reported.

This PR adds support for Lombok's Builder annotation.

Related issues

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)

@oowekyala oowekyala left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for the PR

@cursor
cursor Bot force-pushed the cursor/fix-ufqn-lombok-builder-7c90 branch from 14047df to da2e150 Compare October 26, 2026 02:02
@markjoin123

Copy link
Copy Markdown
Contributor Author

Thanks for the review @oowekyala, that makes sense. I reworked the PR accordingly:

  • The special case in UnnecessaryFullyQualifiedNameRule is gone; the rule itself is unchanged.
  • @Builder (on the type, or on a constructor/method) now adds a static nested class symbol to the enclosing AstClassSym, like the other Lombok-generated members from [java] Better Lombok support #3119. The name is <TypeName>Builder by default, or builderClassName when set (* is replaced by the type name). Nothing is added if a nested class with that name already exists.
  • The generated class is visible in the type body and in the scope of the type's own annotations, so @lombok.Builder is no longer reported as an unnecessary qualification when the generated class is named Builder.

Tests: the #6731 cases in UnnecessaryFullyQualifiedName.xml still cover the false positive, and LombokTest now checks the symbol directly (default name, *Builder, constructor and method builders, an existing nested class, Lombok support disabled). UnnecessaryFullyQualifiedNameTest, LombokTest, SymbolTableResolverTest, AstSymbolTests and AstSymbolResolverTest all pass, and checkstyle is clean. The branch is rebased onto current main.

@adangel adangel left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for the PR!

I think we should address the following points (see also the individual comments):

  • Add a comment in SymbolTableResolver explaining why the generated builder class is added to the scope of the class's own annotations. Java scoping doesn't do this; the reason is a Lombok restriction. Since Lombok 1.18.48, @Builder(builderClassName = "Builder") must be written as @lombok.Builder. Also consider moving this block into a separate private method (e.g. addBuildersToAnnotationScope), so that desugarLombokMembers stays easy to read.
  • Fix the generated builder class name for @Builder on methods. Lombok names it after the method's return type, not the enclosing type (e.g. Bar$FooBuilder for static Foo create() in Bar). Please add a test for this.
  • Update the release notes: please remove the "New and Changed Rules" section. This is a bug fix, so the entry under "Fixed Issues" is enough.

Comment thread docs/pages/release_notes.md Resolved
Comment thread docs/pages/release_notes.md Resolved
@adangel adangel changed the title [java] Fix #6731: UnnecessaryFullyQualifiedName false positive for lombok Builder [java] Fix #6731: Support lombok.Builder (fix UnnecessaryFullyQualifiedName false positive) Nov 6, 2026
@adangel adangel added the needs:user-input Maintainers are waiting for feedback from author label Nov 6, 2026
@cursor
cursor Bot force-pushed the cursor/fix-ufqn-lombok-builder-7c90 branch 2 times, most recently from fdec473 to e0760e4 Compare November 10, 2026 01:21
UnnecessaryFullyQualifiedName reported lombok.Builder when the generated
builder class was named Builder. Add that static nested class to the
enclosing type's symbol, as with other Lombok members, instead of
special-casing the rule. The default name is <TypeName>Builder; builderClassName
overrides it, and an existing nested class of that name is kept.

Fixes pmd#6731
Address review feedback. Explain the Lombok annotation-scope restriction,
return the synthetic builder classes that were added, and name @builder
on a method after its return type (Desk$BookBuilder for static Book book()).
@cursor
cursor Bot force-pushed the cursor/fix-ufqn-lombok-builder-7c90 branch from e0760e4 to 6a3fe12 Compare November 12, 2026 00:07
@adangel adangel removed the needs:user-input Maintainers are waiting for feedback from author label Nov 12, 2026
@adangel adangel added this to the 7.30.0 milestone Nov 12, 2026
@pmd-actions-helper

pmd-actions-helper Bot commented Nov 12, 2026 •

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-11-12 12:27:07+00:00 for b550479)

@adangel adangel left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks!

@adangel adangel self-assigned this Nov 12, 2026
@adangel
adangel merged commit 01ea262 into pmd:main Nov 12, 2026
2 checks passed
Pankraz76 pushed a commit to Pankraz76/pmd_fork_tmp that referenced this pull request Nov 13, 2026
Resolve the conflicts with pmd#7198, which added the (empty) builder class
of lombok.Builder. Build on that implementation instead of duplicating it:

- keep the builder class of pmd#7198, including builders of constructors
  and methods, the '*' wildcard of builderClassName and the annotation scope
- add the members for @builder on a non-generic class: the methods of the
  builder class, builder() and toBuilder()
- honor the access attribute for the builder class
- a handwritten builder class gets the methods it does not declare

Builders of generic classes, constructors and methods stay empty classes.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
Claude-Session: https://claude.ai/code/session_01MTr23wWTVcrESgpWPAS9iR
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[java] UnnecessaryFullyQualifiedName: False positive for lombok.Builder annotation

3 participants