Skip to content

Commit d214489

Browse files
lredortimtebeek
andauthored
Convert Guava Functions.compose (#906)
* Convert Guava `Functions.compose` * Reuse MethodMatcher in UsesMethod precondition --------- Co-authored-by: Tim te Beek <[email protected]>
1 parent 808ddc1 commit d214489

File tree

3 files changed

+159
-0
lines changed

3 files changed

+159
-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+
}

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

Lines changed: 1 addition & 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
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)