Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,31 @@
import lombok.EqualsAndHashCode;
import lombok.Value;
import org.openrewrite.ExecutionContext;
import org.openrewrite.java.AnnotationMatcher;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.JavaParser;
import org.openrewrite.java.JavaTemplate;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaType;

import java.util.List;
import static java.util.stream.Collectors.joining;

import static java.util.Comparator.comparing;
import static lombok.AccessLevel.PUBLIC;

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

Class<?> annotation;
private static final AnnotationMatcher OVERRIDE_MATCHER = new AnnotationMatcher("java.lang.Override");

Class<?> annotation;
JavaType field;
AccessLevel accessLevel;
List<J.Annotation> onMethodAnnotations;

@Override
@Override
public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations multiVariable, ExecutionContext ctx) {
for (J.VariableDeclarations.NamedVariable variable : multiVariable.getVariables()) {
if (variable.getName().getFieldType() == field) {
Expand All @@ -46,8 +53,16 @@ public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations m
.noneMatch(ann -> annotationName.equals(ann.getSimpleName()))) {
maybeAddImport(annotation.getName());
maybeAddImport("lombok.AccessLevel");
String suffix = accessLevel == PUBLIC ? "" : String.format("(AccessLevel.%s)", accessLevel.name());
return JavaTemplate.builder("@" + annotation.getSimpleName() + suffix)
String valueArg = accessLevel == PUBLIC ? "" : String.format("AccessLevel.%s", accessLevel.name());
String suffix;
onMethodAnnotations.removeIf(OVERRIDE_MATCHER::matches);
if (onMethodAnnotations.isEmpty()) {
suffix = valueArg.isEmpty() ? "" : String.format("(%s)", valueArg);
} else {
String onMethodArg = String.format("onMethod_ = {%s}", onMethodAnnotations.stream().map(J.Annotation::toString).collect(joining(",")));
}

return JavaTemplate.builder("@" + annotation.getSimpleName() + suffix)
.imports(annotation.getName(), "lombok.AccessLevel")
.javaParser(JavaParser.fromJavaVersion().classpath("lombok"))
.build().apply(getCursor(), multiVariable.getCoordinates().addAnnotation(comparing(J.Annotation::getSimpleName)));
Expand Down
23 changes: 2 additions & 21 deletions src/main/java/org/openrewrite/java/migrate/lombok/LombokUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,17 @@
import org.jspecify.annotations.Nullable;
import org.openrewrite.Cursor;
import org.openrewrite.internal.StringUtils;
import org.openrewrite.java.AnnotationMatcher;
import org.openrewrite.java.service.AnnotationService;
import org.openrewrite.java.tree.Expression;
import org.openrewrite.java.tree.Flag;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaType;

import java.util.List;

import static lombok.AccessLevel.*;
import static org.openrewrite.java.tree.J.Modifier.Type.*;

class LombokUtils {

private static final AnnotationMatcher OVERRIDE_MATCHER = new AnnotationMatcher("java.lang.Override");

static boolean isGetter(Cursor cursor, AnnotationService service) {
static boolean isGetter(Cursor cursor) {
if (!(cursor.getValue() instanceof J.MethodDeclaration)) {
return false;
}
Expand All @@ -53,10 +47,6 @@ static boolean isGetter(Cursor cursor, AnnotationService service) {
!(method.getBody().getStatements().get(0) instanceof J.Return)) {
return false;
}
// Check there is no annotation except @Overwrite
if (hasAnyAnnotatioOtherThanOverride(cursor, service)) {
return false;
}
// Check field is declared on method type
JavaType.FullyQualified declaringType = method.getMethodType().getDeclaringType();
Expression returnExpression = ((J.Return) method.getBody().getStatements().get(0)).getExpression();
Expand Down Expand Up @@ -127,7 +117,7 @@ public static String deriveGetterMethodName(@Nullable JavaType type, String fiel
return "get" + StringUtils.capitalize(fieldName);
}

static boolean isSetter(Cursor cursor, AnnotationService service) {
static boolean isSetter(Cursor cursor) {
if (!(cursor.getValue() instanceof J.MethodDeclaration)) {
return false;
}
Expand All @@ -148,11 +138,6 @@ static boolean isSetter(Cursor cursor, AnnotationService service) {
return false;
}

// Check there is no annotation except @Overwrite
if (hasAnyAnnotatioOtherThanOverride(cursor, service)) {
return false;
}

// Check there's no up/down cast between parameter and field
J.VariableDeclarations.NamedVariable param = ((J.VariableDeclarations) method.getParameters().get(0)).getVariables().get(0);
Expression variable = ((J.Assignment) method.getBody().getStatements().get(0)).getVariable();
Expand Down Expand Up @@ -241,8 +226,4 @@ static AccessLevel getAccessLevel(J.MethodDeclaration methodDeclaration) {
return PACKAGE;
}

private static boolean hasAnyAnnotatioOtherThanOverride(Cursor cursor, AnnotationService service) {
List<J.Annotation> annotations = service.getAllAnnotations(cursor);
return !(annotations.isEmpty() || (annotations.size() == 1 && OVERRIDE_MATCHER.matches(annotations.get(0))));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.openrewrite.java.tree.Expression;
import org.openrewrite.java.tree.J;

import java.util.List;
import java.util.Set;

import static java.util.Collections.singleton;
Expand Down Expand Up @@ -56,22 +57,25 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
return new JavaIsoVisitor<ExecutionContext>() {
@Override
public J.@Nullable MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext ctx) {
if (LombokUtils.isGetter(getCursor(), service(AnnotationService.class))) {
if (LombokUtils.isGetter(getCursor())) {
Expression returnExpression = ((J.Return) method.getBody().getStatements().get(0)).getExpression();
if (returnExpression instanceof J.Identifier &&
List<J.Annotation> onMethodAnnotations = service(AnnotationService.class).getAllAnnotations(getCursor());
if (returnExpression instanceof J.Identifier &&
((J.Identifier) returnExpression).getFieldType() != null) {
doAfterVisit(new FieldAnnotator(
Getter.class,
((J.Identifier) returnExpression).getFieldType(),
LombokUtils.getAccessLevel(method)));
LombokUtils.getAccessLevel(method),
onMethodAnnotations));
return null;
}
if (returnExpression instanceof J.FieldAccess &&
((J.FieldAccess) returnExpression).getName().getFieldType() != null) {
doAfterVisit(new FieldAnnotator(
Getter.class,
((J.FieldAccess) returnExpression).getName().getFieldType(),
LombokUtils.getAccessLevel(method)));
LombokUtils.getAccessLevel(method),
onMethodAnnotations));
return null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.openrewrite.java.tree.Expression;
import org.openrewrite.java.tree.J;

import java.util.List;
import java.util.Set;

import static java.util.Collections.singleton;
Expand Down Expand Up @@ -55,21 +56,24 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
return new JavaIsoVisitor<ExecutionContext>() {
@Override
public J.@Nullable MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext ctx) {
if (LombokUtils.isSetter(getCursor(), service(AnnotationService.class))) {
if (LombokUtils.isSetter(getCursor())) {
Expression assignmentVariable = ((J.Assignment) method.getBody().getStatements().get(0)).getVariable();
if (assignmentVariable instanceof J.FieldAccess &&
List<J.Annotation> onMethodAnnotations = service(AnnotationService.class).getAllAnnotations(getCursor());
if (assignmentVariable instanceof J.FieldAccess &&
((J.FieldAccess) assignmentVariable).getName().getFieldType() != null) {
doAfterVisit(new FieldAnnotator(Setter.class,
((J.FieldAccess) assignmentVariable).getName().getFieldType(),
LombokUtils.getAccessLevel(method)));
LombokUtils.getAccessLevel(method),
onMethodAnnotations));
return null; //delete

}
if (assignmentVariable instanceof J.Identifier &&
((J.Identifier) assignmentVariable).getFieldType() != null) {
doAfterVisit(new FieldAnnotator(Setter.class,
((J.Identifier) assignmentVariable).getFieldType(),
LombokUtils.getAccessLevel(method)));
LombokUtils.getAccessLevel(method),
onMethodAnnotations));
return null; //delete
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,36 @@ class A {
);
}

@Test
void replacePrivateGetterAnnotated() {
rewriteRun(// language=java
java(
"""
class A {

int foo = 9;

@Deprecated
private int getFoo() {
return foo;
}
}
""",
"""
import lombok.AccessLevel;
import lombok.Getter;

class A {

@Getter(value = AccessLevel.PRIVATE, onMethod_ = {@Deprecated})
int foo = 9;
}
"""

)
);
}

@Test
void replaceJustTheMatchingGetter() {
rewriteRun(// language=java
Expand Down Expand Up @@ -532,20 +562,29 @@ public int getFoo() {

@Issue("https://github.com/openrewrite/rewrite/issues/5015")
@Test
void noChangeIfAnnotated() {
void addOnMethodArgIfAnnotated() {
rewriteRun(// language=java
java("@interface MyOtherAnnotation {}"),
java(
"""
class A {

int foo = 9;

@MyOtherAnnotation
@Deprecated
@SuppressWarnings("deprecation")
public int getFoo() {
return foo;
}
}
""",
"""
import lombok.Getter;

class A {

@Getter(onMethod_ = {@Deprecated, @SuppressWarnings("deprecation")})
int foo = 9;
}
"""
)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,35 @@ class A {
);
}

@Test
void replacePrivateSetterAnnotated() {
rewriteRun(// language=java
java(
"""
class A {

int foo = 9;

@Deprecated
private void setFoo(int foo) {
this.foo = foo;
}
}
""",
"""
import lombok.AccessLevel;
import lombok.Setter;

class A {

@Setter(value = AccessLevel.PRIVATE, onMethod_ = {@Deprecated})
int foo = 9;
}
"""
)
);
}

@Test
void replaceJustTheMatchingSetter() {
rewriteRun(// language=java
Expand Down Expand Up @@ -541,20 +570,29 @@ public void setFoo(int foo) {

@Issue("https://github.com/openrewrite/rewrite/issues/5015")
@Test
void noChangeIfAnnotated() {
void addOnMethodArgIfAnnotated() {
rewriteRun(// language=java
java("@interface MyOtherAnnotation {}"),
java(
"""
class A {

int foo = 9;

@MyOtherAnnotation
@Deprecated
@SuppressWarnings("deprecation")
public void setFoo(int fub) {
this.foo = fub;
}
}
""",
"""
import lombok.Setter;

class A {

@Setter(onMethod_ = {@Deprecated, @SuppressWarnings("deprecation")})
int foo = 9;
}
"""
)
);
Expand Down
Loading