@@ -17,20 +17,28 @@ public class MakeNonReassignedVariablesConstants extends IssuableSubscriptionVis
1717
1818 private static final Logger LOGGER = Loggers .get (MakeNonReassignedVariablesConstants .class );
1919
20+ private final String LOMBOK_SETTER = "Setter" ;
21+ private final String LOMBOK_DATA = "Data" ;
22+
23+ private boolean hasParsedImports = false ;
24+ private boolean hasLombokSetterImport = false ;
25+ private boolean hasLombokDataImport = false ;
26+
27+
2028 @ Override
2129 public List <Kind > nodesToVisit () {
2230 return List .of (Kind .VARIABLE );
2331 }
2432
2533 @ Override
2634 public void visitNode (@ Nonnull Tree tree ) {
27- VariableTree variableTree = (VariableTree ) tree ;
28- LOGGER .debug ("Variable > " + getVariableNameForLogger (variableTree ));
29- LOGGER .debug (" => isNotFinalAndNotStatic(variableTree) = " + isNotFinalAndNotStatic (variableTree ));
30- LOGGER .debug (" => usages = " + variableTree .symbol ().usages ().size ());
31- LOGGER .debug (" => isNotReassigned = " + isNotReassigned (variableTree ));
3235
33- if (isNotFinalAndNotStatic (variableTree ) && isNotReassigned (variableTree )) {
36+ VariableTree variableTree = (VariableTree ) tree ;
37+ LOGGER .info ("Variable > " + getVariableNameForLogger (variableTree ));
38+ LOGGER .info (" => isNotFinalAndNotStatic(variableTree) = " + isNotFinalAndNotStatic (variableTree ));
39+ LOGGER .info (" => usages = " + variableTree .symbol ().usages ().size ());
40+ LOGGER .info (" => isNotReassigned = " + isNotReassigned (variableTree ));
41+ if (hasNoLombokSetter (variableTree ) && isNotFinalAndNotStatic (variableTree ) && isNotReassigned (variableTree )) {
3442 reportIssue (tree , MESSAGE_RULE );
3543 } else {
3644 super .visitNode (tree );
@@ -103,6 +111,77 @@ public static boolean hasModifier(ModifiersTree modifiersTree, Modifier expected
103111 return false ;
104112 }
105113
114+ private boolean hasNoLombokSetter (VariableTree variableTree ) {
115+ // Check if the variable is annotated with @Setter
116+
117+ for (AnnotationTree annotation : variableTree .modifiers ().annotations ()) {
118+ if (annotation .annotationType ().toString ().equals (LOMBOK_SETTER )) {
119+ if (hasLombokImport (variableTree , LOMBOK_SETTER )) {
120+
121+ // Ignore if the annotation has AccessLevel.NONE
122+ if (!annotation .arguments ().isEmpty ()) {
123+ for (ExpressionTree argument : annotation .arguments ()) {
124+ if (argument .is (Kind .MEMBER_SELECT )) {
125+ MemberSelectExpressionTree memberSelectExpressionTree = (MemberSelectExpressionTree ) argument ;
126+ if (memberSelectExpressionTree .expression ().toString ().equals ("AccessLevel" )
127+ && memberSelectExpressionTree .identifier ().name ().equals ("NONE" )) {
128+ return true ;
129+ }
130+ }
131+ }
132+ }
133+ return false ;
134+ }
135+ }
136+ }
137+ // Check if the variable is in a class with @Setter or with @Data
138+ if ( variableTree .parent () != null && !variableTree .parent ().is (Kind .CLASS )){
139+ return true ;
140+ }
141+ if (variableTree .parent () != null && variableTree .parent ().is (Kind .CLASS )) {
142+ ClassTree classTree = (ClassTree ) variableTree .parent ();
143+ for (AnnotationTree annotation : classTree .modifiers ().annotations ()) {
144+ if (annotation .annotationType ().toString ().equals (LOMBOK_SETTER ) && hasLombokImport (variableTree , LOMBOK_SETTER )) {
145+ return false ;
146+ }
147+ if (annotation .annotationType ().toString ().equals (LOMBOK_DATA ) && hasLombokImport (variableTree , LOMBOK_DATA )) {
148+ return false ;
149+ }
150+ }
151+ }
152+
153+ return true ;
154+ }
155+
156+ private boolean hasLombokImport (VariableTree variableTree , String lombokImport ) {
157+ if (!hasParsedImports ) {
158+ Tree currentTree = variableTree ;
159+ while (currentTree .parent () != null && !currentTree .parent ().is (Kind .COMPILATION_UNIT )) {
160+ currentTree = currentTree .parent ();
161+ }
162+ if (currentTree != null ) {
163+ CompilationUnitTree rootNode = (CompilationUnitTree ) currentTree .parent ();
164+ for (var importClauseTree : rootNode .imports ()) {
165+ ImportTree importTree = (ImportTree ) importClauseTree ;
166+ MemberSelectExpressionTree identifier = (MemberSelectExpressionTree ) importTree .qualifiedIdentifier ();
167+
168+ if ("lombok" .equals (identifier .expression ().toString ())) {
169+ if ("*" .equals (identifier .identifier ().name ())) {
170+ hasLombokSetterImport = true ;
171+ hasLombokDataImport = true ;
172+ } else if (LOMBOK_SETTER .equals (identifier .identifier ().name ())) {
173+ hasLombokSetterImport = true ;
174+ } else if (LOMBOK_DATA .equals (identifier .identifier ().name ())) {
175+ hasLombokDataImport = true ;
176+ }
177+ }
178+ }
179+ }
180+ hasParsedImports = true ;
181+ }
182+ return LOMBOK_SETTER .equals (lombokImport ) ? hasLombokSetterImport : hasLombokDataImport ;
183+ }
184+
106185 private String getVariableNameForLogger (VariableTree variableTree ) {
107186 String name = variableTree .simpleName ().name ();
108187
0 commit comments