Skip to content

Commit d735f47

Browse files
add onMethod_ arg If getter/setter method is annotated
1 parent 937bf9b commit d735f47

File tree

6 files changed

+62
-39
lines changed

6 files changed

+62
-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+
import java.util.stream.Collectors;
31+
2832
import static java.util.Comparator.comparing;
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(Collectors.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: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -532,20 +532,29 @@ public int getFoo() {
532532

533533
@Issue("https://github.com/openrewrite/rewrite/issues/5015")
534534
@Test
535-
void noChangeIfAnnotated() {
535+
void addOnMethodArgIfAnnotated() {
536536
rewriteRun(// language=java
537-
java("@interface MyOtherAnnotation {}"),
538537
java(
539538
"""
540539
class A {
541540
542541
int foo = 9;
543542
544-
@MyOtherAnnotation
543+
@Deprecated
544+
@SuppressWarnings("deprecation")
545545
public int getFoo() {
546546
return foo;
547547
}
548548
}
549+
""",
550+
"""
551+
import lombok.Getter;
552+
553+
class A {
554+
555+
@Getter(onMethod_ = {@Deprecated, @SuppressWarnings("deprecation")})
556+
int foo = 9;
557+
}
549558
"""
550559
)
551560
);

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -541,20 +541,29 @@ public void setFoo(int foo) {
541541

542542
@Issue("https://github.com/openrewrite/rewrite/issues/5015")
543543
@Test
544-
void noChangeIfAnnotated() {
544+
void addOnMethodArgIfAnnotated() {
545545
rewriteRun(// language=java
546-
java("@interface MyOtherAnnotation {}"),
547546
java(
548547
"""
549548
class A {
550549
551550
int foo = 9;
552551
553-
@MyOtherAnnotation
552+
@Deprecated
553+
@SuppressWarnings("deprecation")
554554
public void setFoo(int fub) {
555555
this.foo = fub;
556556
}
557557
}
558+
""",
559+
"""
560+
import lombok.Setter;
561+
562+
class A {
563+
564+
@Setter(onMethod_ = {@Deprecated, @SuppressWarnings("deprecation")})
565+
int foo = 9;
566+
}
558567
"""
559568
)
560569
);

0 commit comments

Comments
 (0)