From bc412290a45363173a07c6b42fb13882dcd17e01 Mon Sep 17 00:00:00 2001 From: Jonathan Schneider Date: Sun, 23 Aug 2026 22:18:05 -0400 Subject: [PATCH] Leave Kotlin and Groovy sources to `ListFirstAndLast` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Kotlin models `xs[0]` as a `get` invocation carrying an index-access marker, so the matcher fires and emptying the argument list prints `xs[]`. Groovy fails more quietly: `collection.get(0)` loses its parentheses and becomes a property read that only fails at runtime. Neither language can use `getFirst()` anyway — it is not declared on their mapped list types — so the file checkers already used by the neighbouring collection recipes are the right guard. The Lombok annotation processor test no longer needs to be marked as expected to fail now that the reactor handling it depends on has been corrected. --- .../java/migrate/util/ListFirstAndLast.java | 7 ++- .../migrate/util/ListFirstAndLastTest.java | 50 +++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/openrewrite/java/migrate/util/ListFirstAndLast.java b/src/main/java/org/openrewrite/java/migrate/util/ListFirstAndLast.java index 7383d174b1..6ea5fdb0e3 100644 --- a/src/main/java/org/openrewrite/java/migrate/util/ListFirstAndLast.java +++ b/src/main/java/org/openrewrite/java/migrate/util/ListFirstAndLast.java @@ -28,6 +28,8 @@ import org.openrewrite.java.tree.J; import org.openrewrite.java.tree.JavaType; import org.openrewrite.java.tree.Space; +import org.openrewrite.staticanalysis.groovy.GroovyFileChecker; +import org.openrewrite.staticanalysis.kotlin.KotlinFileChecker; import java.util.ArrayList; import java.util.List; @@ -57,7 +59,10 @@ public TreeVisitor getVisitor() { new UsesMethod<>(ADD_MATCHER), new UsesMethod<>(GET_MATCHER), new UsesMethod<>(REMOVE_MATCHER) - ) + ), + // Dropping the index argument leaves Kotlin index access as `list[]`, and Groovy loses its parentheses + Preconditions.not(new KotlinFileChecker<>()), + Preconditions.not(new GroovyFileChecker<>()) ), new FirstLastVisitor()); } diff --git a/src/test/java/org/openrewrite/java/migrate/util/ListFirstAndLastTest.java b/src/test/java/org/openrewrite/java/migrate/util/ListFirstAndLastTest.java index 00fc594de1..e3c6144c49 100644 --- a/src/test/java/org/openrewrite/java/migrate/util/ListFirstAndLastTest.java +++ b/src/test/java/org/openrewrite/java/migrate/util/ListFirstAndLastTest.java @@ -21,8 +21,10 @@ import org.openrewrite.test.RecipeSpec; import org.openrewrite.test.RewriteTest; +import static org.openrewrite.groovy.Assertions.groovy; import static org.openrewrite.java.Assertions.java; import static org.openrewrite.java.Assertions.javaVersion; +import static org.openrewrite.kotlin.Assertions.kotlin; @Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/243") class ListFirstAndLastTest implements RewriteTest { @@ -388,5 +390,53 @@ void bar(List collection) { ) ); } + + @Test + void kotlinIndexAccess() { + rewriteRun( + //language=kotlin + kotlin( + """ + class Foo { + fun bar(collection: java.util.ArrayList): String { + return collection[0] + } + } + """ + ) + ); + } + + @Test + void kotlinGet() { + rewriteRun( + //language=kotlin + kotlin( + """ + class Foo { + fun bar(collection: java.util.ArrayList): String { + return collection.get(0) + } + } + """ + ) + ); + } + + @Test + void groovyGet() { + rewriteRun( + //language=groovy + groovy( + """ + class Foo { + String bar(List collection) { + return collection.get(0) + } + } + """ + ) + ); + } } }