Skip to content

Preserve casts when converting lombok.val to final var - #1233

Merged
greg-at-moderne merged 1 commit into
openrewrite:mainfrom
sullis:lombok-val-preserve-cast
Sep 3, 2026
Merged

Preserve casts when converting lombok.val to final var#1233
greg-at-moderne merged 1 commit into
openrewrite:mainfrom
sullis:lombok-val-preserve-cast

Conversation

@sullis

@sullis sullis commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

What's changed?

LombokValToFinalVar built the final var declaration with a JavaTemplate, which re-prints the initializer expression. Re-printing can lose type information: a cast to a type variable such as (T) o came out mangled, because the template parse has no knowledge of the enclosing class's type parameters.

This replaces the template with a direct LST transformation:

  • Build the var J.Identifier (as the existing no-initializer branch already did) and swap it in for the val type expression, so both code paths now share that construction.
  • Prepend a final modifier only when one isn't already present, moving the original type expression's prefix onto the modifier so formatting is preserved.

The initializer is carried over untouched, so casts — including generic ones — survive the conversion. Behavior for declarations without an initializer is unchanged (still a plain var, per openrewrite/rewrite#5637).

Also included is a small unrelated test fix: UpgradeToJava25Test.upgradeToJava25 pinned maven-compiler-plugin to 3.15.x in an assertion pattern, which breaks as soon as a newer minor is released. The pattern now accepts any 3.15+ version.

Anything in particular you'd like reviewers to focus on?

The prefix/modifier juggling is the part worth a close look — the existing tests cover annotated, already-final, multi-variable, and comment-carrying declarations, and two new tests cover the casts: preserveCast ((String) "foo") and preserveGenericCast ((T) o).

I can't run the build locally — dependency resolution needs credentials for artifacts.codegenomeproject.org — so I'm relying on CI for verification.

Have you considered any alternatives or workarounds?

Keeping JavaTemplate and making it type-aware. Sidestepping the re-print entirely is simpler, and it also removes the divergence between the initializer and no-initializer code paths.

@sullis

sullis commented Sep 2, 2026

Copy link
Copy Markdown
Contributor Author

@greg-at-moderne
greg-at-moderne self-requested a review September 2, 2026 21:02
@sullis
sullis force-pushed the lombok-val-preserve-cast branch from 4968a3b to dae3453 Compare September 2, 2026 21:21
@sullis
sullis force-pushed the lombok-val-preserve-cast branch from dae3453 to 3b96790 Compare September 2, 2026 21:40
@sullis

sullis commented Sep 2, 2026

Copy link
Copy Markdown
Contributor Author
  • CI build is green

@github-project-automation github-project-automation Bot moved this from In Progress to Ready to Review in OpenRewrite Sep 3, 2026
@greg-at-moderne
greg-at-moderne merged commit 880d8cf into openrewrite:main Sep 3, 2026
1 check passed
@github-project-automation github-project-automation Bot moved this from Ready to Review to Done in OpenRewrite Sep 3, 2026
@greg-at-moderne

Copy link
Copy Markdown
Contributor

Thanks for the PR. Looks well designed and I appreciate the tests.

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.

2 participants