From 2948f224fc688493a1e91059aa83d92ecfd07828 Mon Sep 17 00:00:00 2001 From: Jonathan Schneider Date: Wed, 16 Jun 2021 11:59:41 -0700 Subject: [PATCH] Replace newArrayList(Iterable) with ArrayList constructor --- .../guava/NoGuavaListsNewArrayList.java | 20 +++++++++++++- .../resources/META-INF/rewrite/no-guava.yml | 2 ++ .../guava/NoGuavaListsNewArrayListTest.kt | 27 +++++++++++++++++++ 3 files changed, 48 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/openrewrite/java/migrate/guava/NoGuavaListsNewArrayList.java b/src/main/java/org/openrewrite/java/migrate/guava/NoGuavaListsNewArrayList.java index 872392d106..7663b151b5 100644 --- a/src/main/java/org/openrewrite/java/migrate/guava/NoGuavaListsNewArrayList.java +++ b/src/main/java/org/openrewrite/java/migrate/guava/NoGuavaListsNewArrayList.java @@ -18,6 +18,7 @@ import org.openrewrite.ExecutionContext; import org.openrewrite.Recipe; import org.openrewrite.TreeVisitor; +import org.openrewrite.java.JavaIsoVisitor; import org.openrewrite.java.JavaTemplate; import org.openrewrite.java.JavaVisitor; import org.openrewrite.java.MethodMatcher; @@ -26,6 +27,7 @@ public class NoGuavaListsNewArrayList extends Recipe { private static final MethodMatcher NEW_ARRAY_LIST = new MethodMatcher("com.google.common.collect.Lists newArrayList()"); + private static final MethodMatcher NEW_ARRAY_LIST_ITERABLE = new MethodMatcher("com.google.common.collect.Lists newArrayList(java.lang.Iterable)"); @Override public String getDisplayName() { @@ -39,7 +41,14 @@ public String getDescription() { @Override protected TreeVisitor getApplicableTest() { - return new UsesMethod<>(NEW_ARRAY_LIST); + return new JavaIsoVisitor() { + @Override + public J.CompilationUnit visitCompilationUnit(J.CompilationUnit cu, ExecutionContext executionContext) { + doAfterVisit(new UsesMethod<>(NEW_ARRAY_LIST)); + doAfterVisit(new UsesMethod<>(NEW_ARRAY_LIST_ITERABLE)); + return cu; + } + }; } @Override @@ -49,12 +58,21 @@ protected TreeVisitor getVisitor() { .imports("java.util.ArrayList") .build(); + private final JavaTemplate newArrayListIterable = template("new ArrayList<>(#{any(java.lang.Iterable)})") + .imports("java.util.ArrayList") + .build(); + @Override public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext executionContext) { if (NEW_ARRAY_LIST.matches(method)) { maybeRemoveImport("com.google.common.collect.Lists"); maybeAddImport("java.util.ArrayList"); return method.withTemplate(newArrayList, method.getCoordinates().replace()); + } else if (NEW_ARRAY_LIST_ITERABLE.matches(method)) { + maybeRemoveImport("com.google.common.collect.Lists"); + maybeAddImport("java.util.ArrayList"); + return method.withTemplate(newArrayListIterable, method.getCoordinates().replace(), + method.getArguments().get(0)); } return super.visitMethodInvocation(method, executionContext); } diff --git a/src/main/resources/META-INF/rewrite/no-guava.yml b/src/main/resources/META-INF/rewrite/no-guava.yml index 90ca3f4f1d..5111900cc6 100644 --- a/src/main/resources/META-INF/rewrite/no-guava.yml +++ b/src/main/resources/META-INF/rewrite/no-guava.yml @@ -18,6 +18,8 @@ type: specs.openrewrite.org/v1beta/recipe name: org.openrewrite.java.migrate.guava.NoGuava displayName: Use Java standard library instead of Guava description: Guava filled in important gaps in the Java standard library and still does. But at least some of Guava's API surface area is covered by the Java standard library now, and some projects may be able to remove Guava altogether if they migrate to standard library for these functions. +tags: + - guava recipeList: - org.openrewrite.java.migrate.guava.NoGuavaImmutableSetOf - org.openrewrite.java.migrate.guava.NoGuavaDirectExecutor diff --git a/src/test/kotlin/org/openrewrite/java/migrate/guava/NoGuavaListsNewArrayListTest.kt b/src/test/kotlin/org/openrewrite/java/migrate/guava/NoGuavaListsNewArrayListTest.kt index 95811b3069..122034a211 100644 --- a/src/test/kotlin/org/openrewrite/java/migrate/guava/NoGuavaListsNewArrayListTest.kt +++ b/src/test/kotlin/org/openrewrite/java/migrate/guava/NoGuavaListsNewArrayListTest.kt @@ -23,6 +23,7 @@ import org.openrewrite.java.JavaRecipeTest class NoGuavaListsNewArrayListTest: JavaRecipeTest { override val parser: JavaParser get() = JavaParser.fromJavaVersion() + .logCompilationWarningsAndErrors(true) .classpath("guava") .build() @@ -49,4 +50,30 @@ class NoGuavaListsNewArrayListTest: JavaRecipeTest { } """ ) + + @Test + fun replaceWithNewArrayListIterable() = assertChanged( + before = """ + import com.google.common.collect.*; + + import java.util.ArrayList; + import java.util.Collections; + import java.util.List; + + class Test { + List l = Collections.emptyList(); + List cardinalsWorldSeries = Lists.newArrayList(l); + } + """, + after = """ + import java.util.ArrayList; + import java.util.Collections; + import java.util.List; + + class Test { + List l = Collections.emptyList(); + List cardinalsWorldSeries = new ArrayList<>(l); + } + """ + ) }