|
| 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 | +} |
0 commit comments