-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace !Optional.isPresent() with Optional.isEmpty() on Java 11+ (#130)
* Replace !Optional.isPresent() with Optional.isEmpty() on Java 11+ * Add headers * Extract constant & inline JavaTemplate
- Loading branch information
Tim te Beek
authored
Oct 13, 2022
1 parent
7baf264
commit 767d48d
Showing
3 changed files
with
141 additions
and
0 deletions.
There are no files selected for viewing
85 changes: 85 additions & 0 deletions
85
src/main/java/org/openrewrite/java/migrate/util/OptionalNotPresentToIsEmpty.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* Copyright 2022 the original author or authors. | ||
* <p> | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* <p> | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.openrewrite.java.migrate.util; | ||
|
||
import org.openrewrite.Applicability; | ||
import org.openrewrite.ExecutionContext; | ||
import org.openrewrite.Recipe; | ||
import org.openrewrite.TreeVisitor; | ||
import org.openrewrite.java.JavaTemplate; | ||
import org.openrewrite.java.JavaVisitor; | ||
import org.openrewrite.java.MethodMatcher; | ||
import org.openrewrite.java.search.UsesJavaVersion; | ||
import org.openrewrite.java.search.UsesMethod; | ||
import org.openrewrite.java.tree.Expression; | ||
import org.openrewrite.java.tree.J; | ||
import org.openrewrite.java.tree.J.Unary.Type; | ||
import org.openrewrite.java.tree.Statement; | ||
|
||
import java.time.Duration; | ||
|
||
public class OptionalNotPresentToIsEmpty extends Recipe { | ||
|
||
private static final String JAVA_UTIL_OPTIONAL_IS_PRESENT = "java.util.Optional isPresent()"; | ||
|
||
@Override | ||
public String getDisplayName() { | ||
return "Replace !optional.isPresent() with optional.isEmpty()"; | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return "Replace negated Optional.isPresent() calls with Optional.isEmpty() in Java 11 and above."; | ||
} | ||
|
||
@Override | ||
public Duration getEstimatedEffortPerOccurrence() { | ||
return Duration.ofMinutes(1); | ||
} | ||
|
||
@Override | ||
protected TreeVisitor<?, ExecutionContext> getApplicableTest() { | ||
return Applicability.and( | ||
new UsesJavaVersion<>(11), | ||
new UsesMethod<>(JAVA_UTIL_OPTIONAL_IS_PRESENT)); | ||
} | ||
|
||
@Override | ||
protected TreeVisitor<?, ExecutionContext> getVisitor() { | ||
return new JavaVisitor<ExecutionContext>() { | ||
@Override | ||
public Statement visitStatement(Statement s, ExecutionContext p) { | ||
Statement statement = (Statement) super.visitStatement(s, p); | ||
if (statement instanceof J.Unary) { | ||
J.Unary unary = (J.Unary) statement; | ||
if (unary.getOperator() == Type.Not) { | ||
Expression expression = unary.getExpression(); | ||
if (expression instanceof J.MethodInvocation) { | ||
J.MethodInvocation methodInvocation = (J.MethodInvocation) expression; | ||
if (new MethodMatcher(JAVA_UTIL_OPTIONAL_IS_PRESENT).matches(methodInvocation)) { | ||
return statement.withTemplate( | ||
JavaTemplate.builder(this::getCursor, "#{any()}.isEmpty()").build(), | ||
statement.getCoordinates().replace(), | ||
methodInvocation.getSelect()); | ||
} | ||
} | ||
} | ||
} | ||
return statement; | ||
} | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
src/test/java/org/openrewrite/java/migrate/util/OptionalNotPresentToIsEmptyTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Copyright 2022 the original author or authors. | ||
* <p> | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* <p> | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.openrewrite.java.migrate.util; | ||
|
||
import org.openrewrite.java.JavaParser; | ||
import org.openrewrite.test.RecipeSpec; | ||
import org.openrewrite.test.RewriteTest; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.openrewrite.java.Assertions.java; | ||
import static org.openrewrite.java.Assertions.version; | ||
|
||
class OptionalNotPresentToIsEmptyTest implements RewriteTest { | ||
|
||
@Override | ||
public void defaults(RecipeSpec spec) { | ||
spec.recipe(new OptionalNotPresentToIsEmpty()); | ||
spec.parser(JavaParser.fromJavaVersion().logCompilationWarningsAndErrors(true)); | ||
} | ||
|
||
@Test | ||
void should_replace_not_isPresent_with_isEmpty() { | ||
rewriteRun(version(java(""" | ||
package com.example.app; | ||
import java.util.Optional; | ||
class App { | ||
boolean notPresent(Optional<String> bar){ | ||
return !bar.isPresent(); | ||
} | ||
}""", """ | ||
package com.example.app; | ||
import java.util.Optional; | ||
class App { | ||
boolean notPresent(Optional<String> bar){ | ||
return bar.isEmpty(); | ||
} | ||
}"""), | ||
17)); | ||
} | ||
|
||
} |