Skip to content

Prevent LombokValToFinalVar from removing star imports#986

Merged
timtebeek merged 3 commits intomainfrom
fix-star-import-removal
Feb 17, 2026
Merged

Prevent LombokValToFinalVar from removing star imports#986
timtebeek merged 3 commits intomainfrom
fix-star-import-removal

Conversation

@Jenson3210
Copy link
Contributor

@Jenson3210 Jenson3210 commented Feb 17, 2026

Summary

  • Only call maybeRemoveImport("lombok.var") when there is an explicit import lombok.var;, not from star imports like import lombok.*;
  • Added tests for star import preservation with and without var usage, explicit import removal, and edge cases

Problem

Running UpgradeToJava21 on multi-module Gradle projects removes import 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 LombokValToFinalVar which unconditionally called maybeRemoveImport("lombok.var") in visitCompilationUnit.

Solution

Changed visitCompilationUnit to check the compilation unit's import list: only call maybeRemoveImport("lombok.var") when there is an explicit import lombok.var; statement (where getQualid().getSimpleName() is not "*"). Star imports are left untouched.

Note: when import lombok.*; exists solely for var (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 info

  • preserveStarImportWithVarUsage — star import + var + annotations, incomplete type info

  • removeExplicitVarImport — explicit import lombok.var; removed with incomplete type info

  • removeStarImportWhenOnlyValUsed — star import correctly removed when val is the only lombok usage

  • starImportRemainsWhenOnlyVarUsed — star import remains unused (documented trade-off)

  • Fixes UpgradeToJava21 removes import lombok.*; without adding explicit imports #962

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 Jenson3210 requested a review from timtebeek February 17, 2026 10:13
break;
}
}
return super.visitCompilationUnit(compilationUnit, ctx);
Copy link
Member

Choose a reason for hiding this comment

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

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;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can't do this as star imports would be removed causing the bug to reappear

@github-project-automation github-project-automation bot moved this from In Progress to Ready to Review in OpenRewrite Feb 17, 2026
@timtebeek timtebeek merged commit 0c1793f into main Feb 17, 2026
1 check passed
@timtebeek timtebeek deleted the fix-star-import-removal branch February 17, 2026 15:06
@github-project-automation github-project-automation bot moved this from Ready to Review to Done in OpenRewrite Feb 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

UpgradeToJava21 removes import lombok.*; without adding explicit imports

2 participants