Skip to content

Commit d11a123

Browse files
committed
[DRAFT] Convert Guava Predicates.instanceOf
Trye to fix issue #918, but without success.
1 parent 46d115e commit d11a123

File tree

3 files changed

+142
-0
lines changed

3 files changed

+142
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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 NoGuavaPredicatesInstanceOf extends Recipe {
35+
private static final MethodMatcher PREDICATES_INSTANCE_OF = new MethodMatcher("com.google.common.base.Predicates instanceOf(..)");
36+
37+
@Override
38+
public String getDisplayName() {
39+
return "Prefer `ASpecificClass.class::isInstance`";
40+
}
41+
42+
@Override
43+
public String getDescription() {
44+
return "Prefer `ASpecificClass.class::isInstance` over `Predicates.instanceOf(ASpecificClass.class)`.";
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_INSTANCE_OF),
56+
new JavaIsoVisitor<ExecutionContext>() {
57+
@Override
58+
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
59+
if (PREDICATES_INSTANCE_OF.matches(method)) {
60+
maybeRemoveImport("com.google.common.base.Predicates");
61+
62+
return JavaTemplate.builder("#{any()}::isInstance")
63+
.build()
64+
.apply(getCursor(),
65+
method.getCoordinates().replace(),
66+
method.getArguments().get(0));
67+
}
68+
return super.visitMethodInvocation(method, ctx);
69+
}
70+
}
71+
);
72+
}
73+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ recipeList:
4040
- org.openrewrite.java.migrate.guava.NoGuavaMapsNewTreeMap
4141
- org.openrewrite.java.migrate.guava.NoGuavaPredicatesAndOr
4242
- org.openrewrite.java.migrate.guava.NoGuavaPredicatesEqualTo
43+
- org.openrewrite.java.migrate.guava.NoGuavaPredicatesInstanceOf
4344
- org.openrewrite.java.migrate.guava.NoGuavaPrimitiveAsList
4445
- org.openrewrite.java.migrate.guava.NoGuavaRefasterRecipes
4546
- org.openrewrite.java.migrate.guava.NoGuavaMapsNewHashMap
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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 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+
//language=java
41+
java(
42+
"""
43+
import java.util.Collection;
44+
45+
import com.google.common.base.Predicates;
46+
import com.google.common.collect.Iterables;
47+
48+
class Test {
49+
boolean test(Collection<Object> collection) {
50+
return Iterables.all(collection, Predicates.instanceOf(String.class));
51+
}
52+
}
53+
""",
54+
"""
55+
import java.util.Collection;
56+
57+
import com.google.common.collect.Iterables;
58+
59+
class Test {
60+
boolean test(Collection<Object> collection) {
61+
return Iterables.all(collection, String.class::isInstance);
62+
}
63+
}
64+
"""
65+
)
66+
);
67+
}
68+
}

0 commit comments

Comments
 (0)