Skip to content

Commit c789ee8

Browse files
timtebeekTeamModerne
authored andcommitted
Lombok Best Practices
Use this link to re-run the recipe: https://app.moderne.io/recipes/org.openrewrite.java.migrate.lombok.LombokBestPractices?organizationId=QUxML01vZGVybmUvTW9kZXJuZSArIE9wZW5SZXdyaXRl Co-authored-by: Moderne <team@moderne.io>
1 parent 08296c5 commit c789ee8

6 files changed

Lines changed: 16 additions & 33 deletions

File tree

src/main/java/org/openrewrite/java/testing/hamcrest/HamcrestMatcherToJUnit5.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
*/
1616
package org.openrewrite.java.testing.hamcrest;
1717

18+
import lombok.AccessLevel;
1819
import lombok.Getter;
20+
import lombok.RequiredArgsConstructor;
1921
import org.openrewrite.ExecutionContext;
2022
import org.openrewrite.Preconditions;
2123
import org.openrewrite.Recipe;
@@ -51,6 +53,7 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
5153
new MigrationFromHamcrestVisitor());
5254
}
5355

56+
@RequiredArgsConstructor(access = AccessLevel.PACKAGE)
5457
enum Replacement {
5558
EQUALTO("equalTo", "assertEquals", "assertNotEquals", "#{any(java.lang.Object)}, #{any(java.lang.Object)}", "examinedObjThenMatcherArgs"),
5659
EMPTYARRAY("emptyArray", "assertEquals", "assertNotEquals", "0, #{anyArray(java.lang.Object)}.length", "examinedObjOnly"),
@@ -105,14 +108,6 @@ enum Replacement {
105108
return arguments;
106109
});
107110
}
108-
109-
Replacement(String hamcrest, String junitPositive, String junitNegative, String template, String argumentsMethod) {
110-
this.hamcrest = hamcrest;
111-
this.junitPositive = junitPositive;
112-
this.junitNegative = junitNegative;
113-
this.template = template;
114-
this.argumentsMethod = argumentsMethod;
115-
}
116111
}
117112

