Skip to content
Merged
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 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 NoGuavaFunctionsCompose extends Recipe {
private static final MethodMatcher FUNCTIONS_COMPOSE = new MethodMatcher("com.google.common.base.Functions compose(com.google.common.base.Function, com.google.common.base.Function)");

@Override
public String getDisplayName() {
return "Prefer `Function.compose(Function)`";
}

@Override
public String getDescription() {
return "Prefer `Function.compose(Function)` over `Functions.compose(Function, Function)`.";
}

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

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

return JavaTemplate.builder("#{any(java.util.function.Function)}.compose(#{any(java.util.function.Function)})")
.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 @@ -29,6 +29,7 @@ recipeList:
- org.openrewrite.java.migrate.guava.NoGuavaJava21
- org.openrewrite.java.migrate.guava.NoGuavaCreateTempDir
- org.openrewrite.java.migrate.guava.NoGuavaDirectExecutor
- org.openrewrite.java.migrate.guava.NoGuavaFunctionsCompose
- org.openrewrite.java.migrate.guava.NoGuavaInlineMeMethods
- org.openrewrite.java.migrate.guava.NoGuavaIterablesAnyFilter
- org.openrewrite.java.migrate.guava.NoGuavaListsNewArrayList
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* 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 NoGuavaFunctionsComposeTest implements RewriteTest {

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

@DocumentExample
@Test
void replaceFunctionsCompose() {
//language=java
rewriteRun(
java(
"""
import com.google.common.base.Function;
import com.google.common.base.Functions;

class Test {
public static void test() {
Function<Object, Integer> composed = Functions.compose(new Function<String, Integer>() {
@Override
public Integer apply(String input) {
return input.length();
}
}, new Function<Object, String>() {
@Override
public String apply(Object input) {
return input.toString();
}
});
}
}
""",
"""
import com.google.common.base.Function;

class Test {
public static void test() {
Function<Object, Integer> composed = new Function<String, Integer>() {
@Override
public Integer apply(String input) {
return input.length();
}
}.compose(new Function<Object, String>() {
@Override
public String apply(Object input) {
return input.toString();
}
});
}
}
"""
)
);
}
}