Skip to content

Commit

Permalink
#3156 = fix implementation of happy path, add failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
coiouhkc committed Jun 10, 2023
1 parent f063d99 commit c73542c
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
* - Transformation: Replace instantiation with static calls to methods
*/
public class LombokUtilityClass extends Recipe {
private int violations = -1;

@Override
public String getDisplayName() {
return "Lombok UtilityClass";
Expand Down Expand Up @@ -81,30 +83,32 @@ public static int countViolations(Tree t) {
}
}

private static class LombokUtilityClassChangeVisitor extends JavaIsoVisitor<ExecutionContext> {

private int violations = 0;
private class LombokUtilityClassChangeVisitor extends JavaIsoVisitor<ExecutionContext> {

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

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

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(
JavaTemplate
.builder("@UtilityClass")
.imports("lombok.experimental.UtilityClass")
.javaParser(JavaParser.fromJavaVersion().classpath("lombok"))
.build()
.apply(
getCursor(),
classDecl.getCoordinates().addAnnotation(comparing(J.Annotation::getSimpleName))
), executionContext);
}

return super.visitClassDeclaration(classDecl, executionContext);
Expand All @@ -115,7 +119,7 @@ public J.MethodDeclaration visitMethodDeclaration(
final J.MethodDeclaration method,
final ExecutionContext executionContext
) {
if (violations == 0) {
if (LombokUtilityClass.this.violations == 0) {
return super.visitMethodDeclaration(
method.withModifiers(method.getModifiers().stream()
.filter(m -> m.getType() != J.Modifier.Type.Static)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,74 @@ public int add(final int x, final int y) {
)
);
}

@Test
void onlyUpgradeRelevantToUtilityClass() {
rewriteRun(
recipeSpec -> recipeSpec
.recipe(new LombokUtilityClass()),
java(
"""
public class A {
public static int add(final int x, final int y) {
return x + y;
}
}
""",
"""
import lombok.experimental.UtilityClass;
@UtilityClass
public class A {
public int add(final int x, final int y) {
return x + y;
}
}
"""
),
java("""
public class B {
public int add(final int x, final int y) {
return x + y;
}
}
"""
)
);
}

@Test
void doNotChangeReferenced() {
rewriteRun(
recipeSpec -> recipeSpec
.recipe(new LombokUtilityClass()),
java(
"""
public class A {
public static int add(final int x, final int y) {
return x + y;
}
}
""",
"""
import lombok.experimental.UtilityClass;
@UtilityClass
public class A {
public int add(final int x, final int y) {
return x + y;
}
}
"""
),
java("""
public class B {
public int add(final int x, final int y) {
return A.add(x, y);
}
}
"""
)
);
}
}

0 comments on commit c73542c

Please sign in to comment.