Skip to content

Commit

Permalink
#3156: Update static class members correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
lukas-poos-openvalue committed Jun 11, 2023
1 parent e9e61a1 commit 10b5c96
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -60,7 +57,6 @@ public J.ClassDeclaration visitClassDeclaration(
}

maybeAddImport("lombok.experimental.UtilityClass", false);

return super.visitClassDeclaration(
JavaTemplate
.builder("@UtilityClass")
Expand Down Expand Up @@ -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<Flag> 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
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -145,21 +138,20 @@ 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;
}
}
"""
)
);
}

@Test
void doNotChangeReferenced() {
rewriteRun(
recipeSpec -> recipeSpec
.recipe(new LombokUtilityClass()),
recipeSpec -> recipeSpec.recipe(new LombokUtilityClass()),
java(
"""
public class A {
Expand All @@ -180,21 +172,20 @@ 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);
}
}
"""
)
);
}

@Test
void happyPathInner() {
rewriteRun(
recipeSpec -> recipeSpec
.recipe(new LombokUtilityClass()),
recipeSpec -> recipeSpec.recipe(new LombokUtilityClass()),
java(
"""
public class A {
Expand Down Expand Up @@ -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 {
Expand All @@ -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;
Expand All @@ -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 {
Expand All @@ -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) {
Expand Down

0 comments on commit 10b5c96

Please sign in to comment.