Skip to content

Commit 4b7fb67

Browse files
committed
Light polish
1 parent e6fd1bb commit 4b7fb67

File tree

3 files changed

+20
-38
lines changed

3 files changed

+20
-38
lines changed

src/main/java/org/openrewrite/java/migrate/lombok/ConvertGetter.java

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import lombok.AccessLevel;
1919
import lombok.EqualsAndHashCode;
2020
import lombok.Value;
21+
import org.jspecify.annotations.Nullable;
2122
import org.openrewrite.ExecutionContext;
2223
import org.openrewrite.Recipe;
2324
import org.openrewrite.TreeVisitor;
@@ -29,7 +30,6 @@
2930
import java.util.HashSet;
3031
import java.util.Optional;
3132
import java.util.Set;
32-
import java.util.StringJoiner;
3333

3434
import static java.util.Comparator.comparing;
3535
import static org.openrewrite.java.tree.JavaType.Variable;
@@ -40,33 +40,25 @@ public class ConvertGetter extends Recipe {
4040

4141
@Override
4242
public String getDisplayName() {
43-
//language=markdown
4443
return "Convert getter methods to annotations";
4544
}
4645

4746
@Override
4847
public String getDescription() {
4948
//language=markdown
50-
return new StringJoiner("\n")
51-
.add("Convert trivial getter methods to `@Getter` annotations on their respective fields.")
52-
.add("")
53-
.add("Limitations:")
54-
.add("")
55-
.add(" - Does not add a dependency to Lombok, users need to do that manually")
56-
.add(" - Ignores fields that are declared on the same line as others, e.g. `private int foo, bar;" +
57-
"Users who have such fields are advised to separate them beforehand with " +
58-
"[org.openrewrite.staticanalysis.MultipleVariableDeclaration]" +
59-
"(https://docs.openrewrite.org/recipes/staticanalysis/multiplevariabledeclarations).")
60-
.add(" - Does not offer any of the configuration keys listed in https://projectlombok.org/features/GetterSetter.")
61-
.toString();
49+
return "Convert trivial getter methods to `@Getter` annotations on their respective fields.\n\n" +
50+
"Limitations:\n\n" +
51+
" - Does not add a dependency to Lombok, users need to do that manually\n" +
52+
" - Ignores fields that are declared on the same line as others, e.g. `private int foo, bar; " +
53+
"Users who have such fields are advised to separate them beforehand with [org.openrewrite.staticanalysis.MultipleVariableDeclaration](https://docs.openrewrite.org/recipes/staticanalysis/multiplevariabledeclarations).\n" +
54+
" - Does not offer any of the configuration keys listed in https://projectlombok.org/features/GetterSetter.";
6255
}
6356

6457
@Override
6558
public TreeVisitor<?, ExecutionContext> getVisitor() {
6659
return new MethodRemover();
6760
}
6861

69-
7062
@Value
7163
@EqualsAndHashCode(callSuper = false)
7264
private static class MethodRemover extends JavaIsoVisitor<ExecutionContext> {
@@ -91,7 +83,7 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, Ex
9183
}
9284

9385
@Override
94-
public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext ctx) {
86+
public J.@Nullable MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext ctx) {
9587
assert method.getMethodType() != null;
9688

9789
if (LombokUtils.isEffectivelyGetter(method)) {
@@ -125,12 +117,11 @@ private JavaTemplate getAnnotation(AccessLevel accessLevel) {
125117
JavaTemplate.Builder builder = AccessLevel.PUBLIC.equals(accessLevel) ?
126118
JavaTemplate.builder("@Getter\n") :
127119
JavaTemplate.builder("@Getter(AccessLevel." + accessLevel.name() + ")\n")
128-
.imports("lombok.AccessLevel");
120+
.imports("lombok.AccessLevel");
129121

130122
return builder
131123
.imports("lombok.Getter")
132-
.javaParser(JavaParser.fromJavaVersion()
133-
.classpath("lombok"))
124+
.javaParser(JavaParser.fromJavaVersion().classpath("lombok"))
134125
.build();
135126
}
136127

src/main/java/org/openrewrite/java/migrate/lombok/LombokUtils.java

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
import static lombok.AccessLevel.*;
2929
import static org.openrewrite.java.tree.J.Modifier.Type.*;
3030

31-
public class LombokUtils {
31+
class LombokUtils {
3232

3333
public static boolean isEffectivelyGetter(J.MethodDeclaration method) {
3434
boolean takesNoParameters = method.getParameters().get(0) instanceof J.Empty;
@@ -49,19 +49,16 @@ public static boolean isEffectivelyGetter(J.MethodDeclaration method) {
4949
}
5050

5151
public static String deriveGetterMethodName(JavaType.Variable fieldType) {
52-
boolean isPrimitiveBoolean = JavaType.Variable.Primitive.Boolean.equals(fieldType.getType());
53-
54-
final String fieldName = fieldType.getName();
55-
56-
boolean alreadyStartsWithIs = fieldName.length() >= 3 &&
57-
fieldName.substring(0, 3).matches("is[A-Z]");
58-
59-
if (isPrimitiveBoolean)
60-
if (alreadyStartsWithIs)
52+
String fieldName = fieldType.getName();
53+
if (fieldType.getType() == JavaType.Variable.Primitive.Boolean) {
54+
boolean alreadyStartsWithIs = fieldName.length() >= 3 &&
55+
fieldName.substring(0, 3).matches("is[A-Z]");
56+
if (alreadyStartsWithIs) {
6157
return fieldName;
62-
else
58+
} else {
6359
return "is" + StringUtils.capitalize(fieldName);
64-
60+
}
61+
}
6562
return "get" + StringUtils.capitalize(fieldName);
6663
}
6764

src/test/java/org/openrewrite/java/migrate/lombok/ConvertGetterTest.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,16 @@
1717

1818
import org.junit.jupiter.api.Test;
1919
import org.openrewrite.DocumentExample;
20-
import org.openrewrite.java.JavaParser;
2120
import org.openrewrite.test.RecipeSpec;
2221
import org.openrewrite.test.RewriteTest;
2322

2423
import static org.openrewrite.java.Assertions.java;
2524

26-
// This is a test for the ConvertToNoArgsConstructor recipe, as an example of how to write a test for an imperative recipe.
2725
class ConvertGetterTest implements RewriteTest {
2826

29-
// Note, you can define defaults for the RecipeSpec and these defaults will be used for all tests.
30-
// In this case, the recipe and the parser are common. See below, on how the defaults can be overridden
31-
// per test.
3227
@Override
3328
public void defaults(RecipeSpec spec) {
34-
spec.recipe(new ConvertGetter())
35-
.parser(JavaParser.fromJavaVersion().logCompilationWarningsAndErrors(true));
29+
spec.recipe(new ConvertGetter());
3630
}
3731

3832
@DocumentExample

0 commit comments

Comments
 (0)