Skip to content

Commit 8b6a577

Browse files
committed
Convert Guava Iterables.transform and Collections2.transform
1 parent 83af53b commit 8b6a577

File tree

5 files changed

+314
-0
lines changed

5 files changed

+314
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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+
import org.openrewrite.java.tree.TypeUtils;
28+
29+
import java.util.Set;
30+
31+
import static java.util.Collections.singleton;
32+
33+
public class NoGuavaCollections2Transform extends Recipe {
34+
private static final MethodMatcher COLLECTIONS2_TRANSFORM = new MethodMatcher("com.google.common.collect.Collections2 transform(java.util.Collection, com.google.common.base.Function)");
35+
36+
@Override
37+
public String getDisplayName() {
38+
return "Prefer `Collection.stream().transform(Function)`";
39+
}
40+
41+
@Override
42+
public String getDescription() {
43+
return "Prefer `Collection.stream().transform(Function)` over `Collections2.transform(Collection, Function)`.";
44+
}
45+
46+
@Override
47+
public Set<String> getTags() {
48+
return singleton("guava");
49+
}
50+
51+
@Override
52+
public TreeVisitor<?, ExecutionContext> getVisitor() {
53+
return Preconditions.check(
54+
new UsesMethod<>(COLLECTIONS2_TRANSFORM),
55+
new JavaIsoVisitor<ExecutionContext>() {
56+
@Override
57+
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
58+
if (COLLECTIONS2_TRANSFORM.matches(method)) {
59+
maybeRemoveImport("com.google.common.base.Function");
60+
maybeRemoveImport("com.google.common.collect.Collections2");
61+
maybeAddImport("java.util.function.Function");
62+
63+
if (TypeUtils.isAssignableTo("java.util.Collection", method.getArguments().get(0).getType())) {
64+
return JavaTemplate.builder("#{any(java.util.Collection)}.stream().map(#{any(java.util.function.Function)}).toList()")
65+
.build()
66+
.apply(getCursor(),
67+
method.getCoordinates().replace(),
68+
method.getArguments().get(0),
69+
method.getArguments().get(1));
70+
}
71+
return method;
72+
}
73+
return super.visitMethodInvocation(method, ctx);
74+
}
75+
}
76+
);
77+
}
78+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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.JavaVisitor;
25+
import org.openrewrite.java.MethodMatcher;
26+
import org.openrewrite.java.search.UsesMethod;
27+
import org.openrewrite.java.tree.J;
28+
import org.openrewrite.java.tree.TypeUtils;
29+
30+
import java.util.Set;
31+
32+
import static java.util.Collections.singleton;
33+
34+
public class NoGuavaIterablesTransform extends Recipe {
35+
private static final MethodMatcher ITERABLES_TRANSFORM = new MethodMatcher("com.google.common.collect.Iterables transform(java.lang.Iterable, com.google.common.base.Function)");
36+
37+
@Override
38+
public String getDisplayName() {
39+
return "Prefer `Collection.stream().transform(Function)`";
40+
}
41+
42+
@Override
43+
public String getDescription() {
44+
return "Prefer `Collection.stream().transform(Function)` over `Iterables.transform(Collection, Function)`.";
45+
}
46+
47+
@Override
48+
public Set<String> getTags() {
49+
return singleton("guava");
50+
}
51+
52+
@Override
53+
public TreeVisitor<?, ExecutionContext> getVisitor() {
54+
return Preconditions.check(
55+
new UsesMethod<>(ITERABLES_TRANSFORM),
56+
new JavaIsoVisitor<ExecutionContext>() {
57+
@Override
58+
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
59+
if (ITERABLES_TRANSFORM.matches(method)) {
60+
maybeRemoveImport("com.google.common.base.Function");
61+
maybeRemoveImport("com.google.common.collect.Iterables");
62+
maybeAddImport("java.util.function.Function");
63+
64+
if (TypeUtils.isAssignableTo("java.util.Collection", method.getArguments().get(0).getType())) {
65+
return JavaTemplate.builder("#{any(java.util.Collection)}.stream().map(#{any(java.util.function.Function)}).toList()")
66+
.build()
67+
.apply(getCursor(),
68+
method.getCoordinates().replace(),
69+
method.getArguments().get(0),
70+
method.getArguments().get(1));
71+
}
72+
return method;
73+
}
74+
return super.visitMethodInvocation(method, ctx);
75+
}
76+
}
77+
);
78+
}
79+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ recipeList:
3232
- org.openrewrite.java.migrate.guava.NoGuavaFunctionsCompose
3333
- org.openrewrite.java.migrate.guava.NoGuavaInlineMeMethods
3434
- org.openrewrite.java.migrate.guava.NoGuavaIterablesAnyFilter
35+
- org.openrewrite.java.migrate.guava.NoGuavaIterablesTransform
36+
- org.openrewrite.java.migrate.guava.NoGuavaCollections2Transform
3537
- org.openrewrite.java.migrate.guava.NoGuavaListsNewArrayList
3638
- org.openrewrite.java.migrate.guava.NoGuavaListsNewCopyOnWriteArrayList
3739
- org.openrewrite.java.migrate.guava.NoGuavaListsNewLinkedList
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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 NoGuavaCollections2TransformTest implements RewriteTest {
28+
29+
@Override
30+
public void defaults(RecipeSpec spec) {
31+
spec
32+
.recipe(new NoGuavaCollections2Transform())
33+
.parser(JavaParser.fromJavaVersion().classpathFromResources(new InMemoryExecutionContext(), "guava"));
34+
}
35+
36+
@DocumentExample
37+
@Test
38+
void replaceCollections2Transform() {
39+
//language=java
40+
rewriteRun(
41+
java(
42+
"""
43+
import java.util.Collection;
44+
45+
import com.google.common.base.Function;
46+
import com.google.common.collect.Collections2;
47+
48+
class Test {
49+
Iterable<Integer> test(Collection<String> collection, Function<String, Integer> toSize) {
50+
return Collections2.transform(collection, toSize);
51+
}
52+
}
53+
""",
54+
"""
55+
import java.util.Collection;
56+
57+
import com.google.common.base.Function;
58+
59+
class Test {
60+
Iterable<Integer> test(Collection<String> collection, Function<String, Integer> toSize) {
61+
return collection.stream().map(toSize).toList();
62+
}
63+
}
64+
"""
65+
)
66+
);
67+
}
68+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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 NoGuavaIterablesTransformTest implements RewriteTest {
28+
29+
@Override
30+
public void defaults(RecipeSpec spec) {
31+
spec
32+
.recipe(new NoGuavaIterablesTransform())
33+
.parser(JavaParser.fromJavaVersion().classpathFromResources(new InMemoryExecutionContext(), "guava"));
34+
}
35+
36+
@DocumentExample
37+
@Test
38+
void replaceIterablesTransform() {
39+
//language=java
40+
rewriteRun(
41+
java(
42+
"""
43+
import java.util.Collection;
44+
45+
import com.google.common.base.Function;
46+
import com.google.common.collect.Iterables;
47+
48+
class Test {
49+
Iterable<Integer> test(Collection<String> collection, Function<String, Integer> toSize) {
50+
return Iterables.transform(collection, toSize);
51+
}
52+
}
53+
""",
54+
"""
55+
import java.util.Collection;
56+
57+
import com.google.common.base.Function;
58+
59+
class Test {
60+
Iterable<Integer> test(Collection<String> collection, Function<String, Integer> toSize) {
61+
return collection.stream().map(toSize).toList();
62+
}
63+
}
64+
"""
65+
)
66+
);
67+
}
68+
69+
@Test
70+
void doNotReplaceIterablesTransformWithIterable() {
71+
//language=java
72+
rewriteRun(
73+
java(
74+
"""
75+
import com.google.common.base.Function;
76+
import com.google.common.collect.Iterables;
77+
78+
class Test {
79+
Iterable<Integer> test(Iterable<String> iterable, Function<String, Integer> toSize) {
80+
return Iterables.transform(iterable, toSize);
81+
}
82+
}
83+
"""
84+
)
85+
);
86+
}
87+
}

0 commit comments

Comments
 (0)