Skip to content

Commit

Permalink
Replace !Optional.isPresent() with Optional.isEmpty() on Java 11+ (#130)
Browse files Browse the repository at this point in the history
* 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
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 0 deletions.
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;
}
};
}
}
1 change: 1 addition & 0 deletions src/main/resources/META-INF/rewrite/java8-to-java11.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,4 @@ recipeList:
- org.openrewrite.java.migrate.jacoco.UpgradeJaCoCoMavenPluginVersion
- org.openrewrite.java.migrate.JavaVersion11
- org.openrewrite.java.migrate.util.JavaUtilAPIs
- org.openrewrite.java.migrate.util.OptionalNotPresentToIsEmpty
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));
}

}

0 comments on commit 767d48d

Please sign in to comment.