Skip to content

Commit c24a7a6

Browse files
Lombok: add onMethod_ argument If getter/setter method is annotated (#955)
* add onMethod_ arg If getter/setter method is annotated * add more test cases * fix new test cases * Apply suggestions from code review Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * fix build issue * apply github-actions suggestion --------- Co-authored-by: zakaria-shahen <[email protected]> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent 937bf9b commit c24a7a6

File tree

6 files changed

+121
-39
lines changed

6 files changed

+121
-39
lines changed

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

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,31 @@
1919
import lombok.EqualsAndHashCode;
2020
import lombok.Value;
2121
import org.openrewrite.ExecutionContext;
22+
import org.openrewrite.java.AnnotationMatcher;
2223
import org.openrewrite.java.JavaIsoVisitor;
2324
import org.openrewrite.java.JavaParser;
2425
import org.openrewrite.java.JavaTemplate;
2526
import org.openrewrite.java.tree.J;
2627
import org.openrewrite.java.tree.JavaType;
2728

29+
import java.util.List;
30+
2831
import static java.util.Comparator.comparing;
32+
import static java.util.stream.Collectors.joining;
2933
import static lombok.AccessLevel.PUBLIC;
3034

3135
@EqualsAndHashCode(callSuper = false)
3236
@Value
3337
class FieldAnnotator extends JavaIsoVisitor<ExecutionContext> {
3438

35-
Class<?> annotation;
39+
private static final AnnotationMatcher OVERRIDE_MATCHER = new AnnotationMatcher("java.lang.Override");
40+
41+
Class<?> annotation;
3642
JavaType field;
3743
AccessLevel accessLevel;
44+
List<J.Annotation> onMethodAnnotations;
3845

39-
@Override
46+
@Override
4047
public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations multiVariable, ExecutionContext ctx) {
4148
for (J.VariableDeclarations.NamedVariable variable : multiVariable.getVariables()) {
4249
if (variable.getName().getFieldType() == field) {
@@ -46,8 +53,17 @@ public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations m
4653
.noneMatch(ann -> annotationName.equals(ann.getSimpleName()))) {
4754
maybeAddImport(annotation.getName());
4855
maybeAddImport("lombok.AccessLevel");
49-
String suffix = accessLevel == PUBLIC ? "" : String.format("(AccessLevel.%s)", accessLevel.name());
50-
return JavaTemplate.builder("@" + annotation.getSimpleName() + suffix)
56+
String valueArg = accessLevel == PUBLIC ? "" : String.format("AccessLevel.%s", accessLevel.name());
57+
String suffix;
58+
onMethodAnnotations.removeIf(OVERRIDE_MATCHER::matches);
59+
if (onMethodAnnotations.isEmpty()) {
60+
suffix = valueArg.isEmpty() ? "" : String.format("(%s)", valueArg);
61+
} else {
62+
String onMethodArg = String.format("onMethod_ = {%s}", onMethodAnnotations.stream().map(J.Annotation::toString).collect(joining(",")));
63+
suffix = valueArg.isEmpty() ? String.format("(%s)", onMethodArg) : String.format("(value = %s, %s)", valueArg, onMethodArg);
64+
}
65+
66+
return JavaTemplate.builder("@" + annotation.getSimpleName() + suffix)
5167
.imports(annotation.getName(), "lombok.AccessLevel")
5268
.javaParser(JavaParser.fromJavaVersion().classpath("lombok"))
5369
.build().apply(getCursor(), multiVariable.getCoordinates().addAnnotation(comparing(J.Annotation::getSimpleName)));

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

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,17 @@
1919
import org.jspecify.annotations.Nullable;
2020
import org.openrewrite.Cursor;
2121
import org.openrewrite.internal.StringUtils;
22-
import org.openrewrite.java.AnnotationMatcher;
23-
import org.openrewrite.java.service.AnnotationService;
2422
import org.openrewrite.java.tree.Expression;
2523
import org.openrewrite.java.tree.Flag;
2624
import org.openrewrite.java.tree.J;
2725
import org.openrewrite.java.tree.JavaType;
2826

29-
import java.util.List;
30-
3127
import static lombok.AccessLevel.*;
3228
import static org.openrewrite.java.tree.J.Modifier.Type.*;
3329

3430
class LombokUtils {
3531

36-
private static final AnnotationMatcher OVERRIDE_MATCHER = new AnnotationMatcher("java.lang.Override");
37-
38-
static boolean isGetter(Cursor cursor, AnnotationService service) {
32+
static boolean isGetter(Cursor cursor) {
3933
if (!(cursor.getValue() instanceof J.MethodDeclaration)) {
4034
return false;
4135
}
@@ -53,10 +47,6 @@ static boolean isGetter(Cursor cursor, AnnotationService service) {
5347
!(method.getBody().getStatements().get(0) instanceof J.Return)) {
5448
return false;
5549
}
56-
// Check there is no annotation except @Overwrite
57-
if (hasAnyAnnotatioOtherThanOverride(cursor, service)) {
58-
return false;
59-
}
6050
// Check field is declared on method type
6151
JavaType.FullyQualified declaringType = method.getMethodType().getDeclaringType();
6252
Expression returnExpression = ((J.Return) method.getBody().getStatements().get(0)).getExpression();
@@ -127,7 +117,7 @@ public static String deriveGetterMethodName(@Nullable JavaType type, String fiel
127117
return "get" + StringUtils.capitalize(fieldName);
128118
}
129119

130-
static boolean isSetter(Cursor cursor, AnnotationService service) {
120+
static boolean isSetter(Cursor cursor) {
131121
if (!(cursor.getValue() instanceof J.MethodDeclaration)) {
132122
return false;
133123
}
@@ -148,11 +138,6 @@ static boolean isSetter(Cursor cursor, AnnotationService service) {
148138
return false;
149139
}
150140

151-
// Check there is no annotation except @Overwrite
152-
if (hasAnyAnnotatioOtherThanOverride(cursor, service)) {
153-
return false;
154-
}
155-
156141
// Check there's no up/down cast between parameter and field
157142
J.VariableDeclarations.NamedVariable param = ((J.VariableDeclarations) method.getParameters().get(0)).getVariables().get(0);
158143
Expression variable = ((J.Assignment) method.getBody().getStatements().get(0)).getVariable();
@@ -241,8 +226,4 @@ static AccessLevel getAccessLevel(J.MethodDeclaration methodDeclaration) {
241226
return PACKAGE;
242227
}
243228

244-
private static boolean hasAnyAnnotatioOtherThanOverride(Cursor cursor, AnnotationService service) {
245-
List<J.Annotation> annotations = service.getAllAnnotations(cursor);
246-
return !(annotations.isEmpty() || (annotations.size() == 1 && OVERRIDE_MATCHER.matches(annotations.get(0))));
247-
}
248229
}

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.openrewrite.java.tree.Expression;
2828
import org.openrewrite.java.tree.J;
2929

30+
import java.util.List;
3031
import java.util.Set;
3132

3233
import static java.util.Collections.singleton;
@@ -56,22 +57,25 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
5657
return new JavaIsoVisitor<ExecutionContext>() {
5758
@Override
5859
public J.@Nullable MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext ctx) {
59-
if (LombokUtils.isGetter(getCursor(), service(AnnotationService.class))) {
60+
if (LombokUtils.isGetter(getCursor())) {
6061
Expression returnExpression = ((J.Return) method.getBody().getStatements().get(0)).getExpression();
61-
if (returnExpression instanceof J.Identifier &&
62+
List<J.Annotation> onMethodAnnotations = service(AnnotationService.class).getAllAnnotations(getCursor());
63+
if (returnExpression instanceof J.Identifier &&
6264
((J.Identifier) returnExpression).getFieldType() != null) {
6365
doAfterVisit(new FieldAnnotator(
6466
Getter.class,
6567
((J.Identifier) returnExpression).getFieldType(),
66-
LombokUtils.getAccessLevel(method)));
68+
LombokUtils.getAccessLevel(method),
69+
onMethodAnnotations));
6770
return null;
6871
}
6972
if (returnExpression instanceof J.FieldAccess &&
7073
((J.FieldAccess) returnExpression).getName().getFieldType() != null) {
7174
doAfterVisit(new FieldAnnotator(
7275
Getter.class,
7376
((J.FieldAccess) returnExpression).getName().getFieldType(),
74-
LombokUtils.getAccessLevel(method)));
77+
LombokUtils.getAccessLevel(method),
78+
onMethodAnnotations));
7579
return null;
7680
}
7781
}

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.openrewrite.java.tree.Expression;
2828
import org.openrewrite.java.tree.J;
2929

30+
import java.util.List;
3031
import java.util.Set;
3132

3233
import static java.util.Collections.singleton;
@@ -55,21 +56,24 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
5556
return new JavaIsoVisitor<ExecutionContext>() {
5657
@Override
5758
public J.@Nullable MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext ctx) {
58-
if (LombokUtils.isSetter(getCursor(), service(AnnotationService.class))) {
59+
if (LombokUtils.isSetter(getCursor())) {
5960
Expression assignmentVariable = ((J.Assignment) method.getBody().getStatements().get(0)).getVariable();
60-
if (assignmentVariable instanceof J.FieldAccess &&
61+
List<J.Annotation> onMethodAnnotations = service(AnnotationService.class).getAllAnnotations(getCursor());
62+
if (assignmentVariable instanceof J.FieldAccess &&
6163
((J.FieldAccess) assignmentVariable).getName().getFieldType() != null) {
6264
doAfterVisit(new FieldAnnotator(Setter.class,
6365
((J.FieldAccess) assignmentVariable).getName().getFieldType(),
64-
LombokUtils.getAccessLevel(method)));
66+
LombokUtils.getAccessLevel(method),
67+
onMethodAnnotations));
6568
return null; //delete
6669

6770
}
6871
if (assignmentVariable instanceof J.Identifier &&
6972
((J.Identifier) assignmentVariable).getFieldType() != null) {
7073
doAfterVisit(new FieldAnnotator(Setter.class,
7174
((J.Identifier) assignmentVariable).getFieldType(),
72-
LombokUtils.getAccessLevel(method)));
75+
LombokUtils.getAccessLevel(method),
76+
onMethodAnnotations));
7377
return null; //delete
7478
}
7579
}

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

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,36 @@ class A {
197197
);
198198
}
199199

200+
@Test
201+
void replacePrivateGetterAnnotated() {
202+
rewriteRun(// language=java
203+
java(
204+
"""
205+
class A {
206+
207+
int foo = 9;
208+
209+
@Deprecated
210+
private int getFoo() {
211+
return foo;
212+
}
213+
}
214+
""",
215+
"""
216+
import lombok.AccessLevel;
217+
import lombok.Getter;
218+
219+
class A {
220+
221+
@Getter(value = AccessLevel.PRIVATE, onMethod_ = {@Deprecated})
222+
int foo = 9;
223+
}
224+
"""
225+
226+
)
227+
);
228+
}
229+
200230
@Test
201231
void replaceJustTheMatchingGetter() {
202232
rewriteRun(// language=java
@@ -532,20 +562,29 @@ public int getFoo() {
532562

533563
@Issue("https://github.com/openrewrite/rewrite/issues/5015")
534564
@Test
535-
void noChangeIfAnnotated() {
565+
void addOnMethodArgIfAnnotated() {
536566
rewriteRun(// language=java
537-
java("@interface MyOtherAnnotation {}"),
538567
java(
539568
"""
540569
class A {
541570
542571
int foo = 9;
543572
544-
@MyOtherAnnotation
573+
@Deprecated
574+
@SuppressWarnings("deprecation")
545575
public int getFoo() {
546576
return foo;
547577
}
548578
}
579+
""",
580+
"""
581+
import lombok.Getter;
582+
583+
class A {
584+
585+
@Getter(onMethod_ = {@Deprecated, @SuppressWarnings("deprecation")})
586+
int foo = 9;
587+
}
549588
"""
550589
)
551590
);

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

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,35 @@ class A {
232232
);
233233
}
234234

235+
@Test
236+
void replacePrivateSetterAnnotated() {
237+
rewriteRun(// language=java
238+
java(
239+
"""
240+
class A {
241+
242+
int foo = 9;
243+
244+
@Deprecated
245+
private void setFoo(int foo) {
246+
this.foo = foo;
247+
}
248+
}
249+
""",
250+
"""
251+
import lombok.AccessLevel;
252+
import lombok.Setter;
253+
254+
class A {
255+
256+
@Setter(value = AccessLevel.PRIVATE, onMethod_ = {@Deprecated})
257+
int foo = 9;
258+
}
259+
"""
260+
)
261+
);
262+
}
263+
235264
@Test
236265
void replaceJustTheMatchingSetter() {
237266
rewriteRun(// language=java
@@ -541,20 +570,29 @@ public void setFoo(int foo) {
541570

542571
@Issue("https://github.com/openrewrite/rewrite/issues/5015")
543572
@Test
544-
void noChangeIfAnnotated() {
573+
void addOnMethodArgIfAnnotated() {
545574
rewriteRun(// language=java
546-
java("@interface MyOtherAnnotation {}"),
547575
java(
548576
"""
549577
class A {
550578
551579
int foo = 9;
552580
553-
@MyOtherAnnotation
581+
@Deprecated
582+
@SuppressWarnings("deprecation")
554583
public void setFoo(int fub) {
555584
this.foo = fub;
556585
}
557586
}
587+
""",
588+
"""
589+
import lombok.Setter;
590+
591+
class A {
592+
593+
@Setter(onMethod_ = {@Deprecated, @SuppressWarnings("deprecation")})
594+
int foo = 9;
595+
}
558596
"""
559597
)
560598
);

0 commit comments

Comments
 (0)