Skip to content

Commit ae77730

Browse files
committed
Prefer Optional.stream().collect(toSet()) over Optional.asSet()
1 parent a53e8d1 commit ae77730

File tree

3 files changed

+72
-1
lines changed

3 files changed

+72
-1
lines changed
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/resources/META-INF/rewrite/no-guava.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ recipeList:
153153
- org.openrewrite.java.migrate.guava.PreferJavaUtilOptionalOrElseNull
154154
- org.openrewrite.java.migrate.guava.NoGuavaOptionalFromJavaUtil
155155
- org.openrewrite.java.migrate.guava.NoGuavaOptionalToJavaUtil
156+
- org.openrewrite.java.migrate.guava.NoGuavaOptionalAsSet
156157
- org.openrewrite.java.ChangeMethodName:
157158
methodPattern: com.google.common.base.Optional absent()
158159
newMethodName: empty

src/test/java/org/openrewrite/java/migrate/guava/PreferJavaUtilOptionalTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,13 +324,14 @@ String foo(Optional<String> optional) {
324324
);
325325
}
326326

327-
@ExpectedToFail("Not yet implemented")
328327
@Test
329328
void asSetToStreamCollectToSet() {
330329
// Comparison to java.util.Optional: this method has no equivalent in Java 8's Optional class. However, some use cases can be written with calls to optional.stream().
331330
//language=java
332331
rewriteRun(java(
333332
"""
333+
import java.util.Set;
334+
334335
import com.google.common.base.Optional;
335336
336337
class A {

0 commit comments

Comments
 (0)