Skip to content

Conversation

@timtebeek
Copy link
Member

@timtebeek timtebeek commented Oct 13, 2025

Summary

This PR fixes an issue where UseLombokGetter and UseLombokSetter recipes were incorrectly replacing getter/setter methods that access fields of other objects.

Problem

The recipes were treating expressions like sample.number as if they were accessing a field of the current class, when in reality they were accessing a field of another object (sample).

Before (incorrect behavior):

private class Inner {
    static Sample sample = null;
    
    private Long getNumber() {
        return sample.number;  // Accessing another object's field
    }
}

Would incorrectly transform to:

@Getter(AccessLevel.PRIVATE)
public Long number;  // Added to wrong class!

Solution

Modified LombokUtils.isGetter() and LombokUtils.isSetter() to only handle this.field patterns for J.FieldAccess expressions. Now the recipes correctly identify that methods accessing fields of other objects should not be replaced with Lombok annotations.

Changes

  • Fixed LombokUtils.isGetter() to check that field access targets are explicitly this
  • Fixed LombokUtils.isSetter() with the same pattern for consistency
  • Added test cases for both getter and setter scenarios

Test Plan

  • Added test case noChangeWhenMethodReturnsFieldOfAnotherObject() in UseLombokGetterTest
  • Added test case noChangeWhenMethodSetsFieldOfAnotherObject() in UseLombokSetterTest
  • All existing Lombok tests pass

🤖 Generated with Claude Code

timtebeek and others added 2 commits October 13, 2025 14:31
…of other objects

Fixes #872

The UseLombokGetter recipe was incorrectly removing getter methods when
the method body referenced a field of another object (e.g.,
`return sample.number;` where `sample` is a field of the current class).

The issue was in LombokUtils.isGetter() when handling J.FieldAccess
expressions. Previously, it checked if the target object was a field of
the current class, then incorrectly assumed the accessed field should
be annotated with @Getter.

The fix ensures that only `this.field` patterns are handled for
J.FieldAccess expressions, preventing the recipe from replacing getters
that access fields of other objects.

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

Co-Authored-By: Claude <[email protected]>
The setter logic had the same issue as the getter - it was incorrectly
replacing setter methods when they set fields of other objects
(e.g., `sample.number = value;`).

Applied the same fix: only handle `this.field` patterns for J.FieldAccess
expressions in the isSetter() method.

Added test case to verify the fix.

🤖 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 13, 2025
@timtebeek timtebeek marked this pull request as ready for review October 13, 2025 12:42
@timtebeek timtebeek self-assigned this Oct 13, 2025
@timtebeek timtebeek merged commit 7ebbf72 into main Oct 13, 2025
2 checks passed
@timtebeek timtebeek deleted the getter-setter-from-other-class branch October 13, 2025 12:45
@github-project-automation github-project-automation bot moved this from In Progress to Done in OpenRewrite Oct 13, 2025
@timtebeek timtebeek added bug Something isn't working recipe Recipe requested lombok labels Oct 13, 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

bug Something isn't working lombok recipe Recipe requested

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

Lombok recipes should not remove getter/setter methods when the method body is referring to another classes' field

2 participants