Skip to content

Commit 29759f4

Browse files
lredortimtebeek
andauthored
Convert Guava Iterables.any and .filter (#896)
* Convert Guava `Iterables.any` and `.filter` * Minimize tests --------- Co-authored-by: Tim te Beek <[email protected]>
1 parent d351952 commit 29759f4

File tree

3 files changed

+239
-0
lines changed

3 files changed

+239
-0
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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.JavaTemplate;
23+
import org.openrewrite.java.JavaVisitor;
24+
import org.openrewrite.java.MethodMatcher;
25+
import org.openrewrite.java.search.UsesMethod;
26+
import org.openrewrite.java.tree.J;
27+
import org.openrewrite.java.tree.TypeUtils;
28+
29+
import java.util.Set;
30+
31+
import static java.util.Collections.singleton;
32+
33+
public class NoGuavaIterablesAnyFilter extends Recipe {
34+
private static final MethodMatcher ITERABLES_ANY = new MethodMatcher("com.google.common.collect.Iterables any(java.lang.Iterable, com.google.common.base.Predicate)");
35+
private static final MethodMatcher ITERABLES_FILTER = new MethodMatcher("com.google.common.collect.Iterables filter(java.lang.Iterable, com.google.common.base.Predicate)");
36+
37+
@Override
38+
public String getDisplayName() {
39+
return "Prefer `Collection.stream().anyMatch(Predicate)`";
40+
}
41+
42+
@Override
43+
public String getDescription() {
44+
return "Prefer `Collection.stream().anyMatch(Predicate)` over `Iterables.any(Collection, Predicate)`.";
45+
}
46+
47+
@Override
48+
public Set<String> getTags() {
49+
return singleton("guava");
50+
}
51+
52+
@Override
53+
public TreeVisitor<?, ExecutionContext> getVisitor() {
54+
TreeVisitor<?, ExecutionContext> precondition = Preconditions.or(new UsesMethod<>(ITERABLES_ANY), new UsesMethod<>(ITERABLES_FILTER));
55+
return Preconditions.check(precondition, new JavaVisitor<ExecutionContext>() {
56+
@Override
57+
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
58+
if (ITERABLES_ANY.matches(method)) {
59+
maybeRemoveImport("com.google.common.base.Predicate");
60+
maybeRemoveImport("com.google.common.collect.Iterables");
61+
maybeAddImport("java.util.function.Predicate");
62+
63+
if (TypeUtils.isAssignableTo("java.util.Collection", method.getArguments().get(0).getType())) {
64+
return JavaTemplate.builder("#{any(java.util.Collection)}.stream().anyMatch(#{any(java.util.function.Predicate)})")
65+
.build()
66+
.apply(getCursor(),
67+
method.getCoordinates().replace(),
68+
method.getArguments().get(0),
69+
method.getArguments().get(1));
70+
}
71+
return method;
72+
}
73+
if (ITERABLES_FILTER.matches(method)) {
74+
maybeRemoveImport("com.google.common.base.Predicate");
75+
maybeRemoveImport("com.google.common.collect.Iterables");
76+
maybeAddImport("java.util.function.Predicate");
77+
78+
if (TypeUtils.isAssignableTo("java.util.Collection", method.getArguments().get(0).getType())) {
79+
return JavaTemplate.builder("#{any(java.util.Collection)}.stream().filter(#{any(java.util.function.Predicate)}).toList()")
80+
.build()
81+
.apply(getCursor(),
82+
method.getCoordinates().replace(),
83+
method.getArguments().get(0),
84+
method.getArguments().get(1));
85+
}
86+
return method;
87+
}
88+
return super.visitMethodInvocation(method, ctx);
89+
}
90+
});
91+
}
92+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ recipeList:
3030
- org.openrewrite.java.migrate.guava.NoGuavaCreateTempDir
3131
- org.openrewrite.java.migrate.guava.NoGuavaDirectExecutor
3232
- org.openrewrite.java.migrate.guava.NoGuavaInlineMeMethods
33+
- org.openrewrite.java.migrate.guava.NoGuavaIterablesAnyFilter
3334
- org.openrewrite.java.migrate.guava.NoGuavaListsNewArrayList
3435
- org.openrewrite.java.migrate.guava.NoGuavaListsNewCopyOnWriteArrayList
3536
- org.openrewrite.java.migrate.guava.NoGuavaListsNewLinkedList
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
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 NoGuavaIterablesAnyFilterTest implements RewriteTest {
28+
29+
@Override
30+
public void defaults(RecipeSpec spec) {
31+
spec
32+
.recipe(new NoGuavaIterablesAnyFilter())
33+
.parser(JavaParser.fromJavaVersion().classpathFromResources(new InMemoryExecutionContext(), "guava"));
34+
}
35+
36+
@DocumentExample
37+
@Test
38+
void replaceIterablesAny() {
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.Iterables;
49+
50+
class Test {
51+
boolean test(Collection<Object> collection, Predicate<Object> isNotNull) {
52+
return Iterables.any(collection, isNotNull);
53+
}
54+
}
55+
""",
56+
"""
57+
import java.util.ArrayList;
58+
import java.util.Collection;
59+
import java.util.Objects;
60+
61+
import com.google.common.base.Predicate;
62+
63+
class Test {
64+
boolean test(Collection<Object> collection, Predicate<Object> isNotNull) {
65+
return collection.stream().anyMatch(isNotNull);
66+
}
67+
}
68+
"""
69+
)
70+
);
71+
}
72+
73+
@Test
74+
void iterablesAnyWithIterable() {
75+
//language=java
76+
rewriteRun(
77+
java(
78+
"""
79+
import java.lang.Iterable;
80+
81+
import com.google.common.base.Predicate;
82+
import com.google.common.collect.Iterables;
83+
84+
class Test {
85+
boolean test(Iterable<Object> iterable, Predicate<Object> isNotNull) {
86+
return Iterables.any(iterable, isNotNull);
87+
}
88+
}
89+
"""
90+
)
91+
);
92+
}
93+
94+
@Test
95+
void replaceIterablesFilter() {
96+
//language=java
97+
rewriteRun(
98+
java(
99+
"""
100+
import java.util.Collection;
101+
102+
import com.google.common.base.Predicate;
103+
import com.google.common.collect.Iterables;
104+
105+
class Test {
106+
Iterable<Object> test(Collection<Object> collection, Predicate<Object> isNotNull) {
107+
return Iterables.filter(collection, isNotNull);
108+
}
109+
}
110+
""",
111+
"""
112+
import java.util.Collection;
113+
114+
import com.google.common.base.Predicate;
115+
116+
class Test {
117+
Iterable<Object> test(Collection<Object> collection, Predicate<Object> isNotNull) {
118+
return collection.stream().filter(isNotNull).toList();
119+
}
120+
}
121+
"""
122+
)
123+
);
124+
}
125+
126+
@Test
127+
void iterablesFilterWithIterable() {
128+
//language=java
129+
rewriteRun(
130+
java(
131+
"""
132+
import java.lang.Iterable;
133+
134+
import com.google.common.base.Predicate;
135+
import com.google.common.collect.Iterables;
136+
137+
class Test {
138+
Iterable<Object> test(Iterable<Object> iterable, Predicate<Object> isNotNull) {
139+
return Iterables.filter(iterable, isNotNull);
140+
}
141+
}
142+
"""
143+
)
144+
);
145+
}
146+
}

0 commit comments

Comments
 (0)