Skip to content

Commit 9d2ed73

Browse files
committed
Remove the need for cursor messaging
1 parent 40a44b1 commit 9d2ed73

File tree

2 files changed

+32
-38
lines changed

2 files changed

+32
-38
lines changed

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
class LombokUtils {
3333

3434
static boolean isGetter(J.MethodDeclaration method) {
35+
if (method.getMethodType() == null) {
36+
return false;
37+
}
3538
// Check signature: no parameters
3639
if (!(method.getParameters().get(0) instanceof J.Empty) || method.getReturnTypeExpression() == null) {
3740
return false;
@@ -43,24 +46,20 @@ static boolean isGetter(J.MethodDeclaration method) {
4346
return false;
4447
}
4548
// Check field is declared on method type
46-
JavaType.Method methodType = method.getMethodType();
47-
if (methodType == null) {
48-
return false;
49-
}
50-
JavaType.FullyQualified declaringType = methodType.getDeclaringType();
51-
52-
// Check return: type and matching field name
49+
JavaType.FullyQualified declaringType = method.getMethodType().getDeclaringType();
5350
Expression returnExpression = ((J.Return) method.getBody().getStatements().get(0)).getExpression();
5451
if (returnExpression instanceof J.Identifier) {
5552
J.Identifier identifier = (J.Identifier) returnExpression;
5653
if (identifier.getFieldType() != null && declaringType == identifier.getFieldType().getOwner()) {
54+
// Check return: type and matching field name
5755
return hasMatchingTypeAndName(method, identifier.getType(), identifier.getSimpleName());
5856
}
5957
} else if (returnExpression instanceof J.FieldAccess) {
6058
J.FieldAccess fieldAccess = (J.FieldAccess) returnExpression;
6159
Expression target = fieldAccess.getTarget();
6260
if (target instanceof J.Identifier && ((J.Identifier) target).getFieldType() != null &&
6361
declaringType == ((J.Identifier) target).getFieldType().getOwner()) {
62+
// Check return: type and matching field name
6463
return hasMatchingTypeAndName(method, fieldAccess.getType(), fieldAccess.getSimpleName());
6564
}
6665
}

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

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -68,44 +68,39 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
6868
@Value
6969
@EqualsAndHashCode(callSuper = false)
7070
private static class MethodRemover extends JavaIsoVisitor<ExecutionContext> {
71-
private static final String FIELDS_TO_DECORATE_KEY = "FIELDS_TO_DECORATE";
72-
7371
@Override
7472
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext ctx) {
75-
76-
//initialize set of fields to annotate
77-
getCursor().putMessage(FIELDS_TO_DECORATE_KEY, new HashSet<Finding>());
78-
79-
//delete methods, note down corresponding fields
80-
J.ClassDeclaration classDeclAfterVisit = super.visitClassDeclaration(classDecl, 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+
}
93+
}
94+
return method;
95+
}
96+
}.visitNonNull(oldBody, ctx);
8197

8298
//only thing that can have changed is removal of getter methods
83-
if (classDeclAfterVisit != classDecl) {
99+
if (oldBody != newBody) {
84100
//this set collects the fields for which existing methods have already been removed
85-
Set<Finding> fieldsToDecorate = getCursor().pollNearestMessage(FIELDS_TO_DECORATE_KEY);
86101
doAfterVisit(new FieldAnnotator(fieldsToDecorate));
87102
}
88-
return classDeclAfterVisit;
89-
}
90-
91-
@Override
92-
public J.@Nullable MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext ctx) {
93-
if (method.getMethodType() != null && LombokUtils.isGetter(method)) {
94-
Set<Finding> set = getCursor().getNearestMessage(FIELDS_TO_DECORATE_KEY);
95-
Expression returnExpression = ((J.Return) method.getBody().getStatements().get(0)).getExpression();
96-
if (returnExpression instanceof J.Identifier) {
97-
set.add(new Finding(
98-
((J.Identifier) returnExpression).getSimpleName(),
99-
LombokUtils.getAccessLevel(method.getModifiers())));
100-
return null;
101-
} else if (returnExpression instanceof J.FieldAccess) {
102-
set.add(new Finding(
103-
((J.FieldAccess) returnExpression).getSimpleName(),
104-
LombokUtils.getAccessLevel(method.getModifiers())));
105-
return null;
106-
}
107-
}
108-
return method;
103+
return super.visitClassDeclaration(classDecl.withBody(newBody), ctx);
109104
}
110105
}
111106

0 commit comments

Comments
 (0)