Skip to content

Commit 4d091fa

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

File tree

3 files changed

+265
-0
lines changed

3 files changed

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

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

0 commit comments

Comments
 (0)