Skip to content

Commit

Permalink
#3156 = correct tests, fix logic
Browse files Browse the repository at this point in the history
do not transform to UtilityClass iff non-static fields or methods are present
  • Loading branch information
coiouhkc committed Jun 9, 2023
1 parent 09806c2 commit f063d99
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

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.stream.Collectors;

import static java.util.Comparator.comparing;
Expand Down Expand Up @@ -38,57 +41,90 @@ public String getDescription() {

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

private static class LombokUtilityClassVisitor extends JavaIsoVisitor<ExecutionContext> {
private static class LombokUtilityClassCheckVisitor extends JavaIsoVisitor<AtomicInteger> {

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

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

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

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

if (classDecl.getLeadingAnnotations().stream().noneMatch(a -> "UtilityClass".equals(a.getSimpleName()))) {
return super.visitMethodDeclaration(method, executionContext);
@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);
}

if (!method.hasModifier(J.Modifier.Type.Static)) {
return super.visitMethodDeclaration(method, executionContext);
public static int countViolations(Tree t) {
return new LombokUtilityClassCheckVisitor().reduce(t, new AtomicInteger(0)).get();
}
}

private static class LombokUtilityClassChangeVisitor extends JavaIsoVisitor<ExecutionContext> {

private int violations = 0;

@Override
public J.ClassDeclaration visitClassDeclaration(
final J.ClassDeclaration classDecl,
final ExecutionContext executionContext
) {
this.violations = LombokUtilityClassCheckVisitor.countViolations(classDecl);

if (violations == 0) {

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

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

return super.visitClassDeclaration(classDecl, executionContext);
}

@Override
public J.MethodDeclaration visitMethodDeclaration(
final J.MethodDeclaration method,
final ExecutionContext executionContext
) {
if (violations == 0) {
return super.visitMethodDeclaration(
method.withModifiers(method.getModifiers().stream()
.filter(m -> m.getType() != J.Modifier.Type.Static)
.collect(Collectors.toList())),
executionContext
);
}

return super.visitMethodDeclaration(
method.withModifiers(method.getModifiers().stream()
.filter(m -> m.getType() != J.Modifier.Type.Static)
.collect(Collectors.toList())),
executionContext
);
return super.visitMethodDeclaration(method, executionContext);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,6 @@ void doNotUpgradeToUtilityClassIfNonStaticVariables() {
recipeSpec -> recipeSpec
.recipe(new LombokUtilityClass()),
java(
"""
public class A {
private final int x = 0;
public static int add(final int x, final int y) {
return x + y;
}
}
""",
"""
public class A {
private final int x = 0;
Expand All @@ -69,13 +61,6 @@ void doNotUpgradeToUtilityClassIfNonStaticMethods() {
recipeSpec -> recipeSpec
.recipe(new LombokUtilityClass()),
java(
"""
public class A {
public int add(final int x, final int y) {
return x + y;
}
}
""",
"""
public class A {
public int add(final int x, final int y) {
Expand Down

0 comments on commit f063d99

Please sign in to comment.