Skip to content

Commit 56a67de

Browse files
committed
Convert Guava Sets.filter
1 parent 80f1c88 commit 56a67de

File tree

3 files changed

+156
-0
lines changed

3 files changed

+156
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright 2024 the original author or authors.
3+
* <p>
4+
* Licensed under the Moderne Source Available License (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* https://docs.moderne.io/licensing/moderne-source-available-license
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.openrewrite.java.migrate.guava;
17+
18+
import org.openrewrite.ExecutionContext;
19+
import org.openrewrite.Preconditions;
20+
import org.openrewrite.Recipe;
21+
import org.openrewrite.TreeVisitor;
22+
import org.openrewrite.java.JavaIsoVisitor;
23+
import org.openrewrite.java.JavaTemplate;
24+
import org.openrewrite.java.JavaVisitor;
25+
import org.openrewrite.java.MethodMatcher;
26+
import org.openrewrite.java.search.UsesMethod;
27+
import org.openrewrite.java.tree.J;
28+
import org.openrewrite.java.tree.TypeUtils;
29+
30+
import java.util.Set;
31+
32+
import static java.util.Collections.singleton;
33+
34+
public class NoGuavaSetsFilter extends Recipe {
35+
private static final MethodMatcher SETS_FILTER = new MethodMatcher("com.google.common.collect.Sets filter(java.util.Set, com.google.common.base.Predicate)");
36+
37+
@Override
38+
public String getDisplayName() {
39+
return "Prefer `Collection.stream().filter(Predicate)`";
40+
}
41+
42+
@Override
43+
public String getDescription() {
44+
return "Prefer `Collection.stream().filter(Predicate)` over `Sets.filter(Set, Predicate)`.";
45+
}
46+
47+
@Override
48+
public Set<String> getTags() {
49+
return singleton("guava");
50+
}
51+
52+
@Override
53+
public TreeVisitor<?, ExecutionContext> getVisitor() {
54+
return Preconditions.check(
55+
new UsesMethod<>("com.google.common.collect.Sets filter(java.util.Set, com.google.common.base.Predicate)"),
56+
new JavaIsoVisitor<ExecutionContext>() {
57+
@Override
58+
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
59+
if (SETS_FILTER.matches(method)) {
60+
maybeRemoveImport("com.google.common.base.Predicate");
61+
maybeRemoveImport("com.google.common.collect.Sets");
62+
maybeAddImport("java.util.function.Predicate");
63+
maybeAddImport("java.util.stream.Collectors");
64+
65+
return JavaTemplate.builder("#{any(java.util.Collection)}.stream().filter(#{any(java.util.function.Predicate)}).collect(Collectors.toSet())")
66+
.imports("java.util.stream.Collectors")
67+
.build()
68+
.apply(getCursor(),
69+
method.getCoordinates().replace(),
70+
method.getArguments().get(0),
71+
method.getArguments().get(1));
72+
}
73+
return super.visitMethodInvocation(method, ctx);
74+
}
75+
}
76+
);
77+
}
78+
}

src/main/resources/META-INF/rewrite/no-guava.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ recipeList:
3838
- org.openrewrite.java.migrate.guava.NoGuavaPrimitiveAsList
3939
- org.openrewrite.java.migrate.guava.NoGuavaRefasterRecipes
4040
- org.openrewrite.java.migrate.guava.NoGuavaMapsNewHashMap
41+
- org.openrewrite.java.migrate.guava.NoGuavaSetsFilter
4142
- org.openrewrite.java.migrate.guava.NoGuavaSetsNewHashSet
4243
- org.openrewrite.java.migrate.guava.NoGuavaSetsNewConcurrentHashSet
4344
- org.openrewrite.java.migrate.guava.NoGuavaSetsNewLinkedHashSet
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright 2024 the original author or authors.
3+
* <p>
4+
* Licensed under the Moderne Source Available License (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* https://docs.moderne.io/licensing/moderne-source-available-license
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.openrewrite.java.migrate.guava;
17+
18+
import org.junit.jupiter.api.Test;
19+
import org.openrewrite.DocumentExample;
20+
import org.openrewrite.InMemoryExecutionContext;
21+
import org.openrewrite.java.JavaParser;
22+
import org.openrewrite.test.RecipeSpec;
23+
import org.openrewrite.test.RewriteTest;
24+
25+
import static org.openrewrite.java.Assertions.java;
26+
27+
class NoGuavaSetsFilterTest implements RewriteTest {
28+
29+
@Override
30+
public void defaults(RecipeSpec spec) {
31+
spec
32+
.recipe(new NoGuavaSetsFilter())
33+
.parser(JavaParser.fromJavaVersion().classpathFromResources(new InMemoryExecutionContext(), "guava"));
34+
}
35+
36+
@DocumentExample
37+
@Test
38+
void replaceSetsFilter() {
39+
//language=java
40+
rewriteRun(
41+
java(
42+
"""
43+
import java.util.HashSet;
44+
import java.util.Objects;
45+
import java.util.Set;
46+
47+
import com.google.common.base.Predicate;
48+
import com.google.common.collect.Sets;
49+
50+
class Test {
51+
public static Set<Object> test() {
52+
Set<Object> set = new HashSet<>();
53+
Predicate<Object> isNotNull = Objects::nonNull;
54+
return Sets.filter(set, isNotNull);
55+
}
56+
}
57+
""",
58+
"""
59+
import java.util.HashSet;
60+
import java.util.Objects;
61+
import java.util.Set;
62+
import java.util.stream.Collectors;
63+
64+
import com.google.common.base.Predicate;
65+
66+
class Test {
67+
public static Set<Object> test() {
68+
Set<Object> set = new HashSet<>();
69+
Predicate<Object> isNotNull = Objects::nonNull;
70+
return set.stream().filter(isNotNull).collect(Collectors.toSet());
71+
}
72+
}
73+
"""
74+
)
75+
);
76+
}
77+
}

0 commit comments

Comments
 (0)