@@ -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