Prevent LombokValToFinalVar from removing star imports#986
Merged
Conversation
LombokValToFinalVar unconditionally called `maybeRemoveImport("lombok.var")`
in `visitCompilationUnit` on every file matching the `MaybeUsesImport`
precondition. This matched files with `import lombok.*;` even when no
val/var was used. In multi-module projects with incomplete type information,
`maybeRemoveImport` could not determine that other lombok types (like @DaTa,
@Getter) were still referenced, and removed the entire star import.
Now only calls `maybeRemoveImport` when there is an explicit
`import lombok.var;` statement, leaving star imports untouched.
Fixes #962
Jenson3210
commented
Feb 17, 2026
src/test/java/org/openrewrite/java/migrate/lombok/LombokValToFinalVarTest.java
Show resolved
Hide resolved
Jenson3210
commented
Feb 17, 2026
src/main/java/org/openrewrite/java/migrate/lombok/LombokValToFinalVar.java
Outdated
Show resolved
Hide resolved
timtebeek
reviewed
Feb 17, 2026
| break; | ||
| } | ||
| } | ||
| return super.visitCompilationUnit(compilationUnit, ctx); |
Member
There was a problem hiding this comment.
Would this be easier perhaps than manually inspecting the imports?
Suggested change
| return super.visitCompilationUnit(compilationUnit, ctx); | |
| J.CompilationUnit cu = super.visitCompilationUnit(compilationUnit, ctx); | |
| if (cu != compilationUnit) { | |
| maybeRemoveImport(LOMBOK_VAR); | |
| } | |
| return cu; |
Contributor
Author
There was a problem hiding this comment.
Can't do this as star imports would be removed causing the bug to reappear
timtebeek
approved these changes
Feb 17, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
maybeRemoveImport("lombok.var")when there is an explicitimport lombok.var;, not from star imports likeimport lombok.*;Problem
Running
UpgradeToJava21on multi-module Gradle projects removesimport lombok.*;wildcard imports without adding explicit replacements, breaking compilation. The reporter observed 95 files losing their star import with 0 files gaining explicit lombok imports.The root cause is in
LombokValToFinalVarwhich unconditionally calledmaybeRemoveImport("lombok.var")invisitCompilationUnit.Solution
Changed
visitCompilationUnitto check the compilation unit's import list: only callmaybeRemoveImport("lombok.var")when there is an explicitimport lombok.var;statement (wheregetQualid().getSimpleName()is not"*"). Star imports are left untouched.Note: when
import lombok.*;exists solely forvar(no other lombok usage), the star import will remain as an unused import after the recipe runs. This is an acceptable trade-off—an unused import is preferable to broken compilation.Test plan
Existing tests pass
preserveStarImportWithoutVarUsage— star import + annotations, no var, incomplete type infopreserveStarImportWithVarUsage— star import + var + annotations, incomplete type inforemoveExplicitVarImport— explicitimport lombok.var;removed with incomplete type inforemoveStarImportWhenOnlyValUsed— star import correctly removed when val is the only lombok usagestarImportRemainsWhenOnlyVarUsed— star import remains unused (documented trade-off)Fixes UpgradeToJava21 removes
import lombok.*;without adding explicit imports #962