|
27 | 27 | import org.openrewrite.java.JavaTemplate; |
28 | 28 | import org.openrewrite.java.tree.Expression; |
29 | 29 | import org.openrewrite.java.tree.J; |
| 30 | +import org.openrewrite.java.tree.JavaType; |
30 | 31 |
|
31 | 32 | import java.util.Collections; |
32 | | -import java.util.HashSet; |
33 | | -import java.util.Optional; |
| 33 | +import java.util.List; |
34 | 34 | import java.util.Set; |
35 | 35 |
|
36 | 36 | import static java.util.Comparator.comparing; |
| 37 | +import static lombok.AccessLevel.PUBLIC; |
37 | 38 |
|
38 | 39 | @Value |
39 | 40 | @EqualsAndHashCode(callSuper = false) |
@@ -62,95 +63,52 @@ public Set<String> getTags() { |
62 | 63 |
|
63 | 64 | @Override |
64 | 65 | public TreeVisitor<?, ExecutionContext> getVisitor() { |
65 | | - return new MethodRemover(); |
66 | | - } |
67 | | - |
68 | | - @Value |
69 | | - @EqualsAndHashCode(callSuper = false) |
70 | | - private static class MethodRemover extends JavaIsoVisitor<ExecutionContext> { |
71 | | - @Override |
72 | | - public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext ctx) { |
73 | | - Set<Finding> fieldsToDecorate = new HashSet<>(); |
74 | | - |
75 | | - J.Block oldBody = classDecl.getBody(); |
76 | | - J.Block newBody = (J.Block) new JavaIsoVisitor<ExecutionContext>() { |
77 | | - //delete methods, note down corresponding fields |
78 | | - @Override |
79 | | - public J.@Nullable MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext ctx) { |
80 | | - if (LombokUtils.isGetter(method)) { |
81 | | - Expression returnExpression = ((J.Return) method.getBody().getStatements().get(0)).getExpression(); |
82 | | - if (returnExpression instanceof J.Identifier) { |
83 | | - fieldsToDecorate.add(new Finding( |
84 | | - ((J.Identifier) returnExpression).getSimpleName(), |
85 | | - LombokUtils.getAccessLevel(method.getModifiers()))); |
86 | | - return null; |
87 | | - } else if (returnExpression instanceof J.FieldAccess) { |
88 | | - fieldsToDecorate.add(new Finding( |
89 | | - ((J.FieldAccess) returnExpression).getSimpleName(), |
90 | | - LombokUtils.getAccessLevel(method.getModifiers()))); |
91 | | - return null; |
92 | | - } |
| 66 | + return new JavaIsoVisitor<ExecutionContext>() { |
| 67 | + @Override |
| 68 | + public J.@Nullable MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext ctx) { |
| 69 | + if (LombokUtils.isGetter(method)) { |
| 70 | + Expression returnExpression = ((J.Return) method.getBody().getStatements().get(0)).getExpression(); |
| 71 | + if (returnExpression instanceof J.Identifier && |
| 72 | + ((J.Identifier) returnExpression).getFieldType() != null) { |
| 73 | + doAfterVisit(new FieldAnnotator( |
| 74 | + ((J.Identifier) returnExpression).getFieldType(), |
| 75 | + LombokUtils.getAccessLevel(method.getModifiers()))); |
| 76 | + return null; |
| 77 | + } else if (returnExpression instanceof J.FieldAccess && |
| 78 | + ((J.FieldAccess) returnExpression).getName().getFieldType() != null) { |
| 79 | + doAfterVisit(new FieldAnnotator( |
| 80 | + ((J.FieldAccess) returnExpression).getName().getFieldType(), |
| 81 | + LombokUtils.getAccessLevel(method.getModifiers()))); |
| 82 | + return null; |
93 | 83 | } |
94 | | - return method; |
95 | 84 | } |
96 | | - }.visitNonNull(oldBody, ctx); |
97 | | - |
98 | | - //only thing that can have changed is removal of getter methods |
99 | | - if (oldBody != newBody) { |
100 | | - //this set collects the fields for which existing methods have already been removed |
101 | | - doAfterVisit(new FieldAnnotator(fieldsToDecorate)); |
| 85 | + return method; |
102 | 86 | } |
103 | | - return super.visitClassDeclaration(classDecl.withBody(newBody), ctx); |
104 | | - } |
| 87 | + }; |
105 | 88 | } |
106 | 89 |
|
107 | | - @Value |
108 | | - private static class Finding { |
109 | | - String fieldName; |
110 | | - AccessLevel accessLevel; |
111 | | - } |
112 | 90 |
|
113 | 91 | @Value |
114 | 92 | @EqualsAndHashCode(callSuper = false) |
115 | 93 | static class FieldAnnotator extends JavaIsoVisitor<ExecutionContext> { |
116 | 94 |
|
117 | | - Set<Finding> fieldsToDecorate; |
118 | | - |
119 | | - private JavaTemplate getAnnotation(AccessLevel accessLevel) { |
120 | | - JavaTemplate.Builder builder = AccessLevel.PUBLIC.equals(accessLevel) ? |
121 | | - JavaTemplate.builder("@Getter\n") : |
122 | | - JavaTemplate.builder("@Getter(AccessLevel." + accessLevel.name() + ")\n") |
123 | | - .imports("lombok.AccessLevel"); |
124 | | - |
125 | | - return builder |
126 | | - .imports("lombok.Getter") |
127 | | - .javaParser(JavaParser.fromJavaVersion().classpath("lombok")) |
128 | | - .build(); |
129 | | - } |
| 95 | + JavaType field; |
| 96 | + AccessLevel accessLevel; |
130 | 97 |
|
131 | 98 | @Override |
132 | 99 | public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations multiVariable, ExecutionContext ctx) { |
133 | | - |
134 | | - //we accept only one var decl per line, see description |
135 | | - if (multiVariable.getVariables().size() > 1) { |
136 | | - return multiVariable; |
137 | | - } |
138 | | - |
139 | | - J.VariableDeclarations.NamedVariable variable = multiVariable.getVariables().get(0); |
140 | | - Optional<Finding> field = fieldsToDecorate.stream() |
141 | | - .filter(f -> f.fieldName.equals(variable.getSimpleName())) |
142 | | - .findFirst(); |
143 | | - |
144 | | - if (!field.isPresent()) { |
145 | | - return multiVariable; //not the field we are looking for |
| 100 | + for (J.VariableDeclarations.NamedVariable variable : multiVariable.getVariables()) { |
| 101 | + if (variable.getName().getFieldType() == field) { |
| 102 | + maybeAddImport("lombok.Getter"); |
| 103 | + maybeAddImport("lombok.AccessLevel"); |
| 104 | + String suffix = accessLevel == PUBLIC ? "" : String.format("(AccessLevel.%s)", accessLevel.name()); |
| 105 | + return JavaTemplate.builder("@Getter" + suffix) |
| 106 | + .imports("lombok.Getter", "lombok.AccessLevel") |
| 107 | + .javaParser(JavaParser.fromJavaVersion().classpath("lombok")) |
| 108 | + .build().apply(getCursor(), multiVariable.getCoordinates().addAnnotation(comparing(J.Annotation::getSimpleName))); |
| 109 | + } |
146 | 110 | } |
147 | | - |
148 | | - J.VariableDeclarations annotated = getAnnotation(field.get().getAccessLevel()).apply( |
149 | | - getCursor(), |
150 | | - multiVariable.getCoordinates().addAnnotation(comparing(J.Annotation::getSimpleName))); |
151 | | - maybeAddImport("lombok.Getter"); |
152 | | - maybeAddImport("lombok.AccessLevel"); |
153 | | - return annotated; |
| 111 | + return multiVariable; |
154 | 112 | } |
155 | 113 | } |
156 | 114 | } |
0 commit comments