Skip to content

Commit

Permalink
#3156: Simplify check visitor calls
Browse files Browse the repository at this point in the history
  • Loading branch information
lukas-poos-openvalue committed Jun 11, 2023
1 parent 1e9539e commit 9843177
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

import org.openrewrite.ExecutionContext;
import org.openrewrite.Recipe;
import org.openrewrite.Tree;
import org.openrewrite.TreeVisitor;
import org.openrewrite.java.tree.Flag;
import org.openrewrite.java.tree.J;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;

import static java.util.Comparator.comparing;
Expand All @@ -28,7 +27,6 @@
* - Transformation: Replace instantiation with static calls to methods
*/
public class LombokUtilityClass extends Recipe {
private int violations = -1;

@Override
public String getDisplayName() {
Expand All @@ -43,92 +41,95 @@ public String getDescription() {

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return new LombokUtilityClassChangeVisitor();
return new ChangeVisitor();
}

private static class LombokUtilityClassCheckVisitor extends JavaIsoVisitor<AtomicInteger> {

private static class ChangeVisitor extends JavaIsoVisitor<ExecutionContext> {

@Override
public J.ClassDeclaration visitClassDeclaration(
final J.ClassDeclaration classDecl,
final AtomicInteger counter
final ExecutionContext executionContext
) {
if (classDecl.getLeadingAnnotations().stream().anyMatch(a -> "UtilityClass".equals(a.getSimpleName()))) {
counter.getAndIncrement();
if (!CheckVisitor.shouldPerformChanges(classDecl)) {
return super.visitClassDeclaration(classDecl, executionContext);
}
return super.visitClassDeclaration(classDecl, counter);

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

return super.visitClassDeclaration(
JavaTemplate
.builder("@UtilityClass")
.imports("lombok.experimental.UtilityClass")
.javaParser(JavaParser.fromJavaVersion().classpath("lombok"))
.build()
.apply(
getCursor(),
classDecl.getCoordinates().addAnnotation(comparing(J.Annotation::getSimpleName))
),
executionContext
);
}

@Override
public J.MethodDeclaration visitMethodDeclaration(
final J.MethodDeclaration method,
final AtomicInteger counter
final ExecutionContext executionContext
) {
if (!method.hasModifier(J.Modifier.Type.Static)) {
counter.getAndIncrement();
final J.ClassDeclaration classDecl = getCursor().firstEnclosingOrThrow(J.ClassDeclaration.class);
if (!CheckVisitor.shouldPerformChanges(classDecl)) {
return super.visitMethodDeclaration(method, executionContext);
}
return super.visitMethodDeclaration(method, counter);
}

@Override
public J.VariableDeclarations.NamedVariable visitVariable(J.VariableDeclarations.NamedVariable variable, AtomicInteger counter) {
if (variable.isField(getCursor()) && !variable.getVariableType().hasFlags(Flag.Static)) {
counter.getAndIncrement();
}
return super.visitVariable(variable, counter);
}

public static int countViolations(Tree t) {
return new LombokUtilityClassCheckVisitor().reduce(t, new AtomicInteger(0)).get();
return super.visitMethodDeclaration(
method.withModifiers(method.getModifiers().stream()
.filter(m -> m.getType() != J.Modifier.Type.Static)
.collect(Collectors.toList())),
executionContext
);
}
}

private class LombokUtilityClassChangeVisitor extends JavaIsoVisitor<ExecutionContext> {
private static class CheckVisitor extends JavaIsoVisitor<AtomicBoolean> {

@Override
public J.ClassDeclaration visitClassDeclaration(
final J.ClassDeclaration classDecl,
final ExecutionContext executionContext
final AtomicBoolean shouldPerformChanges
) {
if (LombokUtilityClass.this.violations == -1) {
LombokUtilityClass.this.violations = LombokUtilityClassCheckVisitor.countViolations(classDecl);
}

if (LombokUtilityClass.this.violations == 0
&& classDecl.getLeadingAnnotations().stream().noneMatch(a -> "UtilityClass".equals(a.getSimpleName()))) {

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

return super.visitClassDeclaration(
JavaTemplate
.builder("@UtilityClass")
.imports("lombok.experimental.UtilityClass")
.javaParser(JavaParser.fromJavaVersion().classpath("lombok"))
.build()
.apply(
getCursor(),
classDecl.getCoordinates().addAnnotation(comparing(J.Annotation::getSimpleName))
), executionContext);
if (classDecl.getLeadingAnnotations().stream().anyMatch(a -> "UtilityClass".equals(a.getSimpleName()))) {
shouldPerformChanges.set(false);
}

return super.visitClassDeclaration(classDecl, executionContext);
return super.visitClassDeclaration(classDecl, shouldPerformChanges);
}

@Override
public J.MethodDeclaration visitMethodDeclaration(
final J.MethodDeclaration method,
final ExecutionContext executionContext
final AtomicBoolean shouldPerformChanges
) {
if (!method.hasModifier(J.Modifier.Type.Static)) {
shouldPerformChanges.set(false);
}
return super.visitMethodDeclaration(method, shouldPerformChanges);
}

@Override
public J.VariableDeclarations.NamedVariable visitVariable(
final J.VariableDeclarations.NamedVariable variable,
final AtomicBoolean shouldPerformChanges
) {
if (LombokUtilityClass.this.violations == 0) {
return super.visitMethodDeclaration(
method.withModifiers(method.getModifiers().stream()
.filter(m -> m.getType() != J.Modifier.Type.Static)
.collect(Collectors.toList())),
executionContext
);
if (variable.isField(getCursor())
&& variable.getVariableType() != null
&& !variable.getVariableType().hasFlags(Flag.Static)) {
shouldPerformChanges.set(false);
}
return super.visitVariable(variable, shouldPerformChanges);
}

return super.visitMethodDeclaration(method, executionContext);
private static boolean shouldPerformChanges(final J.ClassDeclaration classDecl) {
return new CheckVisitor().reduce(classDecl, new AtomicBoolean(true)).get();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -150,30 +150,31 @@ void happyPathInner() {
java(
"""
public class A {
public int add(final int x, final int y) {
return x + y;
}
private class B {
private static int substract(final int x, final int y) {
return x - y;
public int add(final int x, final int y) {
return x + y;
}
private class B {
private static int substract(final int x, final int y) {
return x - y;
}
}
}
}
""",
"""
import lombok.experimental.UtilityClass;
public class A {
public int add(final int x, final int y) {
return x + y;
}
@UtilityClass
private class B {
private int substract(final int x, final int y) {
return x - y;
public int add(final int x, final int y) {
return x + y;
}
@UtilityClass
private class B {
private int substract(final int x, final int y) {
return x - y;
}
}
}
}
"""
)
Expand All @@ -191,30 +192,31 @@ void happyPathNested() {
java(
"""
public class A {
public int add(final int x, final int y) {
return x + y;
}
private static class B {
private static int substract(final int x, final int y) {
return x - y;
public int add(final int x, final int y) {
return x + y;
}
private static class B {
private static int substract(final int x, final int y) {
return x - y;
}
}
}
}
""",
"""
import lombok.experimental.UtilityClass;
public class A {
public int add(final int x, final int y) {
return x + y;
}
@UtilityClass
private static class B {
private int substract(final int x, final int y) {
return x - y;
public int add(final int x, final int y) {
return x + y;
}
@UtilityClass
private static class B {
private int substract(final int x, final int y) {
return x - y;
}
}
}
}
"""
)
Expand Down Expand Up @@ -247,6 +249,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 9843177

Please sign in to comment.