|
| 1 | +/* |
| 2 | + * Copyright 2021 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.lombok; |
| 17 | + |
| 18 | +import lombok.AccessLevel; |
| 19 | +import lombok.EqualsAndHashCode; |
| 20 | +import lombok.Value; |
| 21 | +import org.openrewrite.ExecutionContext; |
| 22 | +import org.openrewrite.Recipe; |
| 23 | +import org.openrewrite.TreeVisitor; |
| 24 | +import org.openrewrite.java.JavaIsoVisitor; |
| 25 | +import org.openrewrite.java.JavaParser; |
| 26 | +import org.openrewrite.java.JavaTemplate; |
| 27 | +import org.openrewrite.java.tree.J; |
| 28 | + |
| 29 | +import java.util.HashSet; |
| 30 | +import java.util.Optional; |
| 31 | +import java.util.Set; |
| 32 | +import java.util.StringJoiner; |
| 33 | + |
| 34 | +import static java.util.Comparator.comparing; |
| 35 | +import static org.openrewrite.java.tree.JavaType.Variable; |
| 36 | + |
| 37 | +@Value |
| 38 | +@EqualsAndHashCode(callSuper = false) |
| 39 | +public class ConvertGetter extends Recipe { |
| 40 | + |
| 41 | + @Override |
| 42 | + public String getDisplayName() { |
| 43 | + //language=markdown |
| 44 | + return "Convert getter methods to annotations"; |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public String getDescription() { |
| 49 | + //language=markdown |
| 50 | + return new StringJoiner("\n") |
| 51 | + .add("Convert trivial getter methods to `@Getter` annotations on their respective fields.") |
| 52 | + .add("") |
| 53 | + .add("Limitations:") |
| 54 | + .add("") |
| 55 | + .add(" - Does not add a dependency to Lombok, users need to do that manually") |
| 56 | + .add(" - Ignores fields that are declared on the same line as others, e.g. `private int foo, bar;" + |
| 57 | + "Users who have such fields are advised to separate them beforehand with " + |
| 58 | + "[org.openrewrite.staticanalysis.MultipleVariableDeclaration]" + |
| 59 | + "(https://docs.openrewrite.org/recipes/staticanalysis/multiplevariabledeclarations).") |
| 60 | + .add(" - Does not offer any of the configuration keys listed in https://projectlombok.org/features/GetterSetter.") |
| 61 | + .toString(); |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public TreeVisitor<?, ExecutionContext> getVisitor() { |
| 66 | + return new MethodRemover(); |
| 67 | + } |
| 68 | + |
| 69 | + |
| 70 | + @Value |
| 71 | + @EqualsAndHashCode(callSuper = false) |
| 72 | + private static class MethodRemover extends JavaIsoVisitor<ExecutionContext> { |
| 73 | + private static final String FIELDS_TO_DECORATE_KEY = "FIELDS_TO_DECORATE"; |
| 74 | + |
| 75 | + @Override |
| 76 | + public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext ctx) { |
| 77 | + |
| 78 | + //initialize set of fields to annotate |
| 79 | + getCursor().putMessage(FIELDS_TO_DECORATE_KEY, new HashSet<Finding>()); |
| 80 | + |
| 81 | + //delete methods, note down corresponding fields |
| 82 | + J.ClassDeclaration classDeclAfterVisit = super.visitClassDeclaration(classDecl, ctx); |
| 83 | + |
| 84 | + //only thing that can have changed is removal of getter methods |
| 85 | + if (classDeclAfterVisit != classDecl) { |
| 86 | + //this set collects the fields for which existing methods have already been removed |
| 87 | + Set<Finding> fieldsToDecorate = getCursor().pollNearestMessage(FIELDS_TO_DECORATE_KEY); |
| 88 | + doAfterVisit(new FieldAnnotator(fieldsToDecorate)); |
| 89 | + } |
| 90 | + return classDeclAfterVisit; |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext ctx) { |
| 95 | + assert method.getMethodType() != null; |
| 96 | + |
| 97 | + if (LombokUtils.isEffectivelyGetter(method)) { |
| 98 | + J.Return return_ = (J.Return) method.getBody().getStatements().get(0); |
| 99 | + Variable fieldType = ((J.Identifier) return_.getExpression()).getFieldType(); |
| 100 | + boolean nameMatch = method.getSimpleName().equals(LombokUtils.deriveGetterMethodName(fieldType)); |
| 101 | + if (nameMatch){ |
| 102 | + ((Set<Finding>) getCursor().getNearestMessage(FIELDS_TO_DECORATE_KEY)) |
| 103 | + .add(new Finding(fieldType.getName(), LombokUtils.getAccessLevel(method.getModifiers()))); |
| 104 | + return null; //delete |
| 105 | + } |
| 106 | + } |
| 107 | + return method; |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + @Value |
| 112 | + private static class Finding { |
| 113 | + String fieldName; |
| 114 | + AccessLevel accessLevel; |
| 115 | + } |
| 116 | + |
| 117 | + |
| 118 | + @Value |
| 119 | + @EqualsAndHashCode(callSuper = false) |
| 120 | + static class FieldAnnotator extends JavaIsoVisitor<ExecutionContext>{ |
| 121 | + |
| 122 | + Set<Finding> fieldsToDecorate; |
| 123 | + |
| 124 | + private JavaTemplate getAnnotation(AccessLevel accessLevel) { |
| 125 | + JavaTemplate.Builder builder = AccessLevel.PUBLIC.equals(accessLevel) |
| 126 | + ? JavaTemplate.builder("@Getter\n") |
| 127 | + : JavaTemplate.builder("@Getter(AccessLevel." + accessLevel.name() + ")\n") |
| 128 | + .imports("lombok.AccessLevel"); |
| 129 | + |
| 130 | + return builder |
| 131 | + .imports("lombok.Getter") |
| 132 | + .javaParser(JavaParser.fromJavaVersion() |
| 133 | + .classpath("lombok")) |
| 134 | + .build(); |
| 135 | + } |
| 136 | + |
| 137 | + @Override |
| 138 | + public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations multiVariable, ExecutionContext ctx) { |
| 139 | + |
| 140 | + //we accept only one var decl per line, see description |
| 141 | + if (multiVariable.getVariables().size() > 1) { |
| 142 | + return multiVariable; |
| 143 | + } |
| 144 | + |
| 145 | + J.VariableDeclarations.NamedVariable variable = multiVariable.getVariables().get(0); |
| 146 | + Optional<Finding> field = fieldsToDecorate.stream() |
| 147 | + .filter(f -> f.fieldName.equals(variable.getSimpleName())) |
| 148 | + .findFirst(); |
| 149 | + |
| 150 | + if (!field.isPresent()) { |
| 151 | + return multiVariable; //not the field we are looking for |
| 152 | + } |
| 153 | + |
| 154 | + J.VariableDeclarations annotated = getAnnotation(field.get().getAccessLevel()).apply( |
| 155 | + getCursor(), |
| 156 | + multiVariable.getCoordinates().addAnnotation(comparing(J.Annotation::getSimpleName))); |
| 157 | + maybeAddImport("lombok.Getter"); |
| 158 | + maybeAddImport("lombok.AccessLevel"); |
| 159 | + return annotated; |
| 160 | + } |
| 161 | + } |
| 162 | +} |
0 commit comments