Skip to content

Commit c069b6b

Browse files
committed
Convert Guava Iterables.any and .filter
1 parent 80f1c88 commit c069b6b

File tree

3 files changed

+265
-0
lines changed

3 files changed

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

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: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
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+
public static boolean test() {
52+
Collection<Object> collection = new ArrayList<>();
53+
Predicate<Object> isNotNull = Objects::nonNull;
54+
return Iterables.any(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 boolean test() {
67+
Collection<Object> collection = new ArrayList<>();
68+
Predicate<Object> isNotNull = Objects::nonNull;
69+
return collection.stream().anyMatch(isNotNull);
70+
}
71+
}
72+
"""
73+
)
74+
);
75+
}
76+
77+
@Test
78+
void IterablesAnyWithIterable() {
79+
//language=java
80+
rewriteRun(
81+
java(
82+
"""
83+
import java.util.ArrayList;
84+
import java.lang.Iterable;
85+
import java.util.Objects;
86+
87+
import com.google.common.base.Predicate;
88+
import com.google.common.collect.Iterables;
89+
90+
class Test {
91+
public static boolean test() {
92+
Iterable<Object> iterable = new ArrayList<>();
93+
Predicate<Object> isNotNull = Objects::nonNull;
94+
return Iterables.any(iterable, isNotNull);
95+
}
96+
}
97+
"""
98+
)
99+
);
100+
}
101+
102+
@Test
103+
void replaceIterablesFilter() {
104+
//language=java
105+
rewriteRun(
106+
java(
107+
"""
108+
import java.util.ArrayList;
109+
import java.util.Collection;
110+
import java.util.Objects;
111+
112+
import com.google.common.base.Predicate;
113+
import com.google.common.collect.Iterables;
114+
115+
class Test {
116+
public static Iterable<Object> test() {
117+
Collection<Object> collection = new ArrayList<>();
118+
Predicate<Object> isNotNull = Objects::nonNull;
119+
return Iterables.filter(collection, isNotNull);
120+
}
121+
}
122+
""",
123+
"""
124+
import java.util.ArrayList;
125+
import java.util.Collection;
126+
import java.util.Objects;
127+
128+
import com.google.common.base.Predicate;
129+
130+
class Test {
131+
public static Iterable<Object> test() {
132+
Collection<Object> collection = new ArrayList<>();
133+
Predicate<Object> isNotNull = Objects::nonNull;
134+
return collection.stream().filter(isNotNull).toList();
135+
}
136+
}
137+
"""
138+
)
139+
);
140+
}
141+
142+
@Test
143+
void IterablesFilterWithIterable() {
144+
//language=java
145+
rewriteRun(
146+
java(
147+
"""
148+
import java.util.ArrayList;
149+
import java.lang.Iterable;
150+
import java.util.Objects;
151+
152+
import com.google.common.base.Predicate;
153+
import com.google.common.collect.Iterables;
154+
155+
class Test {
156+
public static Iterable<Object> test() {
157+
Iterable<Object> iterable = new ArrayList<>();
158+
Predicate<Object> isNotNull = Objects::nonNull;
159+
return Iterables.filter(iterable, isNotNull);
160+
}
161+
}
162+
"""
163+
)
164+
);
165+
}
166+
}

0 commit comments

Comments
 (0)