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,80 @@
/*
* 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 org.openrewrite.java.tree.TypeUtils;

import java.util.Set;

import static java.util.Collections.singleton;

public class NoGuavaCollections2Transform extends Recipe {
private static final MethodMatcher COLLECTIONS2_TRANSFORM = new MethodMatcher("com.google.common.collect.Collections2 transform(java.util.Collection, com.google.common.base.Function)");

@Override
public String getDisplayName() {
return "Prefer `Collection.stream().map(Function)` over `Collections2.transform`";
}

@Override
public String getDescription() {
return "Prefer `Collection.stream().map(Function)` over `Collections2.transform(Collection, Function)`.";
}

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

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

if (TypeUtils.isAssignableTo("java.util.Collection", method.getArguments().get(0).getType())) {
return JavaTemplate.builder("#{any(java.util.Collection)}.stream().map(#{any(java.util.function.Function)}).collect(Collectors.toList())")
.imports("java.util.stream.Collectors")
.build()
.apply(getCursor(),
method.getCoordinates().replace(),
method.getArguments().get(0),
method.getArguments().get(1));
}
return method;
}
return super.visitMethodInvocation(method, ctx);
}
}
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* 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 org.openrewrite.java.tree.TypeUtils;

import java.util.Set;

import static java.util.Collections.singleton;

public class NoGuavaIterablesTransform extends Recipe {
private static final MethodMatcher ITERABLES_TRANSFORM = new MethodMatcher("com.google.common.collect.Iterables transform(java.lang.Iterable, com.google.common.base.Function)");

@Override
public String getDisplayName() {
return "Prefer `Collection.stream().map(Function)` over `Iterables.transform`";
}

@Override
public String getDescription() {
return "Prefer `Collection.stream().map(Function)` over `Iterables.transform(Collection, Function)`.";
}

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

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

if (TypeUtils.isAssignableTo("java.util.Collection", method.getArguments().get(0).getType())) {
return JavaTemplate.builder("#{any(java.util.Collection)}.stream().map(#{any(java.util.function.Function)}).collect(Collectors.toList())")
.imports("java.util.stream.Collectors")
.build()
.apply(getCursor(),
method.getCoordinates().replace(),
method.getArguments().get(0),
method.getArguments().get(1));
}
return method;
}
return super.visitMethodInvocation(method, ctx);
}
}
);
}
}
2 changes: 2 additions & 0 deletions src/main/resources/META-INF/rewrite/no-guava.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ recipeList:
- org.openrewrite.java.migrate.guava.NoGuavaFunctionsCompose
- org.openrewrite.java.migrate.guava.NoGuavaInlineMeMethods
- org.openrewrite.java.migrate.guava.NoGuavaIterablesAnyFilter
- org.openrewrite.java.migrate.guava.NoGuavaIterablesTransform
- org.openrewrite.java.migrate.guava.NoGuavaCollections2Transform
- org.openrewrite.java.migrate.guava.NoGuavaListsNewArrayList
- org.openrewrite.java.migrate.guava.NoGuavaListsNewCopyOnWriteArrayList
- org.openrewrite.java.migrate.guava.NoGuavaListsNewLinkedList
Expand Down
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 NoGuavaCollections2TransformTest implements RewriteTest {

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

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

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

class Test {
Iterable<Integer> test(Collection<String> collection, Function<String, Integer> toSize) {
return Collections2.transform(collection, toSize);
}
}
""",
"""
import java.util.Collection;
import java.util.stream.Collectors;

import com.google.common.base.Function;

class Test {
Iterable<Integer> test(Collection<String> collection, Function<String, Integer> toSize) {
return collection.stream().map(toSize).collect(Collectors.toList());
}
}
"""
)
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* 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 NoGuavaIterablesTransformTest implements RewriteTest {

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

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

import com.google.common.base.Function;
import com.google.common.collect.Iterables;

class Test {
Iterable<Integer> test(Collection<String> collection, Function<String, Integer> toSize) {
return Iterables.transform(collection, toSize);
}
}
""",
"""
import java.util.Collection;
import java.util.stream.Collectors;

import com.google.common.base.Function;

class Test {
Iterable<Integer> test(Collection<String> collection, Function<String, Integer> toSize) {
return collection.stream().map(toSize).collect(Collectors.toList());
}
}
"""
)
);
}

@Test
void doNotReplaceIterablesTransformWithIterable() {
//language=java
rewriteRun(
java(
"""
import com.google.common.base.Function;
import com.google.common.collect.Iterables;

class Test {
Iterable<Integer> test(Iterable<String> iterable, Function<String, Integer> toSize) {
return Iterables.transform(iterable, toSize);
}
}
"""
)
);
}
}