Skip to content

Commit 213b8ab

Browse files
authored
Merge branch 'main' into patch-2
2 parents b1ce10b + 46d115e commit 213b8ab

File tree

10 files changed

+423
-30
lines changed

10 files changed

+423
-30
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+
}
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.openrewrite.ExecutionContext;
19+
import org.openrewrite.Preconditions;
20+
import org.openrewrite.Recipe;
21+
import org.openrewrite.TreeVisitor;
22+
import org.openrewrite.java.JavaTemplate;
23+
import org.openrewrite.java.JavaVisitor;
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 NoGuavaOptionalAsSet extends Recipe {
33+
private static final MethodMatcher OPTIONAL_AS_SET = new MethodMatcher("com.google.common.base.Optional asSet()");
34+
35+
@Override
36+
public String getDisplayName() {
37+
return "Prefer `Optional.stream().collect(Collectors.toSet())`";
38+
}
39+
40+
@Override
41+
public String getDescription() {
42+
return "Prefer `Optional.stream().collect(Collectors.toSet())` over `Optional.asSet()`.";
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(new UsesMethod<>(OPTIONAL_AS_SET), new JavaVisitor<ExecutionContext>() {
53+
@Override
54+
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
55+
if (OPTIONAL_AS_SET.matches(method)) {
56+
maybeAddImport("java.util.stream.Collectors");
57+
return JavaTemplate.builder("#{any(java.util.Optional)}.stream().collect(Collectors.toSet())")
58+
.imports("java.util.stream.Collectors")
59+
.build()
60+
.apply(getCursor(),
61+
method.getCoordinates().replace(),
62+
method.getSelect());
63+
}
64+
return super.visitMethodInvocation(method, ctx);
65+
}
66+
}
67+
);
68+
}
69+
}

src/main/java/org/openrewrite/java/migrate/guava/NoGuavaPredicate.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,20 @@ public String getDescription() {
4040
public TreeVisitor<?, ExecutionContext> getVisitor() {
4141
return Preconditions.check(
4242
Preconditions.not(new UsesPredicateMethod<>()),
43-
new TreeVisitor<Tree, ExecutionContext>() {
43+
new JavaIsoVisitor<ExecutionContext>() {
4444
@Override
45-
public Tree preVisit(Tree tree, ExecutionContext ctx) {
46-
Tree t = tree;
47-
t = new ChangeMethodName(
45+
public J preVisit(J java, ExecutionContext ctx) {
46+
J j = (J) new ChangeMethodName(
4847
"com.google.common.base.Predicate apply(..)",
4948
"test",
5049
true,
5150
false
52-
).getVisitor().visitNonNull(t, ctx, getCursor().getParentOrThrow());
53-
return new ChangeType(
51+
).getVisitor().visitNonNull(java, ctx, getCursor().getParentOrThrow());
52+
return (J) new ChangeType(
5453
"com.google.common.base.Predicate",
5554
"java.util.function.Predicate",
5655
false
57-
).getVisitor().visitNonNull(t, ctx, getCursor().getParentOrThrow());
56+
).getVisitor().visitNonNull(j, ctx, getCursor().getParentOrThrow());
5857
}
5958
}
6059
);

src/main/java/org/openrewrite/java/migrate/lombok/LombokValueToRecord.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -372,15 +372,21 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration cd, Execution
372372
classDeclaration = new RemoveAnnotationVisitor(LOMBOK_VALUE_MATCHER).visitClassDeclaration(classDeclaration, ctx);
373373
maybeRemoveImport("lombok.Value");
374374

375+
List<J.Modifier> mappedModifiers = ListUtils.map(classDeclaration.getModifiers(), modifier -> {
376+
J.Modifier.Type type = modifier.getType();
377+
if (type == J.Modifier.Type.Static || type == J.Modifier.Type.Final) {
378+
return null;
379+
}
380+
return modifier;
381+
});
382+
J.ClassDeclaration.Kind kind = classDeclaration.withKind(J.ClassDeclaration.Kind.Type.Record).getPadding().getKind();
383+
if (mappedModifiers.isEmpty()) {
384+
kind = kind.withPrefix(kind.getPrefix().withWhitespace(""));
385+
}
386+
375387
classDeclaration = classDeclaration
376-
.withKind(J.ClassDeclaration.Kind.Type.Record)
377-
.withModifiers(ListUtils.map(classDeclaration.getModifiers(), modifier -> {
378-
J.Modifier.Type type = modifier.getType();
379-
if (type == J.Modifier.Type.Static || type == J.Modifier.Type.Final) {
380-
return null;
381-
}
382-
return modifier;
383-
}))
388+
.getPadding().withKind(kind)
389+
.withModifiers(mappedModifiers)
384390
.withType(buildRecordType(classDeclaration))
385391
.withBody(classDeclaration.getBody()
386392
.withStatements(bodyStatements)

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

Lines changed: 3 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
@@ -153,6 +155,7 @@ recipeList:
153155
- org.openrewrite.java.migrate.guava.PreferJavaUtilOptionalOrElseNull
154156
- org.openrewrite.java.migrate.guava.NoGuavaOptionalFromJavaUtil
155157
- org.openrewrite.java.migrate.guava.NoGuavaOptionalToJavaUtil
158+
- org.openrewrite.java.migrate.guava.NoGuavaOptionalAsSet
156159
- org.openrewrite.java.ChangeMethodName:
157160
methodPattern: com.google.common.base.Optional absent()
158161
newMethodName: empty
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+
}

0 commit comments

Comments
 (0)