Skip to content

Commit 46d115e

Browse files
lredortimtebeekgithub-actions[bot]
authored
Convert Guava Iterables.transform and Collections2.transform (#917)
* Convert Guava `Iterables.transform` and `Collections2.transform` * Remove unused import Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Use `Collectors.toList()` while we support older Java versions still --------- Co-authored-by: Tim te Beek <[email protected]> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Tim te Beek <[email protected]>
1 parent 3263e03 commit 46d115e

File tree

5 files changed

+319
-0
lines changed

5 files changed

+319
-0
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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().map(Function)` over `Collections2.transform`";
39+
}
40+
41+
@Override
42+
public String getDescription() {
43+
return "Prefer `Collection.stream().map(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+
maybeAddImport("java.util.stream.Collectors");
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)}).collect(Collectors.toList())")
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+
return method;
74+
}
75+
return super.visitMethodInvocation(method, ctx);
76+
}
77+
}
78+
);
79+
}
80+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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 NoGuavaIterablesTransform extends Recipe {
34+
private static final MethodMatcher ITERABLES_TRANSFORM = new MethodMatcher("com.google.common.collect.Iterables transform(java.lang.Iterable, com.google.common.base.Function)");
35+
36+
@Override
37+
public String getDisplayName() {
38+
return "Prefer `Collection.stream().map(Function)` over `Iterables.transform`";
39+
}
40+
41+
@Override
42+
public String getDescription() {
43+
return "Prefer `Collection.stream().map(Function)` over `Iterables.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<>(ITERABLES_TRANSFORM),
55+
new JavaIsoVisitor<ExecutionContext>() {
56+
@Override
57+
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
58+
if (ITERABLES_TRANSFORM.matches(method)) {
59+
maybeRemoveImport("com.google.common.base.Function");
60+
maybeRemoveImport("com.google.common.collect.Iterables");
61+
maybeAddImport("java.util.function.Function");
62+
maybeAddImport("java.util.stream.Collectors");
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)}).collect(Collectors.toList())")
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+
return method;
74+
}
75+
return super.visitMethodInvocation(method, ctx);
76+
}
77+
}
78+
);
79+
}
80+
}

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: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
import java.util.stream.Collectors;
57+
58+
import com.google.common.base.Function;
59+
60+
class Test {
61+
Iterable<Integer> test(Collection<String> collection, Function<String, Integer> toSize) {
62+
return collection.stream().map(toSize).collect(Collectors.toList());
63+
}
64+
}
65+
"""
66+
)
67+
);
68+
}
69+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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+
import java.util.stream.Collectors;
57+
58+
import com.google.common.base.Function;
59+
60+
class Test {
61+
Iterable<Integer> test(Collection<String> collection, Function<String, Integer> toSize) {
62+
return collection.stream().map(toSize).collect(Collectors.toList());
63+
}
64+
}
65+
"""
66+
)
67+
);
68+
}
69+
70+
@Test
71+
void doNotReplaceIterablesTransformWithIterable() {
72+
//language=java
73+
rewriteRun(
74+
java(
75+
"""
76+
import com.google.common.base.Function;
77+
import com.google.common.collect.Iterables;
78+
79+
class Test {
80+
Iterable<Integer> test(Iterable<String> iterable, Function<String, Integer> toSize) {
81+
return Iterables.transform(iterable, toSize);
82+
}
83+
}
84+
"""
85+
)
86+
);
87+
}
88+
}

0 commit comments

Comments
 (0)