118113
private static class MigrationFromHamcrestVisitor extends JavaIsoVisitor<ExecutionContext> {

src/main/java/org/openrewrite/java/testing/jmockit/ArgumentMatchersRewriter.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package org.openrewrite.java.testing.jmockit;
1717

18+
import lombok.AccessLevel;
19+
import lombok.RequiredArgsConstructor;
1820
import org.openrewrite.Cursor;
1921
import org.openrewrite.ExecutionContext;
2022
import org.openrewrite.java.JavaParser;
@@ -26,6 +28,7 @@
2628

2729
import static java.util.Collections.singletonList;
2830

31+
@RequiredArgsConstructor(access = AccessLevel.PACKAGE)
2932
class ArgumentMatchersRewriter {
3033

3134
private static final Set<String> JMOCKIT_ARGUMENT_MATCHERS = new HashSet<>();
@@ -78,12 +81,6 @@ class ArgumentMatchersRewriter {
7881
private final ExecutionContext ctx;
7982
private final J.Block expectationsBlock;
8083

81-
ArgumentMatchersRewriter(JavaVisitor<ExecutionContext> visitor, ExecutionContext ctx, J.Block expectationsBlock) {
82-
this.visitor = visitor;
83-
this.ctx = ctx;
84-
this.expectationsBlock = expectationsBlock;
85-
}
86-
8784
J.Block rewriteJMockitBlock() {
8885
List<Statement> newStatements = new ArrayList<>(expectationsBlock.getStatements().size());
8986
for (Statement expectationStatement : expectationsBlock.getStatements()) {

src/main/java/org/openrewrite/java/testing/jmockit/JMockitBlockRewriter.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import lombok.AccessLevel;
1919
import lombok.Data;
20+
import lombok.Getter;
2021
import lombok.Setter;
2122
import org.jspecify.annotations.Nullable;
2223
import org.openrewrite.Cursor;
@@ -63,12 +64,9 @@ private static String getObjectTemplateField(String fqn) {
6364
private J.Block methodBody;
6465
private JavaCoordinates nextStatementCoordinates;
6566

67+
@Getter(AccessLevel.PACKAGE)
6668
private boolean rewriteFailed = false;
6769

68-
boolean isRewriteFailed() {
69-
return rewriteFailed;
70-
}
71-
7270
// keep track of the additional statements being added to the method body, which impacts the statement indices
7371
// used with bodyStatementIndex to obtain the coordinates of the next statement to be written
7472
private int numStatementsAdded = 0;

src/main/java/org/openrewrite/java/testing/jmockit/SetupStatementsRewriter.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package org.openrewrite.java.testing.jmockit;
1717

18+
import lombok.AccessLevel;
19+
import lombok.AllArgsConstructor;
1820
import org.openrewrite.ExecutionContext;
1921
import org.openrewrite.java.JavaVisitor;
2022
import org.openrewrite.java.tree.*;
@@ -26,16 +28,12 @@
2628

2729
import static java.util.Collections.singletonList;
2830

31+
@AllArgsConstructor(access = AccessLevel.PACKAGE)
2932
class SetupStatementsRewriter {
3033

3134
private final JavaVisitor<ExecutionContext> visitor;
3235
private J.Block methodBody;
3336

34-
SetupStatementsRewriter(JavaVisitor<ExecutionContext> visitor, J.Block methodBody) {
35-
this.visitor = visitor;
36-
this.methodBody = methodBody;
37-
}
38-
3937
J.Block rewriteMethodBody() {
4038
List<Statement> statements = methodBody.getStatements();
4139
// iterate over each statement in the method body, find JMockit blocks and rewrite them

src/main/java/org/openrewrite/java/testing/junit5/TestRuleToTestInfo.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.openrewrite.java.testing.junit5;
1717

1818
import lombok.Getter;
19+
import lombok.RequiredArgsConstructor;
1920
import org.jspecify.annotations.Nullable;
2021
import org.openrewrite.ExecutionContext;
2122
import org.openrewrite.Preconditions;
@@ -144,6 +145,7 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, Ex
144145
}
145146
}
146147

148+
@RequiredArgsConstructor
147149
private static class BeforeMethodToTestInfoVisitor extends JavaIsoVisitor<ExecutionContext> {
148150
private final J.MethodDeclaration beforeMethod;
149151
private final J.VariableDeclarations varDecls;
@@ -154,12 +156,6 @@ private static class BeforeMethodToTestInfoVisitor extends JavaIsoVisitor<Execut
154156
.classpathFromResources(ctx, "junit-jupiter-api-5");
155157
}
156158

157-
public BeforeMethodToTestInfoVisitor(J.MethodDeclaration beforeMethod, J.VariableDeclarations varDecls, String testMethodStatement) {
158-
this.beforeMethod = beforeMethod;
159-
this.varDecls = varDecls;
160-
this.testMethodStatement = testMethodStatement;
161-
}
162-
163159
@Override
164160
public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext ctx) {
165161
J.MethodDeclaration md = super.visitMethodDeclaration(method, ctx);

src/main/java/org/openrewrite/java/testing/mockito/MockitoJUnitRunnerToExtension.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
*/
1616
package org.openrewrite.java.testing.mockito;
1717

18+
import lombok.AccessLevel;
1819
import lombok.Getter;
20+
import lombok.RequiredArgsConstructor;
1921
import org.jspecify.annotations.Nullable;
2022
import org.openrewrite.ExecutionContext;
2123
import org.openrewrite.Preconditions;
@@ -136,17 +138,14 @@ private Optional<JavaTemplate> getTemplate(Strictness strictness, ExecutionConte
136138
});
137139
}
138140

141+
@RequiredArgsConstructor(access = AccessLevel.PACKAGE)
139142
private enum Strictness {
140143
LENIENT("org.mockito.junit.MockitoJUnitRunner.Silent"),
141144
WARN("org.mockito.junit.MockitoJUnitRunner"),
142145
STRICT_STUBS("org.mockito.junit.MockitoJUnitRunner.Strict");
143146

144147
final String runner;
145148

146-
Strictness(String runner) {
147-
this.runner = runner;
148-
}
149-
150149
// Return true, if current strictness is greater than given strictness.
151150
boolean isGreaterThan(Strictness strictness) {
152151
return this.ordinal() > strictness.ordinal();

0 commit comments

Comments
 (0)