Skip to content

Commit f4a574d

Browse files
lredortimtebeek
andauthored
Convert Guava Predicates.equalTo (#911)
* Convert Guava `Predicates.equalTo` Fixes issue #903 * Tweak when to add type argument to method * Remove test fixed in subsequent recipe --------- Co-authored-by: Tim te Beek <[email protected]>
1 parent 0680919 commit f4a574d

File tree

6 files changed

+186
-61
lines changed

6 files changed

+186
-61
lines changed

src/main/java/org/openrewrite/java/migrate/guava/NoGuavaPredicatesAndOr.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
public class NoGuavaPredicatesAndOr extends Recipe {
3636
private static final MethodMatcher PREDICATES_AND = new MethodMatcher("com.google.common.base.Predicates and(..)");
3737
private static final MethodMatcher PREDICATES_OR = new MethodMatcher("com.google.common.base.Predicates or(..)");
38+
private static final MethodMatcher PREDICATES_EQUAL_TO = new MethodMatcher("com.google.common.base.Predicates equalTo(..)");
3839

3940
@Override
4041
public String getDisplayName() {
@@ -79,7 +80,7 @@ private J handlePredicatesMethod(J.MethodInvocation method, String operation) {
7980
Expression result = arguments.get(0);
8081

8182
// Avoid generic type issues by not making any changes just yet
82-
if (result instanceof J.MethodInvocation) {
83+
if (result instanceof J.MethodInvocation && !PREDICATES_EQUAL_TO.matches(result)) {
8384
return method;
8485
}
8586

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* Copyright 2025 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.ShortenFullyQualifiedTypeReferences;
26+
import org.openrewrite.java.search.UsesMethod;
27+
import org.openrewrite.java.tree.J;
28+
import org.openrewrite.java.tree.JavaType;
29+
30+
import java.util.Set;
31+
32+
import static java.util.Collections.singleton;
33+
34+
public class NoGuavaPredicatesEqualTo extends Recipe {
35+
private static final MethodMatcher PREDICATES_EQUAL_TO = new MethodMatcher("com.google.common.base.Predicates equalTo(..)");
36+
37+
@Override
38+
public String getDisplayName() {
39+
return "Prefer `Predicate.isEqual(Object)`";
40+
}
41+
42+
@Override
43+
public String getDescription() {
44+
return "Prefer `Predicate.isEqual(Object)` over `Predicates.equalTo(Object)`.";
45+
}
46+
47+
@Override
48+
public Set<String> getTags() {
49+
return singleton("guava");
50+
}
51+
52+
@Override
53+
public TreeVisitor<?, ExecutionContext> getVisitor() {
54+
return Preconditions.check(
55+
new UsesMethod<>(PREDICATES_EQUAL_TO),
56+
new JavaIsoVisitor<ExecutionContext>() {
57+
@Override
58+
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
59+
if (PREDICATES_EQUAL_TO.matches(method)) {
60+
maybeRemoveImport("com.google.common.base.Predicates");
61+
maybeAddImport("java.util.function.Predicate");
62+
63+
if (method.getMethodType().getParameterTypes().get(0) instanceof JavaType.Parameterized) {
64+
String typeString = method.getArguments().get(0).getType().toString();
65+
J.MethodInvocation genericMethod = JavaTemplate.builder("Predicate.<" + typeString + ">isEqual(#{any(java.lang.Object)})")
66+
.imports("java.util.function.Predicate")
67+
.build()
68+
.apply(getCursor(),
69+
method.getCoordinates().replace(),
70+
method.getArguments().get(0));
71+
doAfterVisit(ShortenFullyQualifiedTypeReferences.modifyOnly(genericMethod));
72+
return genericMethod;
73+
}
74+
// Fallback is not type is found.
75+
return JavaTemplate.builder("Predicate.isEqual(#{any(java.lang.Object)})")
76+
.imports("java.util.function.Predicate")
77+
.build()
78+
.apply(getCursor(),
79+
method.getCoordinates().replace(),
80+
method.getArguments().get(0));
81+
}
82+
return super.visitMethodInvocation(method, ctx);
83+
}
84+
}
85+
);
86+
}
87+
}

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ recipeList:
3737
- org.openrewrite.java.migrate.guava.NoGuavaListsNewLinkedList
3838
- org.openrewrite.java.migrate.guava.NoGuavaMapsNewTreeMap
3939
- org.openrewrite.java.migrate.guava.NoGuavaPredicatesAndOr
40+
- org.openrewrite.java.migrate.guava.NoGuavaPredicatesEqualTo
4041
- org.openrewrite.java.migrate.guava.NoGuavaPrimitiveAsList
4142
- org.openrewrite.java.migrate.guava.NoGuavaRefasterRecipes
4243
- org.openrewrite.java.migrate.guava.NoGuavaMapsNewHashMap
@@ -184,13 +185,6 @@ recipeList:
184185
methodPattern: com.google.common.base.Predicates not(com.google.common.base.Predicate)
185186
fullyQualifiedTargetTypeName: java.util.function.Predicate
186187

187-
- org.openrewrite.java.ChangeMethodName:
188-
methodPattern: com.google.common.base.Predicates equalTo(..)
189-
newMethodName: isEqual
190-
- org.openrewrite.java.ChangeMethodTargetToStatic:
191-
methodPattern: com.google.common.base.Predicates isEqual(..)
192-
fullyQualifiedTargetTypeName: java.util.function.Predicate
193-
194188
- org.openrewrite.java.ChangeMethodName:
195189
methodPattern: com.google.common.base.Predicate apply(..)
196190
newMethodName: test

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

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -257,29 +257,4 @@ class Test {
257257
)
258258
);
259259
}
260-
261-
@Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/903")
262-
@Test
263-
void doNotConvertPredicatesEqualToWhenUsedInMethodChain() {
264-
// Converting Predicates.equalTo() to Predicate.isEqual() breaks type inference when chained with .and()
265-
// The issue is that Predicate.isEqual() returns Predicate<? super T> which causes compilation errors
266-
// when chained with other methods
267-
rewriteRun(
268-
//language=java
269-
java(
270-
"""
271-
import com.google.common.base.Predicate;
272-
import com.google.common.base.Predicates;
273-
import java.util.Collection;
274-
275-
class A {
276-
public static Predicate<Collection<String>> combinedPredicate(Collection<String> aCollection) {
277-
Predicate<Collection<String>> anotherPredicate = c -> !c.isEmpty();
278-
return Predicates.and(Predicates.equalTo(aCollection), anotherPredicate);
279-
}
280-
}
281-
"""
282-
)
283-
);
284-
}
285260
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Copyright 2025 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.Issue;
22+
import org.openrewrite.java.JavaParser;
23+
import org.openrewrite.test.RecipeSpec;
24+
import org.openrewrite.test.RewriteTest;
25+
26+
import static org.openrewrite.java.Assertions.java;
27+
28+
class NoGuavaPredicatesEqualToTest implements RewriteTest {
29+
@Override
30+
public void defaults(RecipeSpec spec) {
31+
spec
32+
.recipeFromResources("org.openrewrite.java.migrate.guava.NoGuava")
33+
.parser(JavaParser.fromJavaVersion().classpathFromResources(new InMemoryExecutionContext(), "guava"));
34+
}
35+
36+
@DocumentExample
37+
@Test
38+
void predicatesEqualToToPredicateIsEqual() {
39+
rewriteRun(
40+
//language=java
41+
java(
42+
"""
43+
import com.google.common.base.Predicate;
44+
import com.google.common.base.Predicates;
45+
46+
class A {
47+
public static Predicate<String> isHelloPredicate() {
48+
return Predicates.equalTo("hello");
49+
}
50+
}
51+
""",
52+
"""
53+
import java.util.function.Predicate;
54+
55+
class A {
56+
public static Predicate<String> isHelloPredicate() {
57+
return Predicate.isEqual("hello");
58+
}
59+
}
60+
"""
61+
)
62+
);
63+
}
64+
65+
@Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/903")
66+
@Test
67+
void inlinedPredicatesEqualToToPredicateIsEqual() {
68+
rewriteRun(
69+
//language=java
70+
java(
71+
"""
72+
import com.google.common.base.Predicates;
73+
import com.google.common.base.Predicate;
74+
75+
import java.util.Collection;
76+
77+
class Test {
78+
public static void test(Collection<String> aCollection, Predicate<Collection<String>> anotherPredicate) {
79+
Predicate<Collection<String>> combined = Predicates.and(Predicates.equalTo(aCollection), anotherPredicate);
80+
}
81+
}
82+
""",
83+
"""
84+
import java.util.Collection;
85+
import java.util.function.Predicate;
86+
87+
class Test {
88+
public static void test(Collection<String> aCollection, Predicate<Collection<String>> anotherPredicate) {
89+
Predicate<Collection<String>> combined = Predicate.<Collection<String>>isEqual(aCollection).and(anotherPredicate);
90+
}
91+
}
92+
"""
93+
)
94+
);
95+
}
96+
}

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

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -100,32 +100,4 @@ public static Predicate<String> notEmptyPredicate() {
100100
)
101101
);
102102
}
103-
104-
@Test
105-
void predicatesEqualToToPredicateIsEqual() {
106-
rewriteRun(
107-
//language=java
108-
java(
109-
"""
110-
import com.google.common.base.Predicate;
111-
import com.google.common.base.Predicates;
112-
113-
class A {
114-
public static Predicate<String> isHelloPredicate() {
115-
return Predicates.equalTo("hello");
116-
}
117-
}
118-
""",
119-
"""
120-
import java.util.function.Predicate;
121-
122-
class A {
123-
public static Predicate<String> isHelloPredicate() {
124-
return Predicate.isEqual("hello");
125-
}
126-
}
127-
"""
128-
)
129-
);
130-
}
131103
}

0 commit comments

Comments
 (0)