-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Tim te Beek
authored
Oct 13, 2022
1 parent
d805cb7
commit 45569f9
Showing
3 changed files
with
142 additions
and
0 deletions.
There are no files selected for viewing
86 changes: 86 additions & 0 deletions
86
src/main/java/org/openrewrite/java/migrate/util/OptionalNotEmptyToIsPresent.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,86 @@ | ||
/* | ||
* 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 OptionalNotEmptyToIsPresent extends Recipe { | ||
|
||
private static final String JAVA_UTIL_OPTIONAL_IS_EMPTY = "java.util.Optional isEmpty()"; | ||
|
||
@Override | ||
public String getDisplayName() { | ||
return "Replace !optional.isEmpty() with optional.isPresent()"; | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return "Replace negated Optional.isEmpty() calls with Optional.isPresent() 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_EMPTY)); | ||
} | ||
|
||
@Override | ||
protected TreeVisitor<?, ExecutionContext> getVisitor() { | ||
final MethodMatcher optionalIsPresentMatcher = new MethodMatcher(JAVA_UTIL_OPTIONAL_IS_EMPTY); | ||
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 (optionalIsPresentMatcher.matches(methodInvocation)) { | ||
return statement.withTemplate( | ||
JavaTemplate.builder(this::getCursor, "#{any()}.isPresent()").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/OptionalNotEmptyToIsPresentTest.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 OptionalNotEmptyToIsPresentTest implements RewriteTest { | ||
|
||
@Override | ||
public void defaults(RecipeSpec spec) { | ||
spec.recipe(new OptionalNotEmptyToIsPresent()); | ||
spec.parser(JavaParser.fromJavaVersion().logCompilationWarningsAndErrors(true)); | ||
} | ||
|
||
@Test | ||
void should_replace_not_isEmpty_with_isPresent() { | ||
rewriteRun(version(java(""" | ||
package com.example.app; | ||
import java.util.Optional; | ||
class App { | ||
boolean notPresent(Optional<String> bar){ | ||
return !bar.isEmpty(); | ||
} | ||
}""", """ | ||
package com.example.app; | ||
import java.util.Optional; | ||
class App { | ||
boolean notPresent(Optional<String> bar){ | ||
return bar.isPresent(); | ||
} | ||
}"""), | ||
11)); | ||
} | ||
|
||
} |