From 3d9c9e6652f6905793b52ff2aff7bafa2cda97d9 Mon Sep 17 00:00:00 2001 From: Alexei Bratuhin Date: Sun, 11 Jun 2023 10:38:51 +0200 Subject: [PATCH] #3156 = wip --- .../openrewrite/java/LombokUtilityClass.java | 26 +++++++++++++++++++ .../java/LombokUtilityClassTest.java | 26 ++++++++++++++++++- 2 files changed, 51 insertions(+), 1 deletion(-) 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 ad74b592c35..71341dffb05 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/LombokUtilityClass.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/LombokUtilityClass.java @@ -5,7 +5,10 @@ import org.openrewrite.TreeVisitor; import org.openrewrite.java.tree.Flag; import org.openrewrite.java.tree.J; +import org.openrewrite.java.tree.JavaType; +import java.util.HashSet; +import java.util.Set; import java.util.concurrent.atomic.AtomicBoolean; import java.util.stream.Collectors; @@ -89,6 +92,29 @@ public J.MethodDeclaration visitMethodDeclaration( executionContext ); } + + @Override + public J.VariableDeclarations.NamedVariable visitVariable( + final J.VariableDeclarations.NamedVariable variable, + final ExecutionContext executionContext + ) { + final J.ClassDeclaration classDecl = getCursor().firstEnclosingOrThrow(J.ClassDeclaration.class); + if (!CheckVisitor.shouldPerformChanges(classDecl)) { + return super.visitVariable(variable, executionContext); + } + + JavaType.Variable vari = variable.getVariableType(); + Set flags = new HashSet<>(vari.getFlags()); + flags.remove(Flag.Static); + + return super.visitVariable( + variable + .withName(variable.getName().withSimpleName(variable.getName().getSimpleName().toLowerCase())) + //.withVariableType(vari.withFlags(Collections.emptySet())), + .withVariableType(new JavaType.Variable(null, Flag.Final.getBitMask(), "", null, null, null)), + executionContext + ); + } } private static class CheckVisitor extends JavaIsoVisitor { 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 cbd41665649..1d8017678c3 100644 --- a/rewrite-java/src/test/java/org/openrewrite/java/LombokUtilityClassTest.java +++ b/rewrite-java/src/test/java/org/openrewrite/java/LombokUtilityClassTest.java @@ -8,7 +8,7 @@ class LombokUtilityClassTest implements RewriteTest { @Test - void happyPathSimple() { + void happyPathSimpleMethod() { rewriteRun( recipeSpec -> recipeSpec .recipe(new LombokUtilityClass() @@ -35,6 +35,30 @@ public int add(final int x, final int y) { ); } + @Test + void happyPathSimpleField() { + rewriteRun( + recipeSpec -> recipeSpec + .recipe(new LombokUtilityClass() + ), + java( + """ + public class A { + public static final int C = 0; + } + """, + """ + import lombok.experimental.UtilityClass; + + @UtilityClass + public class A { + public final int c = 0; + } + """ + ) + ); + } + @Test void doNotUpgradeToUtilityClassIfNonStaticVariables() {