Skip to content

Commit d805cb7

Browse files
committed
polish OptionalNotPresentToIsEmpty
1 parent 767d48d commit d805cb7

File tree

2 files changed

+3
-2
lines changed

2 files changed

+3
-2
lines changed

src/main/java/org/openrewrite/java/migrate/util/OptionalNotPresentToIsEmpty.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ protected TreeVisitor<?, ExecutionContext> getApplicableTest() {
5959

6060
@Override
6161
protected TreeVisitor<?, ExecutionContext> getVisitor() {
62+
final MethodMatcher optionalIsPresentMatcher = new MethodMatcher(JAVA_UTIL_OPTIONAL_IS_PRESENT);
6263
return new JavaVisitor<ExecutionContext>() {
6364
@Override
6465
public Statement visitStatement(Statement s, ExecutionContext p) {
@@ -69,7 +70,7 @@ public Statement visitStatement(Statement s, ExecutionContext p) {
6970
Expression expression = unary.getExpression();
7071
if (expression instanceof J.MethodInvocation) {
7172
J.MethodInvocation methodInvocation = (J.MethodInvocation) expression;
72-
if (new MethodMatcher(JAVA_UTIL_OPTIONAL_IS_PRESENT).matches(methodInvocation)) {
73+
if (optionalIsPresentMatcher.matches(methodInvocation)) {
7374
return statement.withTemplate(
7475
JavaTemplate.builder(this::getCursor, "#{any()}.isEmpty()").build(),
7576
statement.getCoordinates().replace(),

src/test/java/org/openrewrite/java/migrate/util/OptionalNotPresentToIsEmptyTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ boolean notPresent(Optional<String> bar){
4949
return bar.isEmpty();
5050
}
5151
}"""),
52-
17));
52+
11));
5353
}
5454

5555
}

0 commit comments

Comments
 (0)