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) + } + } + """ + ) + ); + } } }