Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
@@ -0,0 +1,88 @@
/*
* Copyright 2024 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.JavaTemplate;
import org.openrewrite.java.JavaVisitor;
import org.openrewrite.java.MethodMatcher;
import org.openrewrite.java.search.UsesMethod;
import org.openrewrite.java.tree.J;

import java.util.Set;

import static java.util.Collections.singleton;

public class NoGuavaPredicatesAndOr extends Recipe {
private static final MethodMatcher PREDICATES_AND = new MethodMatcher("com.google.common.base.Predicates and(com.google.common.base.Predicate, com.google.common.base.Predicate)");
private static final MethodMatcher PREDICATES_OR = new MethodMatcher("com.google.common.base.Predicates or(com.google.common.base.Predicate, com.google.common.base.Predicate)");

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

@Override
public String getDescription() {
return "Prefer `Predicate.and(Predicate)` over `Predicates.and(Predicate, Predicate)`.";
}

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

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
TreeVisitor<?, ExecutionContext> precondition = Preconditions.or(new UsesMethod<>(PREDICATES_AND), new UsesMethod<>(PREDICATES_OR));
return Preconditions.check(precondition, new JavaVisitor<ExecutionContext>() {
@Override
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
if (PREDICATES_AND.matches(method)) {
maybeRemoveImport("com.google.common.base.Predicate");
maybeRemoveImport("com.google.common.base.Predicates");
maybeAddImport("java.util.function.Predicate");
if (method.getArguments().size() == 2) {
return JavaTemplate.builder("#{any(java.util.function.Predicate)}.and(#{any(java.util.function.Predicate)})")
.build()
.apply(getCursor(),
method.getCoordinates().replace(),
method.getArguments().get(0),
method.getArguments().get(1));
}
}
if (PREDICATES_OR.matches(method)) {
maybeRemoveImport("com.google.common.base.Predicate");
maybeRemoveImport("com.google.common.base.Predicates");
maybeAddImport("java.util.function.Predicate");
if (method.getArguments().size() == 2) {
return JavaTemplate.builder("#{any(java.util.function.Predicate)}.or(#{any(java.util.function.Predicate)})")
.build()
.apply(getCursor(),
method.getCoordinates().replace(),
method.getArguments().get(0),
method.getArguments().get(1));
}
}

return super.visitMethodInvocation(method, ctx);
}
});
}
}
1 change: 1 addition & 0 deletions src/main/resources/META-INF/rewrite/no-guava.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ recipeList:
- org.openrewrite.java.migrate.guava.NoGuavaListsNewCopyOnWriteArrayList
- org.openrewrite.java.migrate.guava.NoGuavaListsNewLinkedList
- org.openrewrite.java.migrate.guava.NoGuavaMapsNewTreeMap
- org.openrewrite.java.migrate.guava.NoGuavaPredicatesAndOr
- org.openrewrite.java.migrate.guava.NoGuavaPrimitiveAsList
- org.openrewrite.java.migrate.guava.NoGuavaRefasterRecipes
- org.openrewrite.java.migrate.guava.NoGuavaMapsNewHashMap
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/*
* Copyright 2024 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.java.JavaParser;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

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

class NoGuavaPredicatesAndOrTest implements RewriteTest {

@Override
public void defaults(RecipeSpec spec) {
spec
.recipe(new NoGuavaPredicatesAndOr())
.parser(JavaParser.fromJavaVersion().classpathFromResources(new InMemoryExecutionContext(), "guava"));
}

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

class Test {
Predicate<String> isNotNull = s -> s != null;
Predicate<String> isNotEmpty = s -> !s.isEmpty();
Predicate<String> combined = Predicates.and(isNotNull, isNotEmpty);
}
""",
"""
import com.google.common.base.Predicate;

class Test {
Predicate<String> isNotNull = s -> s != null;
Predicate<String> isNotEmpty = s -> !s.isEmpty();
Predicate<String> combined = isNotNull.and(isNotEmpty);
}
"""
)
);
}

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

class Test {
Predicate<String> isNotNull = s -> s != null;
Predicate<String> isNotEmpty = s -> !s.isEmpty();
Predicate<String> combined = Predicates.or(isNotNull, isNotEmpty);
}
""",
"""
import com.google.common.base.Predicate;

class Test {
Predicate<String> isNotNull = s -> s != null;
Predicate<String> isNotEmpty = s -> !s.isEmpty();
Predicate<String> combined = isNotNull.or(isNotEmpty);
}
"""
)
);
}

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

class Test {
Predicate<String> combined = Predicates.and(Objects::nonNull, s -> s.length() > 5);
}
""",
"""
import com.google.common.base.Predicate;
import java.util.Objects;

class Test {
Predicate<String> combined = Objects::nonNull.and(s -> s.length() > 5);
}
"""
)
);
}

@Test
void replacePredicatesAndNestedCalls() {
//language=java
rewriteRun(
spec -> spec.expectedCyclesThatMakeChanges(2),
java(
"""
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;

class Test {
Predicate<Integer> isPositive = n -> n > 0;
Predicate<Integer> isEven = n -> n % 2 == 0;
Predicate<Integer> isLessThan100 = n -> n < 100;
Predicate<Integer> combined = Predicates.and(isPositive, Predicates.and(isEven, isLessThan100));
}
""",
"""
import com.google.common.base.Predicate;

class Test {
Predicate<Integer> isPositive = n -> n > 0;
Predicate<Integer> isEven = n -> n % 2 == 0;
Predicate<Integer> isLessThan100 = n -> n < 100;
Predicate<Integer> combined = isPositive.and(isEven.and(isLessThan100));
}
"""
)
);
}
}