Skip to content

Commit 8376783

Browse files
committed
From visitMethodDeclaration call doAfterVisit
1 parent 9d2ed73 commit 8376783

File tree

1 file changed

+35
-77
lines changed

1 file changed

+35
-77
lines changed

src/main/java/org/openrewrite/java/migrate/lombok/UseLombokGetter.java

Lines changed: 35 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,14 @@
2727
import org.openrewrite.java.JavaTemplate;
2828
import org.openrewrite.java.tree.Expression;
2929
import org.openrewrite.java.tree.J;
30+
import org.openrewrite.java.tree.JavaType;
3031

3132
import java.util.Collections;
32-
import java.util.HashSet;
33-
import java.util.Optional;
33+
import java.util.List;
3434
import java.util.Set;
3535

3636
import static java.util.Comparator.comparing;
37+
import static lombok.AccessLevel.PUBLIC;
3738

3839
@Value
3940
@EqualsAndHashCode(callSuper = false)
@@ -62,95 +63,52 @@ public Set<String> getTags() {
6263

6364
@Override
6465
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;
9383
}
94-
return method;
9584
}
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;
10286
}
103-
return super.visitClassDeclaration(classDecl.withBody(newBody), ctx);
104-
}
87+
};
10588
}
10689

107-
@Value
108-
private static class Finding {
109-
String fieldName;
110-
AccessLevel accessLevel;
111-
}
11290

11391
@Value
11492
@EqualsAndHashCode(callSuper = false)
11593
static class FieldAnnotator extends JavaIsoVisitor<ExecutionContext> {
11694

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;
13097

13198
@Override
13299
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+
}
146110
}
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;
154112
}
155113
}
156114
}

0 commit comments

Comments
 (0)