-
Notifications
You must be signed in to change notification settings - Fork 110
Convert Guava Sets.filter
#898
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 8 commits into
openrewrite:main
from
lredor:lredor/eng/897-Sets_filter
Oct 26, 2025
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
56a67de
Convert Guava `Sets.filter`
lredor 4190e42
Merge branch 'main' into lredor/eng/897-Sets_filter
timtebeek 0aad424
Minimize irrelevant parts in test method setup
timtebeek d54bc22
Remove unused imports
timtebeek 4e55073
Reuse method matcher
timtebeek 0d4df6f
Add a specific case for SortedSet to preserve existing behavior
lredor c90bfe6
Also retain behavior for indirect use of sorted set
timtebeek b2e62c8
Document an unhandled edge case
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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
93 changes: 93 additions & 0 deletions
93
src/main/java/org/openrewrite/java/migrate/guava/NoGuavaSetsFilter.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,93 @@ | ||
| /* | ||
| * 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.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 NoGuavaSetsFilter extends Recipe { | ||
| private static final MethodMatcher SETS_FILTER = new MethodMatcher("com.google.common.collect.Sets filter(java.util.Set, com.google.common.base.Predicate)"); | ||
| private static final MethodMatcher SETS_FILTER_SORTED_SET = new MethodMatcher("com.google.common.collect.Sets filter(java.util.SortedSet, com.google.common.base.Predicate)"); | ||
|
|
||
| @Override | ||
| public String getDisplayName() { | ||
| return "Prefer `Collection.stream().filter(Predicate)`"; | ||
| } | ||
|
|
||
| @Override | ||
| public String getDescription() { | ||
| return "Prefer `Collection.stream().filter(Predicate)` over `Sets.filter(Set, Predicate)`."; | ||
| } | ||
|
|
||
| @Override | ||
| public Set<String> getTags() { | ||
| return singleton("guava"); | ||
| } | ||
|
|
||
| @Override | ||
| public TreeVisitor<?, ExecutionContext> getVisitor() { | ||
| TreeVisitor<?, ExecutionContext> precondition = Preconditions.or(new UsesMethod<>(SETS_FILTER), new UsesMethod<>(SETS_FILTER_SORTED_SET)); | ||
| return Preconditions.check(precondition, new JavaVisitor<ExecutionContext>() { | ||
| @Override | ||
| public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { | ||
| if (SETS_FILTER_SORTED_SET.matches(method)) { | ||
| maybeRemoveImport("com.google.common.base.Predicate"); | ||
| maybeRemoveImport("com.google.common.collect.Sets"); | ||
| maybeAddImport("java.util.TreeSet"); | ||
| maybeAddImport("java.util.function.Predicate"); | ||
| maybeAddImport("java.util.stream.Collectors"); | ||
|
|
||
| return JavaTemplate.builder("#{any(java.util.Collection)}.stream().filter(#{any(java.util.function.Predicate)}).collect(Collectors.toCollection(TreeSet::new))") | ||
| .imports("java.util.TreeSet") | ||
| .imports("java.util.stream.Collectors") | ||
| .build() | ||
| .apply(getCursor(), | ||
| method.getCoordinates().replace(), | ||
| method.getArguments().get(0), | ||
| method.getArguments().get(1)); | ||
| } | ||
| if (SETS_FILTER.matches(method)) { | ||
| maybeRemoveImport("com.google.common.base.Predicate"); | ||
| maybeRemoveImport("com.google.common.collect.Sets"); | ||
| maybeAddImport("java.util.TreeSet"); | ||
| maybeAddImport("java.util.function.Predicate"); | ||
| maybeAddImport("java.util.stream.Collectors"); | ||
|
|
||
| return JavaTemplate.builder("#{any(java.util.Collection)}.stream().filter(#{any(java.util.function.Predicate)}).collect(Collectors.toSet())") | ||
| .imports("java.util.stream.Collectors") | ||
| .build() | ||
| .apply(getCursor(), | ||
| method.getCoordinates().replace(), | ||
| method.getArguments().get(0), | ||
| method.getArguments().get(1)); | ||
| } | ||
| 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
133 changes: 133 additions & 0 deletions
133
src/test/java/org/openrewrite/java/migrate/guava/NoGuavaSetsFilterTest.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,133 @@ | ||
| /* | ||
| * 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.junitpioneer.jupiter.ExpectedToFail; | ||
| import org.junitpioneer.jupiter.Issue; | ||
| 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 NoGuavaSetsFilterTest implements RewriteTest { | ||
|
|
||
| @Override | ||
| public void defaults(RecipeSpec spec) { | ||
| spec | ||
| .recipe(new NoGuavaSetsFilter()) | ||
| .parser(JavaParser.fromJavaVersion().classpathFromResources(new InMemoryExecutionContext(), "guava")); | ||
| } | ||
|
|
||
| @DocumentExample | ||
| @Test | ||
| void replaceSetsFilter() { | ||
| //language=java | ||
| rewriteRun( | ||
| java( | ||
| """ | ||
| import java.util.Set; | ||
|
|
||
| import com.google.common.base.Predicate; | ||
| import com.google.common.collect.Sets; | ||
|
|
||
| class Test { | ||
| public static Set<Object> test(Set<Object> set, Predicate<Object> isNotNull) { | ||
| return Sets.filter(set, isNotNull); | ||
| } | ||
| } | ||
| """, | ||
| """ | ||
| import java.util.Set; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| import com.google.common.base.Predicate; | ||
|
|
||
| class Test { | ||
| public static Set<Object> test(Set<Object> set, Predicate<Object> isNotNull) { | ||
| return set.stream().filter(isNotNull).collect(Collectors.toSet()); | ||
| } | ||
| } | ||
| """ | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| @Test | ||
| void replaceSetsFilterUsingSortedSet() { | ||
| //language=java | ||
| rewriteRun( | ||
| java( | ||
| """ | ||
| import java.util.Set; | ||
| import java.util.SortedSet; | ||
|
|
||
| import com.google.common.base.Predicate; | ||
| import com.google.common.collect.Sets; | ||
|
|
||
| class Test { | ||
| public static Set<Object> test(SortedSet<Object> set, Predicate<Object> isNotNull) { | ||
| return Sets.filter(set, isNotNull); | ||
| } | ||
| } | ||
| """, | ||
| """ | ||
| import java.util.Set; | ||
| import java.util.SortedSet; | ||
| import java.util.TreeSet; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| import com.google.common.base.Predicate; | ||
|
|
||
| class Test { | ||
| public static Set<Object> test(SortedSet<Object> set, Predicate<Object> isNotNull) { | ||
| return set.stream().filter(isNotNull).collect(Collectors.toCollection(TreeSet::new)); | ||
| } | ||
| } | ||
| """ | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| @ExpectedToFail("Sets.filter has special handling for sorted sets which now gets lost") | ||
| @Issue("https://github.com/openrewrite/rewrite-migrate-java/pull/898#discussion_r2461706208") | ||
| @Test | ||
| void replaceSetsFilterIndirectlyUsingSortedSet() { | ||
| //language=java | ||
| rewriteRun( | ||
| java( | ||
| """ | ||
| import java.util.Set; | ||
| import java.util.SortedSet; | ||
|
|
||
| import com.google.common.base.Predicate; | ||
| import com.google.common.collect.Sets; | ||
|
|
||
| class Test { | ||
| public static Set<Object> test(SortedSet<Object> set, Predicate<Object> isNotNull) { | ||
| Set<Object> indirectSet = set; | ||
| return Sets.filter(indirectSet, isNotNull); | ||
| } | ||
| } | ||
| """ | ||
| // This now gets converted, but skips past a `instanceof SortedSet` check that keeps it a sorted set | ||
| ) | ||
| ); | ||
| } | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This case is tricky; we can't really deduce from the type system whether we'd end up in this conditional of special handling:
https://github.com/google/guava/blob/21cdcf6d9a49c1a498acb422471d8d71f9219865/guava/src/com/google/common/collect/Sets.java#L1190-L1194
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd hope that to be rare enough to perhaps still merge the current work, but there is a risk here which could only be mitigated by data flow analysis to figure out if it might be a SortedSet going into this
filter(Set, Predicate)method. 🤔There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Either way thanks for the quick rework; would love to hear your thoughts on this edge case.