From ee8a326dd7c75f200df1d16f4163b56d73719798 Mon Sep 17 00:00:00 2001 From: Jonathan Schneider Date: Wed, 16 Jun 2021 14:02:06 -0700 Subject: [PATCH] NoGuavaSetsNewHashSet --- .../migrate/guava/NoGuavaSetsNewHashSet.java | 76 +++++++++++++++++++ .../guava/NoGuavaSetsNewHashSetTest.kt | 75 ++++++++++++++++++ 2 files changed, 151 insertions(+) create mode 100644 src/main/java/org/openrewrite/java/migrate/guava/NoGuavaSetsNewHashSet.java create mode 100644 src/test/kotlin/org/openrewrite/java/migrate/guava/NoGuavaSetsNewHashSetTest.kt diff --git a/src/main/java/org/openrewrite/java/migrate/guava/NoGuavaSetsNewHashSet.java b/src/main/java/org/openrewrite/java/migrate/guava/NoGuavaSetsNewHashSet.java new file mode 100644 index 0000000000..bd57c1e114 --- /dev/null +++ b/src/main/java/org/openrewrite/java/migrate/guava/NoGuavaSetsNewHashSet.java @@ -0,0 +1,76 @@ +/* + * Copyright 2021 the original author or authors. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * https://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openrewrite.java.migrate.guava; + +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; +import org.openrewrite.java.search.UsesMethod; +import org.openrewrite.java.tree.J; + +import java.util.stream.Collectors; + +public class NoGuavaSetsNewHashSet extends Recipe { + private static final MethodMatcher NEW_HASH_SET = new MethodMatcher("com.google.common.collect.Sets newHashSet(..)"); + + @Override + public String getDisplayName() { + return "Use `new HashSet<>()` instead of Guava"; + } + + @Override + public String getDescription() { + return "Prefer the Java standard library over third-party usage of Guava in simple cases like this."; + } + + @Override + protected TreeVisitor getApplicableTest() { + return new UsesMethod<>(NEW_HASH_SET); + } + + @Override + protected TreeVisitor getVisitor() { + return new JavaVisitor() { + private final JavaTemplate newHashSet = template("new HashSet<>()") + .imports("java.util.HashSet") + .build(); + + @Override + public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext executionContext) { + if (NEW_HASH_SET.matches(method)) { + maybeRemoveImport("com.google.common.collect.Sets"); + maybeAddImport("java.util.HashSet"); + if(method.getArguments().isEmpty()) { + return method.withTemplate(newHashSet, method.getCoordinates().replace()); + } else { + maybeAddImport("java.util.Arrays"); + JavaTemplate newHashSetVarargs = template("new HashSet<>(Arrays.asList(" + method.getArguments().stream().map(a -> "#{any()}").collect(Collectors.joining(",")) + "))") + .imports("java.util.Arrays") + .imports("java.util.HashSet") + .build(); + return method.withTemplate(newHashSetVarargs, method.getCoordinates().replace(), + method.getArguments().toArray()); + } + } + return super.visitMethodInvocation(method, executionContext); + } + }; + } +} diff --git a/src/test/kotlin/org/openrewrite/java/migrate/guava/NoGuavaSetsNewHashSetTest.kt b/src/test/kotlin/org/openrewrite/java/migrate/guava/NoGuavaSetsNewHashSetTest.kt new file mode 100644 index 0000000000..0fbba35a11 --- /dev/null +++ b/src/test/kotlin/org/openrewrite/java/migrate/guava/NoGuavaSetsNewHashSetTest.kt @@ -0,0 +1,75 @@ +/* + * Copyright 2021 the original author or authors. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * https://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openrewrite.java.migrate.guava + +import org.junit.jupiter.api.Test +import org.openrewrite.Recipe +import org.openrewrite.java.JavaParser +import org.openrewrite.java.JavaRecipeTest + +class NoGuavaSetsNewHashSetTest: JavaRecipeTest { + override val parser: JavaParser + get() = JavaParser.fromJavaVersion() + .logCompilationWarningsAndErrors(true) + .classpath("guava") + .build() + + override val recipe: Recipe + get() = NoGuavaSetsNewHashSet() + + @Test + fun replaceWithNewHashSet() = assertChanged( + before = """ + import com.google.common.collect.*; + + import java.util.Set; + + class Test { + Set cardinalsWorldSeries = Sets.newHashSet(); + } + """, + after = """ + import java.util.HashSet; + import java.util.Set; + + class Test { + Set cardinalsWorldSeries = new HashSet<>(); + } + """ + ) + + @Test + fun replaceWithNewHashSetVarargs() = assertChanged( + before = """ + import com.google.common.collect.*; + + import java.util.Set; + + class Test { + Set cardinalsWorldSeries = Sets.newHashSet(2006, 2011); + } + """, + after = """ + import java.util.Arrays; + import java.util.HashSet; + import java.util.Set; + + class Test { + Set cardinalsWorldSeries = new HashSet<>(Arrays.asList(2006, 2011)); + } + """ + ) +}