Skip to content

Commit 942e67c

Browse files
committed
Convert Guava Collections2.filter
1 parent 80f1c88 commit 942e67c

File tree

3 files changed

+151
-0
lines changed

3 files changed

+151
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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.MethodMatcher;
25+
import org.openrewrite.java.search.UsesMethod;
26+
import org.openrewrite.java.tree.J;
27+
28+
import java.util.Set;
29+
30+
import static java.util.Collections.singleton;
31+
32+
public class NoGuavaCollections2Filter extends Recipe {
33+
private static final MethodMatcher COLLECTIONS2_FILTER = new MethodMatcher("com.google.common.collect.Collections2 filter(java.util.Collection, com.google.common.base.Predicate)");
34+
35+
@Override
36+
public String getDisplayName() {
37+
return "Prefer `Collection.stream().filter(Predicate)`";
38+
}
39+
40+
@Override
41+
public String getDescription() {
42+
return "Prefer `Collection.stream().filter(Predicate)` over `Collections2.filter(Collection, Predicate)`.";
43+
}
44+
45+
@Override
46+
public Set<String> getTags() {
47+
return singleton("guava");
48+
}
49+
50+
@Override
51+
public TreeVisitor<?, ExecutionContext> getVisitor() {
52+
return Preconditions.check(
53+
new UsesMethod<>("com.google.common.collect.Collections2 filter(java.util.Collection, com.google.common.base.Predicate)"),
54+
new JavaIsoVisitor<ExecutionContext>() {
55+
@Override
56+
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
57+
if (COLLECTIONS2_FILTER.matches(method)) {
58+
maybeRemoveImport("com.google.common.base.Predicate");
59+
maybeRemoveImport("com.google.common.collect.Collections2");
60+
maybeAddImport("java.util.function.Predicate");
61+
62+
return JavaTemplate.builder("#{any(java.util.Collection)}.stream().filter(#{any(java.util.function.Predicate)}).toList()")
63+
.build()
64+
.apply(getCursor(),
65+
method.getCoordinates().replace(),
66+
method.getArguments().get(0),
67+
method.getArguments().get(1));
68+
}
69+
return super.visitMethodInvocation(method, ctx);
70+
}
71+
}
72+
);
73+
}
74+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ tags:
2727
recipeList:
2828
- org.openrewrite.java.migrate.guava.NoGuavaJava11
2929
- org.openrewrite.java.migrate.guava.NoGuavaJava21
30+
- org.openrewrite.java.migrate.guava.NoGuavaCollections2Filter
3031
- org.openrewrite.java.migrate.guava.NoGuavaCreateTempDir
3132
- org.openrewrite.java.migrate.guava.NoGuavaDirectExecutor
3233
- org.openrewrite.java.migrate.guava.NoGuavaInlineMeMethods
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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 NoGuavaCollections2FilterTest implements RewriteTest {
28+
29+
@Override
30+
public void defaults(RecipeSpec spec) {
31+
spec
32+
.recipe(new NoGuavaCollections2Filter())
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.ArrayList;
44+
import java.util.Collection;
45+
import java.util.Objects;
46+
47+
import com.google.common.base.Predicate;
48+
import com.google.common.collect.Collections2;
49+
50+
class Test {
51+
public static Collection<Object> test() {
52+
Collection<Object> collection = new ArrayList<>();
53+
Predicate<Object> isNotNull = Objects::nonNull;
54+
return Collections2.filter(collection, isNotNull);
55+
}
56+
}
57+
""",
58+
"""
59+
import java.util.ArrayList;
60+
import java.util.Collection;
61+
import java.util.Objects;
62+
63+
import com.google.common.base.Predicate;
64+
65+
class Test {
66+
public static Collection<Object> test() {
67+
Collection<Object> collection = new ArrayList<>();
68+
Predicate<Object> isNotNull = Objects::nonNull;
69+
return collection.stream().filter(isNotNull).toList();
70+
}
71+
}
72+
"""
73+
)
74+
);
75+
}
76+
}

0 commit comments

Comments
 (0)