Skip to content

Commit c0b3bb7

Browse files
authored
Merge branch 'main' into only-convert-guava-predicate-when-not-used-in-method
2 parents dba3f1b + fd556ee commit c0b3bb7

File tree

5 files changed

+386
-0
lines changed

5 files changed

+386
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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.search.UsesMethod;
26+
import org.openrewrite.java.tree.J;
27+
28+
import java.util.Set;
29+
30+
import static java.util.Collections.singleton;
31+
32+
public class NoGuavaFunctionsCompose extends Recipe {
33+
private static final MethodMatcher FUNCTIONS_COMPOSE = new MethodMatcher("com.google.common.base.Functions compose(com.google.common.base.Function, com.google.common.base.Function)");
34+
35+
@Override
36+
public String getDisplayName() {
37+
return "Prefer `Function.compose(Function)`";
38+
}
39+
40+
@Override
41+
public String getDescription() {
42+
return "Prefer `Function.compose(Function)` over `Functions.compose(Function, Function)`.";
43+
}
44+
45+
@Override
46+
public Set<String> getTags() {
47+
return singleton("guava");
48+
}
49+
50+
@Override
51+
public TreeVisitor<?, ExecutionContext> getVisitor() {
52+
return Preconditions.check(
53+
new UsesMethod<>(FUNCTIONS_COMPOSE),
54+
new JavaIsoVisitor<ExecutionContext>() {
55+
@Override
56+
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
57+
if (FUNCTIONS_COMPOSE.matches(method)) {
58+
maybeRemoveImport("com.google.common.base.Function");
59+
maybeRemoveImport("com.google.common.base.Functions");
60+
maybeAddImport("java.util.function.Function");
61+
62+
return JavaTemplate.builder("#{any(java.util.function.Function)}.compose(#{any(java.util.function.Function)})")
63+
.build()
64+
.apply(getCursor(),
65+
method.getCoordinates().replace(),
66+
method.getArguments().get(0),
67+
method.getArguments().get(1));
68+
}
69+
return super.visitMethodInvocation(method, ctx);
70+
}
71+
}
72+
);
73+
}
74+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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.JavaTemplate;
23+
import org.openrewrite.java.JavaVisitor;
24+
import org.openrewrite.java.MethodMatcher;
25+
import org.openrewrite.java.search.UsesMethod;
26+
import org.openrewrite.java.tree.J;
27+
28+
import java.util.Set;
29+
30+
import static java.util.Collections.singleton;
31+
32+
public class NoGuavaSetsFilter extends Recipe {
33+
private static final MethodMatcher SETS_FILTER = new MethodMatcher("com.google.common.collect.Sets filter(java.util.Set, com.google.common.base.Predicate)");
34+
private static final MethodMatcher SETS_FILTER_SORTED_SET = new MethodMatcher("com.google.common.collect.Sets filter(java.util.SortedSet, com.google.common.base.Predicate)");
35+
36+
@Override
37+
public String getDisplayName() {
38+
return "Prefer `Collection.stream().filter(Predicate)`";
39+
}
40+
41+
@Override
42+
public String getDescription() {
43+
return "Prefer `Collection.stream().filter(Predicate)` over `Sets.filter(Set, Predicate)`.";
44+
}
45+
46+
@Override
47+
public Set<String> getTags() {
48+
return singleton("guava");
49+
}
50+
51+
@Override
52+
public TreeVisitor<?, ExecutionContext> getVisitor() {
53+
TreeVisitor<?, ExecutionContext> precondition = Preconditions.or(new UsesMethod<>(SETS_FILTER), new UsesMethod<>(SETS_FILTER_SORTED_SET));
54+
return Preconditions.check(precondition, new JavaVisitor<ExecutionContext>() {
55+
@Override
56+
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
57+
if (SETS_FILTER_SORTED_SET.matches(method)) {
58+
maybeRemoveImport("com.google.common.base.Predicate");
59+
maybeRemoveImport("com.google.common.collect.Sets");
60+
maybeAddImport("java.util.TreeSet");
61+
maybeAddImport("java.util.function.Predicate");
62+
maybeAddImport("java.util.stream.Collectors");
63+
64+
return JavaTemplate.builder("#{any(java.util.Collection)}.stream().filter(#{any(java.util.function.Predicate)}).collect(Collectors.toCollection(TreeSet::new))")
65+
.imports("java.util.TreeSet")
66+
.imports("java.util.stream.Collectors")
67+
.build()
68+
.apply(getCursor(),
69+
method.getCoordinates().replace(),
70+
method.getArguments().get(0),
71+
method.getArguments().get(1));
72+
}
73+
if (SETS_FILTER.matches(method)) {
74+
maybeRemoveImport("com.google.common.base.Predicate");
75+
maybeRemoveImport("com.google.common.collect.Sets");
76+
maybeAddImport("java.util.TreeSet");
77+
maybeAddImport("java.util.function.Predicate");
78+
maybeAddImport("java.util.stream.Collectors");
79+
80+
return JavaTemplate.builder("#{any(java.util.Collection)}.stream().filter(#{any(java.util.function.Predicate)}).collect(Collectors.toSet())")
81+
.imports("java.util.stream.Collectors")
82+
.build()
83+
.apply(getCursor(),
84+
method.getCoordinates().replace(),
85+
method.getArguments().get(0),
86+
method.getArguments().get(1));
87+
}
88+
return super.visitMethodInvocation(method, ctx);
89+
}
90+
}
91+
);
92+
}
93+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ recipeList:
2929
- org.openrewrite.java.migrate.guava.NoGuavaJava21
3030
- org.openrewrite.java.migrate.guava.NoGuavaCreateTempDir
3131
- org.openrewrite.java.migrate.guava.NoGuavaDirectExecutor
32+
- org.openrewrite.java.migrate.guava.NoGuavaFunctionsCompose
3233
- org.openrewrite.java.migrate.guava.NoGuavaInlineMeMethods
3334
- org.openrewrite.java.migrate.guava.NoGuavaIterablesAnyFilter
3435
- org.openrewrite.java.migrate.guava.NoGuavaListsNewArrayList
@@ -39,6 +40,7 @@ recipeList:
3940
- org.openrewrite.java.migrate.guava.NoGuavaPrimitiveAsList
4041
- org.openrewrite.java.migrate.guava.NoGuavaRefasterRecipes
4142
- org.openrewrite.java.migrate.guava.NoGuavaMapsNewHashMap
43+
- org.openrewrite.java.migrate.guava.NoGuavaSetsFilter
4244
- org.openrewrite.java.migrate.guava.NoGuavaSetsNewHashSet
4345
- org.openrewrite.java.migrate.guava.NoGuavaSetsNewConcurrentHashSet
4446
- org.openrewrite.java.migrate.guava.NoGuavaSetsNewLinkedHashSet
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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.java.JavaParser;
22+
import org.openrewrite.test.RecipeSpec;
23+
import org.openrewrite.test.RewriteTest;
24+
25+
import static org.openrewrite.java.Assertions.java;
26+
27+
class NoGuavaFunctionsComposeTest implements RewriteTest {
28+
29+
@Override
30+
public void defaults(RecipeSpec spec) {
31+
spec
32+
.recipe(new NoGuavaFunctionsCompose())
33+
.parser(JavaParser.fromJavaVersion().classpathFromResources(new InMemoryExecutionContext(), "guava"));
34+
}
35+
36+
@DocumentExample
37+
@Test
38+
void replaceFunctionsCompose() {
39+
//language=java
40+
rewriteRun(
41+
java(
42+
"""
43+
import com.google.common.base.Function;
44+
import com.google.common.base.Functions;
45+
46+
class Test {
47+
public static void test() {
48+
Function<Object, Integer> composed = Functions.compose(new Function<String, Integer>() {
49+
@Override
50+
public Integer apply(String input) {
51+
return input.length();
52+
}
53+
}, new Function<Object, String>() {
54+
@Override
55+
public String apply(Object input) {
56+
return input.toString();
57+
}
58+
});
59+
}
60+
}
61+
""",
62+
"""
63+
import com.google.common.base.Function;
64+
65+
class Test {
66+
public static void test() {
67+
Function<Object, Integer> composed = new Function<String, Integer>() {
68+
@Override
69+
public Integer apply(String input) {
70+
return input.length();
71+
}
72+
}.compose(new Function<Object, String>() {
73+
@Override
74+
public String apply(Object input) {
75+
return input.toString();
76+
}
77+
});
78+
}
79+
}
80+
"""
81+
)
82+
);
83+
}
84+
}

0 commit comments

Comments
 (0)