Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
public class NoGuavaPredicatesAndOr extends Recipe {
private static final MethodMatcher PREDICATES_AND = new MethodMatcher("com.google.common.base.Predicates and(..)");
private static final MethodMatcher PREDICATES_OR = new MethodMatcher("com.google.common.base.Predicates or(..)");
private static final MethodMatcher PREDICATES_EQUAL_TO = new MethodMatcher("com.google.common.base.Predicates equalTo(..)");

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

// Avoid generic type issues by not making any changes just yet
if (result instanceof J.MethodInvocation) {
if (result instanceof J.MethodInvocation && !PREDICATES_EQUAL_TO.matches(result)) {
return method;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Copyright 2025 the original author or authors.
* <p>
* Licensed under the Moderne Source Available License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://docs.moderne.io/licensing/moderne-source-available-license
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.java.migrate.guava;

import org.openrewrite.ExecutionContext;
import org.openrewrite.Preconditions;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.JavaTemplate;
import org.openrewrite.java.MethodMatcher;
import org.openrewrite.java.ShortenFullyQualifiedTypeReferences;
import org.openrewrite.java.search.UsesMethod;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaType;

import java.util.Set;

import static java.util.Collections.singleton;

public class NoGuavaPredicatesEqualTo extends Recipe {
private static final MethodMatcher PREDICATES_EQUAL_TO = new MethodMatcher("com.google.common.base.Predicates equalTo(..)");

@Override
public String getDisplayName() {
return "Prefer `Predicate.isEqual(Object)`";
}

@Override
public String getDescription() {
return "Prefer `Predicate.isEqual(Object)` over `Predicates.equalTo(Object)`.";
}

@Override
public Set<String> getTags() {
return singleton("guava");
}

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return Preconditions.check(
new UsesMethod<>(PREDICATES_EQUAL_TO),
new JavaIsoVisitor<ExecutionContext>() {
@Override
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
if (PREDICATES_EQUAL_TO.matches(method)) {
maybeRemoveImport("com.google.common.base.Predicates");
maybeAddImport("java.util.function.Predicate");

if (method.getMethodType().getParameterTypes().get(0) instanceof JavaType.Parameterized) {
String typeString = method.getArguments().get(0).getType().toString();
J.MethodInvocation genericMethod = JavaTemplate.builder("Predicate.<" + typeString + ">isEqual(#{any(java.lang.Object)})")
.imports("java.util.function.Predicate")
.build()
.apply(getCursor(),
method.getCoordinates().replace(),
method.getArguments().get(0));
doAfterVisit(ShortenFullyQualifiedTypeReferences.modifyOnly(genericMethod));
return genericMethod;
}
// Fallback is not type is found.
return JavaTemplate.builder("Predicate.isEqual(#{any(java.lang.Object)})")
.imports("java.util.function.Predicate")
.build()
.apply(getCursor(),
method.getCoordinates().replace(),
method.getArguments().get(0));
}
return super.visitMethodInvocation(method, ctx);
}
}
);
}
}
8 changes: 1 addition & 7 deletions src/main/resources/META-INF/rewrite/no-guava.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ recipeList:
- org.openrewrite.java.migrate.guava.NoGuavaListsNewLinkedList
- org.openrewrite.java.migrate.guava.NoGuavaMapsNewTreeMap
- org.openrewrite.java.migrate.guava.NoGuavaPredicatesAndOr
- org.openrewrite.java.migrate.guava.NoGuavaPredicatesEqualTo
- org.openrewrite.java.migrate.guava.NoGuavaPrimitiveAsList
- org.openrewrite.java.migrate.guava.NoGuavaRefasterRecipes
- org.openrewrite.java.migrate.guava.NoGuavaMapsNewHashMap
Expand Down Expand Up @@ -184,13 +185,6 @@ recipeList:
methodPattern: com.google.common.base.Predicates not(com.google.common.base.Predicate)
fullyQualifiedTargetTypeName: java.util.function.Predicate

- org.openrewrite.java.ChangeMethodName:
methodPattern: com.google.common.base.Predicates equalTo(..)
newMethodName: isEqual
- org.openrewrite.java.ChangeMethodTargetToStatic:
methodPattern: com.google.common.base.Predicates isEqual(..)
fullyQualifiedTargetTypeName: java.util.function.Predicate

- org.openrewrite.java.ChangeMethodName:
methodPattern: com.google.common.base.Predicate apply(..)
newMethodName: test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* Copyright 2025 the original author or authors.
* <p>
* Licensed under the Moderne Source Available License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://docs.moderne.io/licensing/moderne-source-available-license
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.java.migrate.guava;

import org.junit.jupiter.api.Test;
import org.openrewrite.DocumentExample;
import org.openrewrite.InMemoryExecutionContext;
import org.openrewrite.Issue;
import org.openrewrite.java.JavaParser;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

import static org.openrewrite.java.Assertions.java;

class NoGuavaPredicatesEqualToTest implements RewriteTest {
@Override
public void defaults(RecipeSpec spec) {
spec
.recipeFromResources("org.openrewrite.java.migrate.guava.NoGuava")
.parser(JavaParser.fromJavaVersion().classpathFromResources(new InMemoryExecutionContext(), "guava"));
}

@DocumentExample
@Test
void predicatesEqualToToPredicateIsEqual() {
rewriteRun(
//language=java
java(
"""
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;

class A {
public static Predicate<String> isHelloPredicate() {
return Predicates.equalTo("hello");
}
}
""",
"""
import java.util.function.Predicate;

class A {
public static Predicate<String> isHelloPredicate() {
return Predicate.isEqual("hello");
}
}
"""
)
);
}

@Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/903")
@Test
void inlinedPredicatesEqualToToPredicateIsEqual() {
rewriteRun(
//language=java
java(
"""
import com.google.common.base.Predicates;
import com.google.common.base.Predicate;

import java.util.Collection;

class Test {
public static void test(Collection<String> aCollection, Predicate<Collection<String>> anotherPredicate) {
Predicate<Collection<String>> combined = Predicates.and(Predicates.equalTo(aCollection), anotherPredicate);
}
}
""",
"""
import java.util.Collection;
import java.util.function.Predicate;

class Test {
public static void test(Collection<String> aCollection, Predicate<Collection<String>> anotherPredicate) {
Predicate<Collection<String>> combined = Predicate.<Collection<String>>isEqual(aCollection).and(anotherPredicate);
}
}
"""
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,32 +100,4 @@ public static Predicate<String> notEmptyPredicate() {
)
);
}

@Test
void predicatesEqualToToPredicateIsEqual() {
rewriteRun(
//language=java
java(
"""
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;

class A {
public static Predicate<String> isHelloPredicate() {
return Predicates.equalTo("hello");
}
}
""",
"""
import java.util.function.Predicate;

class A {
public static Predicate<String> isHelloPredicate() {
return Predicate.isEqual("hello");
}
}
"""
)
);
}
}
Loading