Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright 2024 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 NoGuavaCollections2Filter extends Recipe {
private static final MethodMatcher COLLECTIONS2_FILTER = new MethodMatcher("com.google.common.collect.Collections2 filter(java.util.Collection, 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 `Collections2.filter(Collection, Predicate)`.";
}

@Override
public Set<String> getTags() {
return singleton("guava");
}

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return Preconditions.check(
new UsesMethod<>(COLLECTIONS2_FILTER),
new JavaIsoVisitor<ExecutionContext>() {
@Override
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
if (COLLECTIONS2_FILTER.matches(method)) {
maybeRemoveImport("com.google.common.base.Predicate");
maybeRemoveImport("com.google.common.collect.Collections2");
maybeAddImport("java.util.function.Predicate");

return JavaTemplate.builder("#{any(java.util.Collection)}.stream().filter(#{any(java.util.function.Predicate)}).toList()")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure these are as interchangeable ; Collections2.filter returns a com.google.common.collect.Collections2.FilteredCollection which acts as a view on the underlying collection, whereas .toList() returns a fully new collection with very different performance and semantics when adding/removing entries. Any thoughts there?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You’re certainly right (about the performance), but the current Guava recipes cause compilation errors, as explained in the description of issue #899.
To respect the “do no harm” principle, one option might be to conditionally replace com.google.common.base.Predicate with java.util.function.Predicate.
However, this seems a bit more complex — probably too complex for me at this stage.

I probably thought a bit too quickly when creating my latest issues and PRs, aiming to eliminate all compilation errors and remove any remaining Guava dependencies.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes understandable that you're looking for either a more complete migration off of Guava (with no compilation issues), or a more cautious move off of Guava's Predicate onto Java's Predicate. We have something similar when choosing to move off of more specific Collections methods onto their interfaces, as seen in this recipe:
https://github.com/openrewrite/rewrite-static-analysis/blob/da756464470f9c5dbc056fa2e008123d73a50e05/src/main/java/org/openrewrite/staticanalysis/UseCollectionInterfaces.java#L277-L297

Perhaps good to also/instead add similar handling before we change Predicate: look how it's used, and only make changes when safe to do so.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either way appreciate the help both in developing recipes, and thinking through the options. 🙏🏻

.build()
.apply(getCursor(),
method.getCoordinates().replace(),
method.getArguments().get(0),
method.getArguments().get(1));
}
return super.visitMethodInvocation(method, ctx);
}
}
);
}
}
1 change: 1 addition & 0 deletions src/main/resources/META-INF/rewrite/no-guava.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ tags:
recipeList:
- org.openrewrite.java.migrate.guava.NoGuavaJava11
- org.openrewrite.java.migrate.guava.NoGuavaJava21
- org.openrewrite.java.migrate.guava.NoGuavaCollections2Filter
- org.openrewrite.java.migrate.guava.NoGuavaCreateTempDir
- org.openrewrite.java.migrate.guava.NoGuavaDirectExecutor
- org.openrewrite.java.migrate.guava.NoGuavaInlineMeMethods
Expand Down
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.java.JavaParser;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

import static org.openrewrite.java.Assertions.java;

class NoGuavaCollections2FilterTest implements RewriteTest {

@Override
public void defaults(RecipeSpec spec) {
spec
.recipe(new NoGuavaCollections2Filter())
.parser(JavaParser.fromJavaVersion().classpathFromResources(new InMemoryExecutionContext(), "guava"));
}

@DocumentExample
@Test
void replaceCollections2Filter() {
//language=java
rewriteRun(
java(
"""
import java.util.Collection;

import com.google.common.base.Predicate;
import com.google.common.collect.Collections2;

class Test {
Collection<Object> test(Collection<Object> collection, Predicate<Object> isNotNull) {
return Collections2.filter(collection, isNotNull);
}
}
""",
"""
import java.util.Collection;

import com.google.common.base.Predicate;

class Test {
Collection<Object> test(Collection<Object> collection, Predicate<Object> isNotNull) {
return collection.stream().filter(isNotNull).toList();
}
}
"""
)
);
}
}