Skip to content

Commit

Permalink
#3156 = wip
Browse files Browse the repository at this point in the history
  • Loading branch information
coiouhkc committed Jun 11, 2023
1 parent 9843177 commit 3d9c9e6
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<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)),
executionContext
);
}
}

private static class CheckVisitor extends JavaIsoVisitor<AtomicBoolean> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
class LombokUtilityClassTest implements RewriteTest {

@Test
void happyPathSimple() {
void happyPathSimpleMethod() {
rewriteRun(
recipeSpec -> recipeSpec
.recipe(new LombokUtilityClass()
Expand All @@ -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() {
Expand Down

0 comments on commit 3d9c9e6

Please sign in to comment.