Skip to content

Commit b17a592

Browse files
authored
Convert Iterables.all(Collection, Predictate) into .stream().allMatch(Predicate) (#945)
Fixes #920
1 parent c47788e commit b17a592

File tree

4 files changed

+197
-0
lines changed

4 files changed

+197
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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 NoGuavaIterablesAll extends Recipe {
34+
private static final MethodMatcher ITERABLES_ALL = new MethodMatcher("com.google.common.collect.Iterables all(java.lang.Iterable, com.google.common.base.Predicate)");
35+
36+
@Override
37+
public String getDisplayName() {
38+
return "Prefer `Collection.stream().allMatch(Predicate)`";
39+
}
40+
41+
@Override
42+
public String getDescription() {
43+
return "Prefer `Collection.stream().allMatch(Predicate)` over `Iterables.all(Collection, Predicate)`.";
44+
}
45+
46+
@Override
47+
public Set<String> getTags() {
48+
return singleton("guava");
49+
}
50+
51+
@Override
52+
public TreeVisitor<?, ExecutionContext> getVisitor() {
53+
return Preconditions.check(new UsesMethod<>(ITERABLES_ALL), new JavaVisitor<ExecutionContext>() {
54+
@Override
55+
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
56+
if (ITERABLES_ALL.matches(method)) {
57+
maybeRemoveImport("com.google.common.base.Predicate");
58+
maybeRemoveImport("com.google.common.collect.Iterables");
59+
maybeAddImport("java.util.function.Predicate");
60+
61+
if (TypeUtils.isAssignableTo("java.util.Collection", method.getArguments().get(0).getType())) {
62+
return JavaTemplate.builder("#{any(java.util.Collection)}.stream().allMatch(#{any(java.util.function.Predicate)})")
63+
.build()
64+
.apply(getCursor(),
65+
method.getCoordinates().replace(),
66+
method.getArguments().get(0),
67+
method.getArguments().get(1));
68+
}
69+
return method;
70+
}
71+
return super.visitMethodInvocation(method, ctx);
72+
}
73+
});
74+
}
75+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ recipeList:
3131
- org.openrewrite.java.migrate.guava.NoGuavaCreateTempDir
3232
- org.openrewrite.java.migrate.guava.NoGuavaDirectExecutor
3333
- org.openrewrite.java.migrate.guava.NoGuavaFunctionsCompose
34+
- org.openrewrite.java.migrate.guava.NoGuavaIterablesAll
3435
- org.openrewrite.java.migrate.guava.NoGuavaIterablesAnyFilter
3536
- org.openrewrite.java.migrate.guava.NoGuavaIterablesTransform
3637
- org.openrewrite.java.migrate.guava.NoGuavaCollections2Transform
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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 NoGuavaIterablesAllTest implements RewriteTest {
28+
29+
@Override
30+
public void defaults(RecipeSpec spec) {
31+
spec
32+
.recipe(new NoGuavaIterablesAll())
33+
.parser(JavaParser.fromJavaVersion().classpathFromResources(new InMemoryExecutionContext(), "guava"));
34+
}
35+
36+
@DocumentExample
37+
@Test
38+
void replaceIterablesAll() {
39+
//language=java
40+
rewriteRun(
41+
java(
42+
"""
43+
import java.util.Collection;
44+
45+
import com.google.common.base.Predicate;
46+
import com.google.common.collect.Iterables;
47+
48+
class Test {
49+
boolean test(Collection<String> collection, Predicate<String> aPredicate) {
50+
return Iterables.all(collection, aPredicate);
51+
}
52+
}
53+
""",
54+
"""
55+
import java.util.Collection;
56+
57+
import com.google.common.base.Predicate;
58+
59+
class Test {
60+
boolean test(Collection<String> collection, Predicate<String> aPredicate) {
61+
return collection.stream().allMatch(aPredicate);
62+
}
63+
}
64+
"""
65+
)
66+
);
67+
}
68+
69+
@Test
70+
void iterablesAllWithIterable() {
71+
//language=java
72+
rewriteRun(
73+
java(
74+
"""
75+
import java.lang.Iterable;
76+
77+
import com.google.common.base.Predicate;
78+
import com.google.common.collect.Iterables;
79+
80+
class Test {
81+
boolean test(Iterable<String> iterable, Predicate<String> aPredicate) {
82+
return Iterables.all(iterable, aPredicate);
83+
}
84+
}
85+
"""
86+
)
87+
);
88+
}
89+
}

src/test/java/org/openrewrite/java/migrate/guava/NoGuavaTest.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,36 @@ static boolean isEqual(Object obj0, Object obj1) {
128128
)
129129
);
130130
}
131+
132+
@Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/920")
133+
@Test
134+
void iterablesAllMigration() {
135+
//language=java
136+
rewriteRun(
137+
java(
138+
"""
139+
import java.util.Collection;
140+
141+
import com.google.common.base.Predicate;
142+
import com.google.common.collect.Iterables;
143+
144+
class Test {
145+
boolean test(Collection<String> collection, Predicate<String> aPredicate) {
146+
return Iterables.all(collection, aPredicate);
147+
}
148+
}
149+
""",
150+
"""
151+
import java.util.Collection;
152+
import java.util.function.Predicate;
153+
154+
class Test {
155+
boolean test(Collection<String> collection, Predicate<String> aPredicate) {
156+
return collection.stream().allMatch(aPredicate);
157+
}
158+
}
159+
"""
160+
)
161+
);
162+
}
131163
}

0 commit comments

Comments
 (0)