Skip to content

Commit 45569f9

Browse files
author
Tim te Beek
authored
Add OptionalNotEmptyToIsPresent as inverse of #130 (#132)
1 parent d805cb7 commit 45569f9

File tree

3 files changed

+142
-0
lines changed

3 files changed

+142
-0
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright 2022 the original author or authors.
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (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://www.apache.org/licenses/LICENSE-2.0
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.util;
17+
18+
import org.openrewrite.Applicability;
19+
import org.openrewrite.ExecutionContext;
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.UsesJavaVersion;
26+
import org.openrewrite.java.search.UsesMethod;
27+
import org.openrewrite.java.tree.Expression;
28+
import org.openrewrite.java.tree.J;
29+
import org.openrewrite.java.tree.J.Unary.Type;
30+
import org.openrewrite.java.tree.Statement;
31+
32+
import java.time.Duration;
33+
34+
public class OptionalNotEmptyToIsPresent extends Recipe {
35+
36+
private static final String JAVA_UTIL_OPTIONAL_IS_EMPTY = "java.util.Optional isEmpty()";
37+
38+
@Override
39+
public String getDisplayName() {
40+
return "Replace !optional.isEmpty() with optional.isPresent()";
41+
}
42+
43+
@Override
44+
public String getDescription() {
45+
return "Replace negated Optional.isEmpty() calls with Optional.isPresent() in Java 11 and above.";
46+
}
47+
48+
@Override
49+
public Duration getEstimatedEffortPerOccurrence() {
50+
return Duration.ofMinutes(1);
51+
}
52+
53+
@Override
54+
protected TreeVisitor<?, ExecutionContext> getApplicableTest() {
55+
return Applicability.and(
56+
new UsesJavaVersion<>(11),
57+
new UsesMethod<>(JAVA_UTIL_OPTIONAL_IS_EMPTY));
58+
}
59+
60+
@Override
61+
protected TreeVisitor<?, ExecutionContext> getVisitor() {
62+
final MethodMatcher optionalIsPresentMatcher = new MethodMatcher(JAVA_UTIL_OPTIONAL_IS_EMPTY);
63+
return new JavaVisitor<ExecutionContext>() {
64+
@Override
65+
public Statement visitStatement(Statement s, ExecutionContext p) {
66+
Statement statement = (Statement) super.visitStatement(s, p);
67+
if (statement instanceof J.Unary) {
68+
J.Unary unary = (J.Unary) statement;
69+
if (unary.getOperator() == Type.Not) {
70+
Expression expression = unary.getExpression();
71+
if (expression instanceof J.MethodInvocation) {
72+
J.MethodInvocation methodInvocation = (J.MethodInvocation) expression;
73+
if (optionalIsPresentMatcher.matches(methodInvocation)) {
74+
return statement.withTemplate(
75+
JavaTemplate.builder(this::getCursor, "#{any()}.isPresent()").build(),
76+
statement.getCoordinates().replace(),
77+
methodInvocation.getSelect());
78+
}
79+
}
80+
}
81+
}
82+
return statement;
83+
}
84+
};
85+
}
86+
}

src/main/resources/META-INF/rewrite/java8-to-java11.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,4 @@ recipeList:
5050
- org.openrewrite.java.migrate.JavaVersion11
5151
- org.openrewrite.java.migrate.util.JavaUtilAPIs
5252
- org.openrewrite.java.migrate.util.OptionalNotPresentToIsEmpty
53+
- org.openrewrite.java.migrate.util.OptionalNotEmptyToIsPresent
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2022 the original author or authors.
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (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://www.apache.org/licenses/LICENSE-2.0
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.util;
17+
18+
import org.openrewrite.java.JavaParser;
19+
import org.openrewrite.test.RecipeSpec;
20+
import org.openrewrite.test.RewriteTest;
21+
22+
import org.junit.jupiter.api.Test;
23+
24+
import static org.openrewrite.java.Assertions.java;
25+
import static org.openrewrite.java.Assertions.version;
26+
27+
class OptionalNotEmptyToIsPresentTest implements RewriteTest {
28+
29+
@Override
30+
public void defaults(RecipeSpec spec) {
31+
spec.recipe(new OptionalNotEmptyToIsPresent());
32+
spec.parser(JavaParser.fromJavaVersion().logCompilationWarningsAndErrors(true));
33+
}
34+
35+
@Test
36+
void should_replace_not_isEmpty_with_isPresent() {
37+
rewriteRun(version(java("""
38+
package com.example.app;
39+
import java.util.Optional;
40+
class App {
41+
boolean notPresent(Optional<String> bar){
42+
return !bar.isEmpty();
43+
}
44+
}""", """
45+
package com.example.app;
46+
import java.util.Optional;
47+
class App {
48+
boolean notPresent(Optional<String> bar){
49+
return bar.isPresent();
50+
}
51+
}"""),
52+
11));
53+
}
54+
55+
}

0 commit comments

Comments
 (0)