Fix MigrateCollections* template errors on complex expression contexts#1067
Merged
knutwannheden merged 1 commit intomainfrom Apr 21, 2026
Merged
Conversation
The `MigrateCollections{Empty,Singleton,Unmodifiable}*` family applied
`JavaTemplate` in `.contextSensitive()` mode. When the
`Collections.emptyList()` (etc.) call sat inside a switch expression
(`case 0 -> Collections.emptyList()`), a ternary branch, a method
argument, or similar non-statement contexts, the block-statement stub
generator in rewrite-core either produced 0 statements (for
`J.SwitchExpression` parents, which it does not handle) or 2 statements
(for some substitutions with method-invocation values), causing
`IllegalArgumentException: Expected a template that would generate
exactly one statement to replace one statement, but generated {0,2}`.
Dropping `.contextSensitive()` makes these simple static-factory
templates context-free, so the stub is a trivial
`class Template {{ Object o = List.of(); }}` that always parses to one
statement regardless of surrounding syntax.
Also guard every `MigrateCollections*` recipe with
`KotlinFileChecker`/`GroovyFileChecker` preconditions — consistent with
`ReplaceUnusedVariablesWithUnderscore` and `IfElseIfConstructToSwitch` —
since `JavaTemplate` cannot reliably target non-Java trees, and the
idiomatic replacement in Kotlin (`emptyList()`) and Groovy (`[]`) isn't
`List.of()` anyway.
This was referenced Apr 21, 2026
MigrateCollections* template errors on complex expression contexts
This was referenced Apr 21, 2026
mergify Bot
added a commit
to robfrank/linklift
that referenced
this pull request
May 3, 2026
…30.1 to 3.34.0 [skip ci] Bumps [org.openrewrite.recipe:rewrite-migrate-java](https://github.com/openrewrite/rewrite-migrate-java) from 3.30.1 to 3.34.0. Release notes *Sourced from [org.openrewrite.recipe:rewrite-migrate-java's releases](https://github.com/openrewrite/rewrite-migrate-java/releases).* > 3.34.0 > ------ > > What's Changed > -------------- > > * Ensure `case` always has a space before the label in `IfElseIfConstructToSwitch` by [`@timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#1071](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/1071) > * Add `UsePredicateNot` to replace cast-and-negate with `Predicate.not(..)` by [`@timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#1072](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/1072) > > **Full Changelog**: <openrewrite/rewrite-migrate-java@v3.33.0...v3.34.0> > > 3.33.1 > ------ > > **Full Changelog**: <openrewrite/rewrite-migrate-java@v3.33.0...v3.33.1> > > 3.33.0 > ------ > > What's Changed > -------------- > > * Add URLEqualsHashCodeRecipes to Java best practices by [`@timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#1058](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/1058) > * Add --add-opens JVM args to surefire/failsafe for Java 25 by [`@timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#1055](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/1055) > * Collapse UpgradeDockerImageVersion cartesian via ChangeFrom capture by [`@jkschneider`](https://github.com/jkschneider) in [openrewrite/rewrite-migrate-java#1060](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/1060) > * Add NoJodaTime to Java best practices by [`@timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#1061](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/1061) > * Support `copyOf` migration for Guava by [`@greg-at-moderne`](https://github.com/greg-at-moderne) in [openrewrite/rewrite-migrate-java#1062](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/1062) > * Rename `_` identifier to `__` for Java ≤ 8 source files by [`@Jenson3210`](https://github.com/Jenson3210) in [openrewrite/rewrite-migrate-java#1063](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/1063) > * Fix `ClassCastException` on parenthesized initializers in `var` recipes by [`@knutwannheden`](https://github.com/knutwannheden) in [openrewrite/rewrite-migrate-java#1064](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/1064) > * Gate Kotlin-based Java version cap on actual Kotlin sources by [`@timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#1066](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/1066) > * Fix `MigrateCollections*` template errors on complex expression contexts by [`@knutwannheden`](https://github.com/knutwannheden) in [openrewrite/rewrite-migrate-java#1067](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/1067) > > **Full Changelog**: <openrewrite/rewrite-migrate-java@v3.32.1...v3.33.0> > > v3.32.1 > ------- > > What's Changed > -------------- > > * Remove parentheses from single unnamed lambda parameters by [`@timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#1054](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/1054) > * Skip Kotlin and Groovy files in ReplaceUnusedVariablesWithUnderscore by [`@timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#1056](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/1056) > > **Full Changelog**: <openrewrite/rewrite-migrate-java@v3.32.0...v3.32.1> > > 3.32.0 > ------ > > What's Changed > -------------- > > * Cap Java version at 24 for Kotlin <2.3 in UpgradeToJava25 by [`@timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#1035](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/1035) > * Inline JavaTemplate fields at their usage sites by [`@timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#1036](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/1036) > * Use `JavaTemplate.apply()` static method by [`@timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#1037](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/1037) > * Fix ClassCastException in UseVarForGenericMethodInvocations by [`@knutwannheden`](https://github.com/knutwannheden) in [openrewrite/rewrite-migrate-java#1039](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/1039) > * Upgrade Mockito to 5.17.x in Java 25 migration recipe by [`@timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#1041](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/1041) > * Migrate Mojarra and Glassfish JSF dependencies to Jakarta Faces by [`@timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#1040](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/1040) > * Upgrade jakarta.annotation-api to 3.0.x in Jakarta EE 11 recipe by [`@timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#1042](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/1042) > * Move type-use annotations to array brackets during JSpecify migration by [`@timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#1038](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/1038) > * Add explicit casts for visitor visit() return type by [`@timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#1049](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/1049) > * Add UseVarForTypeCast and UseVarForConstructors to UseVar recipe by [`@timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#1050](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/1050) > * Replace Collections.emptyXXX with Immutable Static Factory Methods by [`@mkarg`](https://github.com/mkarg) in [openrewrite/rewrite-migrate-java#1045](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/1045) > * Fix `AddMissingMethodImplementation` generating stubs for inherited methods by [`@timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#1051](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/1051) > * Add EnableLombokAnnotationProcessor to LombokBestPractices by [`@timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#1052](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/1052) ... (truncated) Commits * [`61c1777`](openrewrite/rewrite-migrate-java@61c1777) Extract documentation examples from tests * [`95028a5`](openrewrite/rewrite-migrate-java@95028a5) Add `UsePredicateNot` to replace cast-and-negate with `Predicate.not(..)` ([#1](https://redirect.github.com/openrewrite/rewrite-migrate-java/issues/1)... * [`a58b8b2`](openrewrite/rewrite-migrate-java@a58b8b2) Ensure `case` always has a space before the label in `IfElseIfConstructToSwit... * [`eac4344`](openrewrite/rewrite-migrate-java@eac4344) Fix `MigrateCollections*` template errors on complex expression contexts ([#1067](https://redirect.github.com/openrewrite/rewrite-migrate-java/issues/1067)) * [`0ca5cc8`](openrewrite/rewrite-migrate-java@0ca5cc8) Extract documentation examples from tests * [`968a22f`](openrewrite/rewrite-migrate-java@968a22f) Gate Kotlin-based Java version cap on actual Kotlin sources ([#1066](https://redirect.github.com/openrewrite/rewrite-migrate-java/issues/1066)) * [`a471dd1`](openrewrite/rewrite-migrate-java@a471dd1) [Auto] SDKMAN! Java candidates as of 2026-04-20T1116 * [`3b7b950`](openrewrite/rewrite-migrate-java@3b7b950) Fix `ClassCastException` on parenthesized initializers in `var` recipes ([#1064](https://redirect.github.com/openrewrite/rewrite-migrate-java/issues/1064)) * [`461609f`](openrewrite/rewrite-migrate-java@461609f) Rename `_` identifier to `__` for Java ≤ 8 source files ([#1063](https://redirect.github.com/openrewrite/rewrite-migrate-java/issues/1063)) * [`634e7d9`](openrewrite/rewrite-migrate-java@634e7d9) Support `copyOf` migration for Guava ([#1062](https://redirect.github.com/openrewrite/rewrite-migrate-java/issues/1062)) * Additional commits viewable in [compare view](openrewrite/rewrite-migrate-java@v3.30.1...v3.34.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 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)
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.
Motivation
The
MigrateCollections{Empty,Singleton,Unmodifiable}*recipes threwIllegalArgumentException: Expected a template that would generate exactly one statement to replace one statement, but generated {0,2}whenever theCollections.xxx()call sat in an expression context that the block-statement stub generator doesn't handle. Observed in the wild on Spring, Spring Data, and Conductor codebases — for example:The recipes also blew up on Kotlin
.ktsources (threeMigrateCollectionsSingletonMapfailures in Spring Security), becauseJavaTemplate's stub generator produces a Java-shaped stub that doesn't line up with a K-tree.Summary
.contextSensitive()from allJavaTemplate.builder(...)calls in theMigrateCollections*family. The templates (List.of(),Map.of(),Set.of(),Set.of(#{any()}),Map.of(#{any()}, #{any()})) reference only static factories and their arguments — no local scope — so the context-sensitive block-statement stub generator was doing nothing useful and just exposing gaps (no handling forJ.SwitchExpression, substitution quirks for method-invocation values). With context-free templating, the stub is alwaysclass Template {{ Object o = List.of(); }}and parses to exactly one statement regardless of the surrounding syntax.KotlinFileChecker/GroovyFileCheckerpreconditions to everyMigrateCollections*recipe, matching the pattern already used byReplaceUnusedVariablesWithUnderscoreandIfElseIfConstructToSwitch. Idiomatic Kotlin already hasemptyList()/mapOf()/setOf()and Groovy has[]/[:]; if those languages ever warrant migration, they belong in separate language-specific recipes.Test plan
MigrateCollectionsEmptyListTest,MigrateCollectionsEmptyMapTest,MigrateCollectionsEmptySetTest, andMigrateCollectionsSingletonMapTest.MigrateCollectionsSingletonMapTestto lock in the precondition../gradlew testpasses locally.