Skip to content

Commit

Permalink
Replace newArrayList(Iterable) with ArrayList constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
jkschneider committed Jun 16, 2021
1 parent 8d7112c commit 2948f22
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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() {
Expand All @@ -39,7 +41,14 @@ public String getDescription() {

@Override
protected TreeVisitor<?, ExecutionContext> getApplicableTest() {
return new UsesMethod<>(NEW_ARRAY_LIST);
return new JavaIsoVisitor<ExecutionContext>() {
@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
Expand All @@ -49,12 +58,21 @@ protected TreeVisitor<?, ExecutionContext> 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);
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/META-INF/rewrite/no-guava.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import org.openrewrite.java.JavaRecipeTest
class NoGuavaListsNewArrayListTest: JavaRecipeTest {
override val parser: JavaParser
get() = JavaParser.fromJavaVersion()
.logCompilationWarningsAndErrors(true)
.classpath("guava")
.build()

Expand All @@ -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<Integer> l = Collections.emptyList();
List<Integer> cardinalsWorldSeries = Lists.newArrayList(l);
}
""",
after = """
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
class Test {
List<Integer> l = Collections.emptyList();
List<Integer> cardinalsWorldSeries = new ArrayList<>(l);
}
"""
)
}

0 comments on commit 2948f22

Please sign in to comment.