Skip to content

Conversation

@timtebeek
Copy link
Member

@timtebeek timtebeek commented Oct 17, 2025

Summary

  • Fixed issue where @Getter annotation was being added twice to field declarations containing multiple variables
  • Added test case multipleVariableDeclarations to verify the fix

Problem

When a class had multiple variables declared on one line (e.g., int foo, bar = 9;) with corresponding getter methods for each variable, the UseLombokGetter recipe was incorrectly adding the @Getter annotation twice to the same field declaration.

This happened because:

  1. Each getter method (getFoo() and getBar()) triggered a separate FieldAnnotator visitor
  2. Both visitors encountered the same VariableDeclarations node (since both variables share the same declaration)
  3. Each visitor attempted to add the annotation without checking if it already existed

Solution

Updated FieldAnnotator.visitVariableDeclarations() to check if the annotation already exists before applying it. The fix:

  • Checks if the annotation simple name already exists in the variable declaration's leading annotations
  • Only applies the annotation if it doesn't already exist
  • Returns the multiVariable node appropriately in both cases

Test plan

🤖 Generated with Claude Code

When multiple variables are declared on one line (e.g., `int foo, bar = 9;`)
with corresponding getter methods, the recipe was adding @Getter twice to
the same field declaration. This happened because each getter method triggered
a separate FieldAnnotator visitor, and both attempted to annotate the shared
VariableDeclarations node.

The fix adds a check to prevent duplicate annotations by verifying if the
annotation already exists before applying it.

Fixes #876

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
@github-project-automation github-project-automation bot moved this to In Progress in OpenRewrite Oct 17, 2025
@timtebeek timtebeek self-assigned this Oct 17, 2025
@timtebeek timtebeek moved this from In Progress to Ready to Review in OpenRewrite Oct 17, 2025
@timtebeek timtebeek marked this pull request as ready for review October 17, 2025 13:21
@timtebeek timtebeek merged commit ce56846 into main Oct 17, 2025
2 checks passed
@timtebeek timtebeek deleted the lombok-multiple-variable-declarations branch October 17, 2025 13:48
@github-project-automation github-project-automation bot moved this from Ready to Review to Done in OpenRewrite Oct 17, 2025
mergify bot added a commit to robfrank/linklift that referenced this pull request Oct 30, 2025
…19.0 to 3.20.0 [skip ci]

[//]: # (dependabot-start)
⚠️ \*\*Dependabot is rebasing this PR\*\* ⚠️
Rebasing might not happen immediately, so don't worry if this takes some time.
Note: if you make any changes to this PR yourself, they will take precedence over the rebase.
---
[//]: # (dependabot-end)
Bumps [org.openrewrite.recipe:rewrite-migrate-java](https://github.com/openrewrite/rewrite-migrate-java) from 3.19.0 to 3.20.0.
Release notes

*Sourced from [org.openrewrite.recipe:rewrite-migrate-java's releases](https://github.com/openrewrite/rewrite-migrate-java/releases).*

> 3.20.0
> ------
>
> What's Changed
> --------------
>
> * Fix Lombok recipes incorrectly replacing getters/setters accessing other objects' fields by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#873](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/873)
> * Update suppressions by [`@​Laurens-W`](https://github.com/Laurens-W) in [openrewrite/rewrite-migrate-java#874](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/874)
> * Fix duplicate [`@​Getter`](https://github.com/Getter) annotations on multiple variable declarations by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#877](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/877)
> * Enable the switch recipes as part of the Java 21 migration by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#878](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/878)
> * Fix `NoGuavaSetsNewHashSet` to skip `Iterable`-only arguments by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#882](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/882)
> * Convert Guava `Predicates.and` and `.or` by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#885](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/885)
> * Convert Guava `Predicates.equalTo` and `.not` by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#887](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/887)
> * OpenRewrite recipe best practices by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#888](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/888)
>
> **Full Changelog**: <openrewrite/rewrite-migrate-java@v3.19.0...v3.20.0>


Commits

* [`ecb6886`](openrewrite/rewrite-migrate-java@ecb6886) OpenRewrite recipe best practices ([#888](https://redirect.github.com/openrewrite/rewrite-migrate-java/issues/888))
* [`8014157`](openrewrite/rewrite-migrate-java@8014157) Convert Guava `Predicates.equalTo` and `.not` ([#887](https://redirect.github.com/openrewrite/rewrite-migrate-java/issues/887))
* [`cef9c1f`](openrewrite/rewrite-migrate-java@cef9c1f) Convert Guava `Predicates.and` and `.or` ([#885](https://redirect.github.com/openrewrite/rewrite-migrate-java/issues/885))
* [`67e8d8c`](openrewrite/rewrite-migrate-java@67e8d8c) Fix `NoGuavaSetsNewHashSet` to skip `Iterable`-only arguments ([#882](https://redirect.github.com/openrewrite/rewrite-migrate-java/issues/882))
* [`92f9a94`](openrewrite/rewrite-migrate-java@92f9a94) Enable the switch recipes as part of the Java 21 migration ([#878](https://redirect.github.com/openrewrite/rewrite-migrate-java/issues/878))
* [`209211e`](openrewrite/rewrite-migrate-java@209211e) [Auto] SDKMAN! Java candidates as of 2025-10-20T1016
* [`ce56846`](openrewrite/rewrite-migrate-java@ce56846) Fix duplicate [`@​Getter`](https://github.com/Getter) annotations on multiple variable declarations ([#877](https://redirect.github.com/openrewrite/rewrite-migrate-java/issues/877))
* [`e18534b`](openrewrite/rewrite-migrate-java@e18534b) Add missed recipes to Java 6 and No Guava recipes
* [`207d898`](openrewrite/rewrite-migrate-java@207d898) Update suppressions ([#874](https://redirect.github.com/openrewrite/rewrite-migrate-java/issues/874))
* [`7ebbf72`](openrewrite/rewrite-migrate-java@7ebbf72) Fix Lombok recipes incorrectly replacing getters/setters accessing other obje...
* Additional commits viewable in [compare view](openrewrite/rewrite-migrate-java@v3.19.0...v3.20.0)
  
[![Dependabot compatibility score](https://dependabot-badges.githubapp.com/badges/compatibility\_score?dependency-name=org.openrewrite.recipe:rewrite-migrate-java&package-manager=maven&previous-version=3.19.0&new-version=3.20.0)](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores)
Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting `@dependabot rebase`.
[//]: # (dependabot-automerge-start)
[//]: # (dependabot-automerge-end)
---
Dependabot commands and options
  
You can trigger Dependabot actions by commenting on this PR:
- `@dependabot rebase` will rebase this PR
- `@dependabot recreate` will recreate this PR, overwriting any edits that have been made to it
- `@dependabot merge` will merge this PR after your CI passes on it
- `@dependabot squash and merge` will squash and merge this PR after your CI passes on it
- `@dependabot cancel merge` will cancel a previously requested merge and block automerging
- `@dependabot reopen` will reopen this PR if it is closed
- `@dependabot close` will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually
- `@dependabot show  ignore conditions` will show all of the ignore conditions of the specified dependency
- `@dependabot ignore this major version` will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
- `@dependabot ignore this minor version` will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
- `@dependabot ignore this dependency` will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

Support lombok getter/setter generation for multiple variables declared on a single line

2 participants