From 10b5c968ee66a232a32aa52c8fe1412ca817a18d Mon Sep 17 00:00:00 2001 From: Lukas Poos Date: Sun, 11 Jun 2023 10:50:06 +0200 Subject: [PATCH] #3156: Update static class members correctly --- .../openrewrite/java/LombokUtilityClass.java | 29 +++++----- .../java/LombokUtilityClassTest.java | 57 ++++++++----------- 2 files changed, 36 insertions(+), 50 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 71341dffb05..cd46bc99e69 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/LombokUtilityClass.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/LombokUtilityClass.java @@ -5,10 +5,7 @@ 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; @@ -60,7 +57,6 @@ public J.ClassDeclaration visitClassDeclaration( } maybeAddImport("lombok.experimental.UtilityClass", false); - return super.visitClassDeclaration( JavaTemplate .builder("@UtilityClass") @@ -94,24 +90,25 @@ public J.MethodDeclaration visitMethodDeclaration( } @Override - public J.VariableDeclarations.NamedVariable visitVariable( - final J.VariableDeclarations.NamedVariable variable, + public J.VariableDeclarations visitVariableDeclarations( + final J.VariableDeclarations multiVariable, final ExecutionContext executionContext ) { final J.ClassDeclaration classDecl = getCursor().firstEnclosingOrThrow(J.ClassDeclaration.class); if (!CheckVisitor.shouldPerformChanges(classDecl)) { - return super.visitVariable(variable, executionContext); + return super.visitVariableDeclarations(multiVariable, 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)), + return super.visitVariableDeclarations( + multiVariable + .withModifiers(multiVariable.getModifiers().stream() + .filter(m -> m.getType() != J.Modifier.Type.Static) + .collect(Collectors.toList()) + ) + .withVariables(multiVariable.getVariables().stream() + .map(v -> v.withName(v.getName().withSimpleName(v.getName().getSimpleName().toLowerCase()))) + .collect(Collectors.toList()) + ), executionContext ); } 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 93f181606cd..6ab10395c72 100644 --- a/rewrite-java/src/test/java/org/openrewrite/java/LombokUtilityClassTest.java +++ b/rewrite-java/src/test/java/org/openrewrite/java/LombokUtilityClassTest.java @@ -10,9 +10,7 @@ class LombokUtilityClassTest implements RewriteTest { @Test void happyPathSimpleMethod() { rewriteRun( - recipeSpec -> recipeSpec - .recipe(new LombokUtilityClass() - ), + recipeSpec -> recipeSpec.recipe(new LombokUtilityClass()), java( """ public class A { @@ -62,9 +60,7 @@ public class A { @Test void happyPathMultiVariableField() { rewriteRun( - recipeSpec -> recipeSpec - .recipe(new LombokUtilityClass() - ), + recipeSpec -> recipeSpec.recipe(new LombokUtilityClass()), java( """ public class A { @@ -87,8 +83,7 @@ public class A { @Test void doNotUpgradeToUtilityClassIfNonStaticVariables() { rewriteRun( - recipeSpec -> recipeSpec - .recipe(new LombokUtilityClass()), + recipeSpec -> recipeSpec.recipe(new LombokUtilityClass()), java( """ public class A { @@ -106,8 +101,7 @@ public static int add(final int x, final int y) { @Test void doNotUpgradeToUtilityClassIfNonStaticMethods() { rewriteRun( - recipeSpec -> recipeSpec - .recipe(new LombokUtilityClass()), + recipeSpec -> recipeSpec.recipe(new LombokUtilityClass()), java( """ public class A { @@ -123,8 +117,7 @@ public int add(final int x, final int y) { @Test void onlyUpgradeRelevantToUtilityClass() { rewriteRun( - recipeSpec -> recipeSpec - .recipe(new LombokUtilityClass()), + recipeSpec -> recipeSpec.recipe(new LombokUtilityClass()), java( """ public class A { @@ -145,12 +138,12 @@ public int add(final int x, final int y) { """ ), java(""" - public class B { - public int add(final int x, final int y) { - return x + y; - } - } - """ + public class B { + public int add(final int x, final int y) { + return x + y; + } + } + """ ) ); } @@ -158,8 +151,7 @@ public int add(final int x, final int y) { @Test void doNotChangeReferenced() { rewriteRun( - recipeSpec -> recipeSpec - .recipe(new LombokUtilityClass()), + recipeSpec -> recipeSpec.recipe(new LombokUtilityClass()), java( """ public class A { @@ -180,12 +172,12 @@ public int add(final int x, final int y) { """ ), java(""" - public class B { - public int add(final int x, final int y) { - return A.add(x, y); - } - } - """ + public class B { + public int add(final int x, final int y) { + return A.add(x, y); + } + } + """ ) ); } @@ -193,8 +185,7 @@ public int add(final int x, final int y) { @Test void happyPathInner() { rewriteRun( - recipeSpec -> recipeSpec - .recipe(new LombokUtilityClass()), + recipeSpec -> recipeSpec.recipe(new LombokUtilityClass()), java( """ public class A { @@ -235,8 +226,7 @@ private int substract(final int x, final int y) { @Test void happyPathNested() { rewriteRun( - recipeSpec -> recipeSpec - .recipe(new LombokUtilityClass()), + recipeSpec -> recipeSpec.recipe(new LombokUtilityClass()), java( """ public class A { @@ -253,7 +243,7 @@ private static int substract(final int x, final int y) { """, """ import lombok.experimental.UtilityClass; - + public class A { public int add(final int x, final int y) { return x + y; @@ -274,8 +264,7 @@ private int substract(final int x, final int y) { @Test void happyPathNonPublic() { rewriteRun( - recipeSpec -> recipeSpec - .recipe(new LombokUtilityClass()), + recipeSpec -> recipeSpec.recipe(new LombokUtilityClass()), java( """ public class A { @@ -297,7 +286,7 @@ public int add(final int x, final int y) { return x + y; } } - + @UtilityClass class B { public int substract(final int x, final int y) {