From bb366ab5ab99467ca552c868507871f414f6dc82 Mon Sep 17 00:00:00 2001 From: Alexei Bratuhin Date: Sun, 11 Jun 2023 10:57:21 +0200 Subject: [PATCH] #3156 = fix non-final fields, mark progress --- .../openrewrite/java/LombokUtilityClass.java | 18 ++++++++++-------- .../java/LombokUtilityClassTest.java | 16 ++++++++++++++++ 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/rewrite-java/src/main/java/org/openrewrite/java/LombokUtilityClass.java b/rewrite-java/src/main/java/org/openrewrite/java/LombokUtilityClass.java index cd46bc99e69..09f847de122 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/LombokUtilityClass.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/LombokUtilityClass.java @@ -13,18 +13,20 @@ /** * TODO: Check the following criteria: - * - Lombok in dependencies - * - All methods of class are static - * - No instances of given class - * - All static attributes are final + * - Lombok in dependencies + + * - All methods of class are static + + * - No instances of given class + + * - All static attributes are final + *

* TODO: Perform the transformation: - * - Add the annotation - * - Remove static from all attributes and methods + * - Add the annotation + + * - Remove static from all attributes and methods + *

* TODO: Features to consider: * - Transformation: Add Lombok config if not present + supported configuration options for utility class - * - Transformation: Replace instantiation with static calls to methods + * - Transformation: Replace instantiation with static calls to methods --> na + * - Anonymous classes ??? + * - Reflection ??? */ public class LombokUtilityClass extends Recipe { @@ -145,7 +147,7 @@ public J.VariableDeclarations.NamedVariable visitVariable( ) { if (variable.isField(getCursor()) && variable.getVariableType() != null - && !variable.getVariableType().hasFlags(Flag.Static)) { + && !variable.getVariableType().hasFlags(Flag.Static, Flag.Final)) { shouldPerformChanges.set(false); } return super.visitVariable(variable, shouldPerformChanges); diff --git a/rewrite-java/src/test/java/org/openrewrite/java/LombokUtilityClassTest.java b/rewrite-java/src/test/java/org/openrewrite/java/LombokUtilityClassTest.java index 6ab10395c72..95922296fc9 100644 --- a/rewrite-java/src/test/java/org/openrewrite/java/LombokUtilityClassTest.java +++ b/rewrite-java/src/test/java/org/openrewrite/java/LombokUtilityClassTest.java @@ -57,6 +57,22 @@ public class A { ); } + @Test + void doNotChangeFieldIfNotFinal() { + rewriteRun( + recipeSpec -> recipeSpec + .recipe(new LombokUtilityClass() + ), + java( + """ + public class A { + public static int C = 0; + } + """ + ) + ); + } + @Test void happyPathMultiVariableField() { rewriteRun(