Skip to content

fix: do not abort generation on reserved-word property names (#418) - #426

Merged
AndreasIgel merged 21 commits into
java-helpers:mainfrom
igel-devin-ai:fix/issue-418-keyword-property-names
Nov 23, 2026
Merged

AndreasIgel merged 21 commits into
java-helpers:mainfrom
igel-devin-ai:fix/issue-418-keyword-property-names

Conversation

@igel-devin-ai

@igel-devin-ai igel-devin-ai commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Closes #418.

setX -> property x can produce a reserved keyword (setStatic -> static). Before this change, that aborted generation for the whole target class.

  • Reserved names: when the setter-derived name is not a legal Java name, BuilderDefinitionCreator takes the property name from a matching getter (isStatic() -> isStatic). Generated methods: isStatic(boolean), or withIsStatic(boolean) with a setter prefix. build() still calls setStatic(...).
  • Deterministic getter choice: JavaLangAnalyser.findGettersForField orders candidates is* -> get* -> accessor-style, so declaration order no longer decides. The builder-side resolveBuilderPropertyName returns Optional and only yields names backed by a real, non-ignored getter.
  • Skip instead of abort: if no getter gives a legal name, only that property is skipped with a warning (an error in strict mode). The rest of the type is still generated.
  • Setter name from the DTO: BuilderFieldDto.getSetterName() returns Optional<String>. Setter-based fields keep the name of the setter that discovered them. Constructor fields get the setter found by the builder-side findBuilderSetterForProperty, or stay empty when no setter exists; no set<Cap> name is invented. CoreMethodsEnhancer throws a descriptive IllegalStateException (field + DTO) if a setter-based field ever has no setter name; that is an internal invariant. buildSetterSourceDescription uses the same guard; BuilderProcessor.analyse rethrows any IllegalStateException from builder extraction as a BuilderException on the annotated element (warning, or error in strict mode). Setter/getter property names come from one helper, propertyNameFromAccessor, which strips only a lowercase set/get followed by an uppercase letter.
  • Builder-side filters: setters still need the set + uppercase shape and one parameter, and prefix-free methods are never setters. On top of the generic JavaLangAnalyser lookups, BuilderDefinitionCreator applies the same eligibility as setter discovery: void, non-static, accessible, not @IgnoreInBuilder. An ignored getter therefore cannot supply a reserved name, and an ignored or ineligible setter is not used for constructor-field setter or deprecation lookup.
  • @IgnoreInBuilder targets: now {METHOD, CONSTRUCTOR}; PARAMETER was dropped because the processor never read it there. Automatic constructor selection skips ignored constructors. @IgnoreInBuilder together with @SimpleBuilderConstructor on one constructor skips generation with one diagnostic: a warning, or an error in strict mode. If every accessible constructor is ignored, the existing "no accessible constructor" diagnostic applies: a warning, or an error in strict mode. The performance-test generator no longer annotates record components.
  • Docs: docs/CONFIGURATION.md has a new section, "Property Detection and Ignoring Members", covering setter detection, @IgnoreInBuilder and reserved-word names. README links to it.

Tests

  • ReservedPropertyNameTest: parameterized over all Java keywords plus true/false/null; exact generated declarations for plain and prefixed naming, unusable and ignored getters, getter priority, strict mode (failed() with message and source location), and a constructor property not duplicated by a setter with a different name.
  • JavaLangAnalyserTest: getter ordering and setter shape rules.
  • BuilderProcessorTest: exact error/warning counts for the constructor conflict, and the all-constructors-ignored case in both normal and strict mode.
  • CoreMethodsEnhancerTest: resolved setter, missing setter name (exception), constructor-only fields, and no fields.
  • BuilderProcessorTest: an ignored larger constructor makes the builder use the smaller one, both annotations together (exact counts per mode, message and location), all constructors ignored, an ignored only-parameterized constructor, and the annotation on a parameter no longer compiles.
  • BuilderDefinitionCreatorTest: accessor name derivation (settings(), getaway(), isStatic, ...) and the setter source description.
  • AnnotationCopyTest: ignored or ineligible @Deprecated setters do not mark constructor fields as deprecated.
  • Mutation check: restoring the old lookups fails 8 of the 9 new filter tests. Restoring the old backing-field naming fails the naming tests.

Known limitations (not changed here)

Verified: mvn clean verify passes (processor: 624 tests). Generated examples are unchanged.

@codecov

codecov Bot commented Nov 22, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ All tests successful. No failed tests found.

📢 Thoughts on this report? Let us know!

- findSetterForProperty in JavaLangAnalyser: JavaBeans convention first,
  then a scan over setters whose derived property name (including the
  getter-based reserved-name fallback) matches; used to fill
  BuilderFieldDto.setterName and for deprecation propagation
- skip setters already covered by a constructor field once the
  getter-derived name is resolved (avoids duplicate renamed fields)
- replace deprecated StringUtils.removeStart with Strings.CI.removeStart
- simplify the setter lookup for deprecation detection and fix a broken
  comment wrap
…elds (java-helpers#418)

- extract the setter's property-name derivation into public
  JavaLangAnalyser.resolveSetterPropertyName and reuse it in
  createFieldFromSetter (removes the duplicated getter-fallback block)
- setter-discovered fields take the name from the already-iterated
  setter element; findSetterForProperty only runs for
  constructor-parameter fields and the deprecation lookup
@AndreasIgel

AndreasIgel commented Nov 23, 2026 •

Copy link
Copy Markdown
Collaborator

Check coverage in SonarQube, looks like the case of non getter found is never reached!? Why is this the case? Shouldn't we have already a test, where a setter without getter is defined? Ahhh, but that then would not have multiple candidates for getters? Maybe having a test with getters with different cases? But then the exact same case should be winning and this is currently not the case!?

@igel-devin-ai

Copy link
Copy Markdown
Contributor Author

Addressed in 88144e8:

  • The unchecked orElse(propertyName) is gone — resolveSetterPropertyName now returns Optional<String>, so a resolved name is always backed by a real member (legal setter-derived name or a name from an actual getter). New test resolveSetterPropertyName_usesGetterDerivedNameOrEmpty covers both branches, incl. the no-getter case (setTransient → empty).
  • Multi-style getter preference: added findGetterForField_prefersIsGetThenAccessorStyle — a DTO with active(), getActive() and isActive() — asserting isActive wins under the deterministic is* → get* → accessor ordering.

The processor-level paths (skip-warning and strict-mode error for setDefault with no legal getter) were already covered by ReservedPropertyNameTest; Sonar should now report the new-code branches covered on the next analysis.

Comment thread docs/CONFIGURATION.md Resolved
@java-helpers java-helpers deleted a comment from sonarqubecloud Bot Nov 23, 2026
@AndreasIgel
AndreasIgel merged commit 5aca98e into java-helpers:main Nov 23, 2026
7 checks passed
@sonarqubecloud

Copy link
Copy Markdown

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.

Property names derived from setters/getters can be Java keywords, aborting all generation

4 participants