|
| 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.EqualsAndHashCode; |
| 19 | +import lombok.Value; |
| 20 | +import org.openrewrite.ExecutionContext; |
| 21 | +import org.openrewrite.Recipe; |
| 22 | +import org.openrewrite.TreeVisitor; |
| 23 | +import org.openrewrite.java.JavaIsoVisitor; |
| 24 | +import org.openrewrite.java.JavaParser; |
| 25 | +import org.openrewrite.java.JavaTemplate; |
| 26 | +import org.openrewrite.java.tree.J; |
| 27 | + |
| 28 | +import static java.util.Comparator.comparing; |
| 29 | + |
| 30 | +@Value |
| 31 | +@EqualsAndHashCode(callSuper = false) |
| 32 | +public class SummarizeSetter extends Recipe { |
| 33 | + |
| 34 | + @Override |
| 35 | + public String getDisplayName() { |
| 36 | + //language=markdown |
| 37 | + return "Summarize @Setter on fields to class level annotation"; |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public String getDescription() { |
| 42 | + //language=markdown |
| 43 | + return "Substitutes a class level `@Setter` annotation for annotations on every field."; |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public TreeVisitor<?, ExecutionContext> getVisitor() { |
| 48 | + return new Summarizer(); |
| 49 | + } |
| 50 | + |
| 51 | + |
| 52 | + @Value |
| 53 | + @EqualsAndHashCode(callSuper = false) |
| 54 | + private static class Summarizer extends JavaIsoVisitor<ExecutionContext> { |
| 55 | + private static final String ALL_FIELDS_DECORATED_ACC = "ALL_FIELDS_DECORATED_ACC"; |
| 56 | + |
| 57 | + @Override |
| 58 | + public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext ctx) { |
| 59 | + |
| 60 | + //initialize variable to store if all encountered fields have setters |
| 61 | + getCursor().putMessage(ALL_FIELDS_DECORATED_ACC, true); |
| 62 | + |
| 63 | + //delete methods, note down corresponding fields |
| 64 | + J.ClassDeclaration classDeclAfterVisit = super.visitClassDeclaration(classDecl, ctx); |
| 65 | + |
| 66 | + boolean allFieldsAnnotated = getCursor().pollNearestMessage(ALL_FIELDS_DECORATED_ACC); |
| 67 | + |
| 68 | + //only thing that can have changed is removal of setter methods |
| 69 | + //and something needs to have changed before we add an annotation at class level |
| 70 | + if (classDeclAfterVisit != classDecl && allFieldsAnnotated) { |
| 71 | + //Add annotation |
| 72 | + JavaTemplate template = JavaTemplate.builder("@Setter\n") |
| 73 | + .imports("lombok.Setter") |
| 74 | + .javaParser(JavaParser.fromJavaVersion().classpath("lombok")) |
| 75 | + .build(); |
| 76 | + |
| 77 | + return template.apply( |
| 78 | + updateCursor(classDeclAfterVisit), |
| 79 | + classDeclAfterVisit.getCoordinates().addAnnotation(comparing(J.Annotation::getSimpleName))); |
| 80 | + } |
| 81 | + return classDecl; |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations variableDecls, ExecutionContext ctx){ |
| 86 | + |
| 87 | + boolean allFieldsAnnotatedSoFar = getCursor().getNearestMessage(ALL_FIELDS_DECORATED_ACC); |
| 88 | + if (!allFieldsAnnotatedSoFar) { |
| 89 | + return variableDecls; |
| 90 | + } |
| 91 | + J.VariableDeclarations visited = super.visitVariableDeclarations(variableDecls, ctx); |
| 92 | + |
| 93 | + boolean hasSetterAnnotation = variableDecls != visited; |
| 94 | + if (hasSetterAnnotation) { |
| 95 | + return fixFormat(variableDecls, visited, ctx); |
| 96 | + } else if (variableDecls.hasModifier(J.Modifier.Type.Final) |
| 97 | + || variableDecls.hasModifier(J.Modifier.Type.Static)) { |
| 98 | + //final fields and static field don't need to have an annotation |
| 99 | + return visited; |
| 100 | + } else { |
| 101 | + getCursor().putMessageOnFirstEnclosing(J.ClassDeclaration.class, ALL_FIELDS_DECORATED_ACC, false); |
| 102 | + } |
| 103 | + return variableDecls; |
| 104 | + } |
| 105 | + |
| 106 | + private J.VariableDeclarations fixFormat(J.VariableDeclarations initial, J.VariableDeclarations visited, ExecutionContext ctx) { |
| 107 | + |
| 108 | + boolean isAnnotationOnLineAbove = initial.toString().contains("@Setter\n"); |
| 109 | + |
| 110 | + boolean isTopAnnotationRemoved = !initial.getLeadingAnnotations().isEmpty() |
| 111 | + && initial.getLeadingAnnotations() |
| 112 | + .get(0) |
| 113 | + .getSimpleName().equals("Setter"); |
| 114 | + |
| 115 | + if (isAnnotationOnLineAbove && isTopAnnotationRemoved) { |
| 116 | + String minus1NewLine = visited.getPrefix().getWhitespace().replaceFirst("\n", ""); |
| 117 | + visited = visited.withPrefix(visited.getPrefix().withWhitespace(minus1NewLine)); |
| 118 | + } |
| 119 | + return autoFormat(visited, ctx); |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public J.Annotation visitAnnotation(J.Annotation annotation, ExecutionContext ctx) { |
| 124 | + boolean isSetterAnnotated = annotation.getSimpleName().equals("Setter") |
| 125 | + && annotation.getArguments() == null; //no Access level, or other arguments |
| 126 | + |
| 127 | + return isSetterAnnotated |
| 128 | + //should only trigger on field annotation, not class annotation |
| 129 | + && getCursor().getParent().getValue() instanceof J.VariableDeclarations |
| 130 | + ? null |
| 131 | + : annotation; |
| 132 | + } |
| 133 | + } |
| 134 | +} |
0 commit comments