Skip to content

Commit b70cb61

Browse files
lredortimtebeek
andauthored
Only change Guava Predicate.apply to test when not using filter or similar methods (#909)
* Add tests for inconsistency around Predicate and its method This commit adds a test, in PreferJavaUtilPredicateTest, to reveal the inconsistency between the conversion of `com.google.common.base.Predicate` and its `apply` method. The test is similar to `NoGuavaPredicateTest.doNotChangeWhenUsingCollectionsFilter()`, and as a consequence, the method must remain unchanged. Bug: #908 * Only change from `apply` to `test` when not using any `filter` methods * Fix faulty conflict resolve --------- Co-authored-by: Tim te Beek <[email protected]>
1 parent f4a574d commit b70cb61

File tree

4 files changed

+53
-15
lines changed

4 files changed

+53
-15
lines changed

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

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,8 @@
1515
*/
1616
package org.openrewrite.java.migrate.guava;
1717

18-
import org.openrewrite.ExecutionContext;
19-
import org.openrewrite.Preconditions;
20-
import org.openrewrite.Recipe;
21-
import org.openrewrite.TreeVisitor;
18+
import org.openrewrite.*;
19+
import org.openrewrite.java.ChangeMethodName;
2220
import org.openrewrite.java.ChangeType;
2321
import org.openrewrite.java.JavaIsoVisitor;
2422
import org.openrewrite.java.MethodMatcher;
@@ -42,11 +40,23 @@ public String getDescription() {
4240
public TreeVisitor<?, ExecutionContext> getVisitor() {
4341
return Preconditions.check(
4442
Preconditions.not(new UsesPredicateMethod<>()),
45-
new ChangeType(
46-
"com.google.common.base.Predicate",
47-
"java.util.function.Predicate",
48-
false)
49-
.getVisitor()
43+
new TreeVisitor<Tree, ExecutionContext>() {
44+
@Override
45+
public Tree preVisit(Tree tree, ExecutionContext ctx) {
46+
Tree t = tree;
47+
t = new ChangeMethodName(
48+
"com.google.common.base.Predicate apply(..)",
49+
"test",
50+
true,
51+
false
52+
).getVisitor().visitNonNull(t, ctx, getCursor().getParentOrThrow());
53+
return new ChangeType(
54+
"com.google.common.base.Predicate",
55+
"java.util.function.Predicate",
56+
false
57+
).getVisitor().visitNonNull(t, ctx, getCursor().getParentOrThrow());
58+
}
59+
}
5060
);
5161
}
5262

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -184,11 +184,6 @@ recipeList:
184184
- org.openrewrite.java.ChangeMethodTargetToStatic:
185185
methodPattern: com.google.common.base.Predicates not(com.google.common.base.Predicate)
186186
fullyQualifiedTargetTypeName: java.util.function.Predicate
187-
188-
- org.openrewrite.java.ChangeMethodName:
189-
methodPattern: com.google.common.base.Predicate apply(..)
190-
newMethodName: test
191-
matchOverrides: true
192187
- org.openrewrite.java.migrate.guava.NoGuavaPredicate
193188

194189
---

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,11 @@ void doNotChangeWhenUsingCollectionsFilter() {
183183
import java.util.Collection;
184184
185185
class Test {
186-
Predicate<String> notEmpty = s -> !s.isEmpty();
186+
Predicate<String> notEmpty = new Predicate<String>() {
187+
@Override public boolean apply(String s) {
188+
return !s.isEmpty();
189+
}
190+
};
187191
188192
public Collection<String> filterCollection(Collection<String> input) {
189193
return Collections2.filter(input, notEmpty);

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.junit.jupiter.api.Test;
1919
import org.openrewrite.DocumentExample;
2020
import org.openrewrite.InMemoryExecutionContext;
21+
import org.openrewrite.Issue;
2122
import org.openrewrite.java.JavaParser;
2223
import org.openrewrite.test.RecipeSpec;
2324
import org.openrewrite.test.RewriteTest;
@@ -100,4 +101,32 @@ public static Predicate<String> notEmptyPredicate() {
100101
)
101102
);
102103
}
104+
105+
@Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/899")
106+
@Test
107+
void doNotChangeWhenUsingCollectionsFilter() {
108+
// Collections2.filter requires Guava Predicate as last parameter
109+
rewriteRun(
110+
//language=java
111+
java(
112+
"""
113+
import com.google.common.base.Predicate;
114+
import com.google.common.collect.Collections2;
115+
import java.util.Collection;
116+
117+
class Test {
118+
Predicate<String> notEmpty = new Predicate<String>() {
119+
@Override public boolean apply(String s) {
120+
return !s.isEmpty();
121+
}
122+
};
123+
124+
public Collection<String> filterCollection(Collection<String> input) {
125+
return Collections2.filter(input, notEmpty);
126+
}
127+
}
128+
"""
129+
)
130+
);
131+
}
103132
}

0 commit comments

Comments
 (0)