-
Notifications
You must be signed in to change notification settings - Fork 107
Convert Guava Predicates.instanceOf
#919
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
timtebeek
merged 7 commits into
openrewrite:main
from
lredor:lredor/bug/908-Predicates-instanceOf
Nov 12, 2025
+154
−0
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d11a123
[DRAFT] Convert Guava `Predicates.instanceOf`
lredor a1eaa6a
Apply suggestions from code review
timtebeek 16a6e7f
Merge branch 'main' into lredor/bug/908-Predicates-instanceOf
timtebeek 6296c10
Apply suggestions from code review
timtebeek 6cf0177
Use explicit constructors for now
timtebeek f1b2f2c
Merge branch 'main' into lredor/bug/908-Predicates-instanceOf
timtebeek b6c31ff
Require a public no args constructor
timtebeek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
73 changes: 73 additions & 0 deletions
73
src/main/java/org/openrewrite/java/migrate/guava/NoGuavaPredicatesInstanceOf.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| /* | ||
| * 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; | ||
timtebeek marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| import org.openrewrite.java.tree.JavaType; | ||
|
|
||
timtebeek marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| import java.util.Set; | ||
|
|
||
| import static java.util.Collections.singleton; | ||
|
|
||
| public class NoGuavaPredicatesInstanceOf extends Recipe { | ||
| private static final MethodMatcher PREDICATES_INSTANCE_OF = new MethodMatcher("com.google.common.base.Predicates instanceOf(..)"); | ||
|
|
||
| @Override | ||
| public String getDisplayName() { | ||
| return "Prefer `ASpecificClass.class::isInstance`"; | ||
| } | ||
|
|
||
| @Override | ||
| public String getDescription() { | ||
| return "Prefer `ASpecificClass.class::isInstance` over `Predicates.instanceOf(ASpecificClass.class)`."; | ||
| } | ||
|
|
||
| @Override | ||
| public Set<String> getTags() { | ||
| return singleton("guava"); | ||
| } | ||
|
|
||
| @Override | ||
| public TreeVisitor<?, ExecutionContext> getVisitor() { | ||
| return Preconditions.check( | ||
| new UsesMethod<>(PREDICATES_INSTANCE_OF), | ||
| new JavaIsoVisitor<ExecutionContext>() { | ||
| @Override | ||
| public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { | ||
| if (PREDICATES_INSTANCE_OF.matches(method)) { | ||
| maybeRemoveImport("com.google.common.base.Predicates"); | ||
|
|
||
| return JavaTemplate.builder("#{any()}::isInstance") | ||
| .build() | ||
| .apply(getCursor(), | ||
| method.getCoordinates().replace(), | ||
| method.getArguments().get(0)); | ||
| } | ||
| return super.visitMethodInvocation(method, ctx); | ||
| } | ||
| } | ||
| ); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
src/test/java/org/openrewrite/java/migrate/guava/NoGuavaPredicatesInstanceOfTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| /* | ||
| * 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; | ||
timtebeek marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| import org.openrewrite.java.JavaParser; | ||
| import org.openrewrite.test.RecipeSpec; | ||
| import org.openrewrite.test.RewriteTest; | ||
|
|
||
| import static org.openrewrite.java.Assertions.java; | ||
|
|
||
| class NoGuavaPredicatesInstanceOfTest implements RewriteTest { | ||
| @Override | ||
| public void defaults(RecipeSpec spec) { | ||
| spec | ||
| .recipe(new NoGuavaPredicatesInstanceOf()) | ||
| .parser(JavaParser.fromJavaVersion().classpathFromResources(new InMemoryExecutionContext(), "guava")); | ||
| } | ||
|
|
||
| @DocumentExample | ||
| @Test | ||
| void predicatesEqualToToPredicateIsEqual() { | ||
| rewriteRun( | ||
| //language=java | ||
| java( | ||
| """ | ||
| import java.util.Collection; | ||
|
|
||
| import com.google.common.base.Predicates; | ||
| import com.google.common.collect.Iterables; | ||
|
|
||
| class Test { | ||
| boolean test(Collection<Object> collection) { | ||
| return Iterables.all(collection, Predicates.instanceOf(String.class)); | ||
| } | ||
| } | ||
| """, | ||
| """ | ||
| import java.util.Collection; | ||
|
|
||
| import com.google.common.collect.Iterables; | ||
|
|
||
| class Test { | ||
| boolean test(Collection<Object> collection) { | ||
| return Iterables.all(collection, String.class::isInstance); | ||
| } | ||
| } | ||
| """ | ||
| ) | ||
| ); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.