Skip to content

Commit 187bcbe

Browse files
lredortimtebeekgithub-actions[bot]
authored
Convert Guava Predicates.instanceOf (#919)
* [DRAFT] Convert Guava `Predicates.instanceOf` Trye to fix issue #918, but without success. * Apply suggestions from code review Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Apply suggestions from code review Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Use explicit constructors for now * Require a public no args constructor Fixes #933 --------- Co-authored-by: Tim te Beek <timtebeek@gmail.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Tim te Beek <tim@moderne.io>
1 parent f9c0ad4 commit 187bcbe

File tree

3 files changed

+154
-0
lines changed

3 files changed

+154
-0
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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.*;
19+
import org.openrewrite.java.JavaVisitor;
20+
import org.openrewrite.java.MethodMatcher;
21+
import org.openrewrite.java.search.UsesMethod;
22+
import org.openrewrite.java.tree.*;
23+
import org.openrewrite.marker.Markers;
24+
25+
import java.util.Set;
26+
27+
import static java.util.Collections.emptyList;
28+
import static java.util.Collections.singleton;
29+
30+
public class NoGuavaPredicatesInstanceOf extends Recipe {
31+
private static final MethodMatcher PREDICATES_INSTANCE_OF = new MethodMatcher("com.google.common.base.Predicates instanceOf(..)");
32+
33+
@Override
34+
public String getDisplayName() {
35+
return "Prefer `A.class::isInstance`";
36+
}
37+
38+
@Override
39+
public String getDescription() {
40+
return "Prefer `A.class::isInstance` over `Predicates.instanceOf(A.class)`.";
41+
}
42+
43+
@Override
44+
public Set<String> getTags() {
45+
return singleton("guava");
46+
}
47+
48+
@Override
49+
public TreeVisitor<?, ExecutionContext> getVisitor() {
50+
return Preconditions.check(
51+
new UsesMethod<>(PREDICATES_INSTANCE_OF),
52+
new JavaVisitor<ExecutionContext>() {
53+
@Override
54+
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
55+
if (PREDICATES_INSTANCE_OF.matches(method)) {
56+
maybeRemoveImport("com.google.common.base.Predicates");
57+
Expression clazz = method.getArguments().get(0);
58+
// XXX `JavaTemplate.builder("#{any()}::isInstance")` failed here
59+
// TODO Add type information for reference and identifier
60+
return new J.MemberReference(
61+
Tree.randomId(),
62+
method.getPrefix(),
63+
Markers.EMPTY,
64+
JRightPadded.build(clazz),
65+
null,
66+
JLeftPadded.build(new J.Identifier(
67+
Tree.randomId(),
68+
Space.EMPTY,
69+
Markers.EMPTY,
70+
emptyList(),
71+
"isInstance",
72+
null,
73+
null)),
74+
null,
75+
null,
76+
null);
77+
}
78+
return super.visitMethodInvocation(method, ctx);
79+
}
80+
}
81+
);
82+
}
83+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ recipeList:
4141
- org.openrewrite.java.migrate.guava.NoGuavaMapsNewTreeMap
4242
- org.openrewrite.java.migrate.guava.NoGuavaPredicatesAndOr
4343
- org.openrewrite.java.migrate.guava.NoGuavaPredicatesEqualTo
44+
- org.openrewrite.java.migrate.guava.NoGuavaPredicatesInstanceOf
4445
- org.openrewrite.java.migrate.guava.NoGuavaPrimitiveAsList
4546
- org.openrewrite.java.migrate.guava.NoGuavaRefasterRecipes
4647
- org.openrewrite.java.migrate.guava.NoGuavaMapsNewHashMap
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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.java.JavaParser;
22+
import org.openrewrite.test.RecipeSpec;
23+
import org.openrewrite.test.RewriteTest;
24+
import org.openrewrite.test.TypeValidation;
25+
26+
import static org.openrewrite.java.Assertions.java;
27+
28+
class NoGuavaPredicatesInstanceOfTest implements RewriteTest {
29+
@Override
30+
public void defaults(RecipeSpec spec) {
31+
spec
32+
.recipe(new NoGuavaPredicatesInstanceOf())
33+
.parser(JavaParser.fromJavaVersion().classpathFromResources(new InMemoryExecutionContext(), "guava"));
34+
}
35+
36+
@DocumentExample
37+
@Test
38+
void predicatesEqualToToPredicateIsEqual() {
39+
rewriteRun(
40+
// XXX Pending JavaTemplate support for MemberReference
41+
spec -> spec.afterTypeValidationOptions(TypeValidation.none()),
42+
//language=java
43+
java(
44+
"""
45+
import java.util.Collection;
46+
47+
import com.google.common.base.Predicates;
48+
import com.google.common.collect.Iterables;
49+
50+
class Test {
51+
boolean test(Collection<Object> collection) {
52+
return Iterables.all(collection, Predicates.instanceOf(String.class));
53+
}
54+
}
55+
""",
56+
"""
57+
import java.util.Collection;
58+
59+
import com.google.common.collect.Iterables;
60+
61+
class Test {
62+
boolean test(Collection<Object> collection) {
63+
return Iterables.all(collection, String.class::isInstance);
64+
}
65+
}
66+
"""
67+
)
68+
);
69+
}
70+
}

0 commit comments

Comments
 (0)