Fix NoGuava* collection recipes crashing on Groovy sources - #1210
Merged
Conversation
`Maps.newHashMap(someMap)` inside a Spock spec threw `IllegalArgumentException:
Expected a template that would generate exactly one statement to replace one
statement, but generated 0`.
The templates were marked `.contextSensitive()`, which makes `JavaTemplate`
re-parse the enclosing block as Java before splicing in the replacement. On a
Groovy LST that block can contain constructs with no Java equivalent (`def`,
`>> {}`, `_ as String`), so it fails to parse and zero statements come back.
None of these templates reference anything from the surrounding scope — they
are self-contained expressions with explicit imports — so context sensitivity
was never needed. `NoGuavaDirectExecutor` is left as-is: its `Runnable::run`
method reference does lose type attribution without context.
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.
What
org.openrewrite.java.migrate.guava.NoGuavaMapsNewHashMapthrows on Groovy sources:Found via the Moderne SaaS flagship run of Java best practices against the "Netflix + Spring" org, where it puts
Netflix/metacatintoRepositoryRecipeRunErroron every run. The trigger ismetacat-thrift/src/test/groovy/com/netflix/metacat/thrift/CatalogThriftHiveMetastoreSpec.groovy— a Spock spec containingMaps.newHashMap(ImmutableMap.of(...)), which a sibling recipe in the same composite (NoGuavaImmutableMapOf) has already rewritten toMaps.newHashMap(Map.of(...))by the time this recipe sees it.Why
The templates are marked
.contextSensitive(). That makesJavaTemplatereconstruct and re-parse the enclosing block as Java before splicing in the replacement. On a Groovy LST that block routinely contains constructs with no Java equivalent —def result = ...,timer.record(_, _) >> {},registry.createId(_ as String) >> id— so the reconstructed source does not parse,parseBlockStatementsreturns zero statements, andmaybeReplaceStatementthrows.None of these templates reference anything from the surrounding scope: they are self-contained expressions (
new HashMap<>(#{any(java.util.Map)})) whose only external dependency is an import they already declare explicitly. Context sensitivity was never needed here, and dropping it also lets the templates be cached.RecipeRunExceptionand skipped) — the replacement works correctly on Groovy once the template is context-free, so there is no reason to skip those sources.Scope
The same unnecessary
.contextSensitive()appears across the wholeNoGuava*collection-factory family, all with the identical latent crash, so they are fixed together:NoGuavaListsNewArrayList,NoGuavaListsNewCopyOnWriteArrayList,NoGuavaListsNewLinkedListNoGuavaMapsNewHashMap,NoGuavaMapsNewLinkedHashMap,NoGuavaMapsNewTreeMapNoGuavaSetsNewConcurrentHashSet,NoGuavaSetsNewHashSet,NoGuavaSetsNewLinkedHashSetNoGuavaDirectExecutoris deliberately left alone — itsRunnable::runmethod reference does lose type attribution without context (MemberReference type is missing or malformed), so.contextSensitive()is load-bearing there.Testing
Added
NoGuavaMapsNewHashMapTest#replaceWithNewHashMapWithMapInGroovy, agroovy(...)source spec modelled on the metacat block (Spockwhen:/then:, mock interactions, multi-line argument) that reproduces the crash before this change. It needs aGroovyParserwith Guava on its classpath, and an explicittestImplementation("org.openrewrite:rewrite-groovy")soorg.openrewrite.groovy.Assertionsis no longer picked up only transitively.All 218 tests in
org.openrewrite.java.migrate.guavapass.