Add $N backref capture to ChangeFrom#7371
Merged
jkschneider merged 1 commit intomainfrom Apr 14, 2026
Merged
Conversation
Each `*` in `oldImageName` / `oldTag` / `oldDigest` / `oldPlatform` is now
a positional capture group, numbered in order of appearance within that
field. `$N` in the paired `new*` template substitutes capture N.
`$0` substitutes the full original field. `\$` is a literal dollar.
Capture numbering is per-field; there is no cross-field sharing.
Motivation: callers that want to preserve an arbitrary suffix across a
rewrite (e.g. `openjdk:8-jdk-alpine` -> `eclipse-temurin:17-jdk-alpine`)
previously had to enumerate every suffix variant, because `new*` values
were treated as literals. With capture support, the same transformation
collapses to a single recipe:
new ChangeFrom("openjdk", "8*", null, null,
"eclipse-temurin", "17$1", null, null)
`$1` carries whatever the `*` matched, including the empty string for the
bare `openjdk:8` tag (standard glob semantics per `StringUtils.matchesGlob`).
Validation in `validate()` rejects `$N` references that exceed the number
of `*`s in the paired `old*` glob, so authoring typos surface at recipe
construction rather than as silent wrong rewrites.
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
*inoldImageName/oldTag/oldDigest/oldPlatformis now a positional capture.$Nin the pairednew*substitutes capture N;$0substitutes the full original field;\$is a literal.validate()rejects dangling references.openjdk:8,openjdk:8-jdk-alpine,openjdk:8-jre-noble, etc. and rewrites each to theeclipse-temurin:17…equivalent in one recipe.Context
org.openrewrite.java.migrate.UpgradeDockerImageVersionin rewrite-migrate-java currently emits ~3,150ChangeFromchildren per invocation because it has to enumerate every (image × oldVersion × suffix) triple. Capture support lets that recipe shrink to ~90 children per invocation (35× reduction). A follow-up PR in rewrite-migrate-java consumes this feature.Test plan
ChangeFromTestcases still pass@Nested Capturestests cover: single capture, multi-capture preservation,$0full-match,\$literal, image-name capture, and threevalidate()rejection cases (backref without matching capture, backref beyond capture count,$0without captures is still valid)