-
Notifications
You must be signed in to change notification settings - Fork 119
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
Changes from 4 commits
56a67de
4190e42
0aad424
d54bc22
4e55073
0d4df6f
c90bfe6
b2e62c8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| /* | ||
| * 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.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)"); | ||
|
|
||
| @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() { | ||
| return Preconditions.check( | ||
| new UsesMethod<>("com.google.common.collect.Sets filter(java.util.Set, com.google.common.base.Predicate)"), | ||
| new JavaIsoVisitor<ExecutionContext>() { | ||
| @Override | ||
| public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { | ||
| if (SETS_FILTER.matches(method)) { | ||
| maybeRemoveImport("com.google.common.base.Predicate"); | ||
| maybeRemoveImport("com.google.common.collect.Sets"); | ||
| 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())") | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's a slight concern here where
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're right. I added a case for that. |
||
| .imports("java.util.stream.Collectors") | ||
| .build() | ||
| .apply(getCursor(), | ||
| method.getCoordinates().replace(), | ||
| method.getArguments().get(0), | ||
| method.getArguments().get(1)); | ||
| } | ||
| return super.visitMethodInvocation(method, ctx); | ||
| } | ||
| } | ||
| ); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| /* | ||
| * 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.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()); | ||
| } | ||
| } | ||
| """ | ||
| ) | ||
| ); | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.