-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Recipe that converts explicit setters to the lombok annotation (#625)
* feat: add recipe that converts explicit getters to the lombok annotation * chore: IntelliJ auto-formatter * add licence header Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Apply suggestions from code review Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Apply suggestions from code review Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * roll back nullable annotation * Light polish * Rename and add Lombok tag * Also handle field access * Push down method and variable name matching into utils * Demonstrate failing case of nested inner class getter * fix: year in licence header had copy-pasted from the example recipe * migrate existing recipe as-is * deactivate getter test for development * Rename and add Lombok tag * fix year in licence header * chore: IntelliJ auto-formatter * apply best practices * light polish * copy from: Also handle field access * minor changes * Minimize changes with `main` branch ahead of rebase to avoid conflicts * Resolve compilation issues * Ensure there is no change for a nested Setter * Extract a reusable FieldAnnotator class * Adopt now shared `FieldAnnotator` for setters as well * Convert most of the checks as used for UseLombokGetter * Add one more style we ought to cover * Add remaining checks to make all tests pass * Move down variable and method closer to usage * Inline variables used only once --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Tim te Beek <[email protected]> Co-authored-by: Tim te Beek <[email protected]>
- Loading branch information
1 parent
17de874
commit bb00d0a
Showing
5 changed files
with
729 additions
and
39 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
src/main/java/org/openrewrite/java/migrate/lombok/FieldAnnotator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Copyright 2024 the original author or authors. | ||
* <p> | ||
* Licensed under the Moderne Source Available License (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* <p> | ||
* https://docs.moderne.io/licensing/moderne-source-available-license | ||
* <p> | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.openrewrite.java.migrate.lombok; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.EqualsAndHashCode; | ||
import lombok.Value; | ||
import org.openrewrite.ExecutionContext; | ||
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 static java.util.Comparator.comparing; | ||
import static lombok.AccessLevel.PUBLIC; | ||
|
||
@Value | ||
@EqualsAndHashCode(callSuper = false) | ||
class FieldAnnotator extends JavaIsoVisitor<ExecutionContext> { | ||
|
||
Class<?> annotation; | ||
JavaType field; | ||
AccessLevel accessLevel; | ||
|
||
@Override | ||
public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations multiVariable, ExecutionContext ctx) { | ||
for (J.VariableDeclarations.NamedVariable variable : multiVariable.getVariables()) { | ||
if (variable.getName().getFieldType() == field) { | ||
maybeAddImport(annotation.getName()); | ||
maybeAddImport("lombok.AccessLevel"); | ||
String suffix = accessLevel == PUBLIC ? "" : String.format("(AccessLevel.%s)", accessLevel.name()); | ||
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))); | ||
} | ||
} | ||
return multiVariable; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
src/main/java/org/openrewrite/java/migrate/lombok/UseLombokSetter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Copyright 2024 the original author or authors. | ||
* <p> | ||
* Licensed under the Moderne Source Available License (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* <p> | ||
* https://docs.moderne.io/licensing/moderne-source-available-license | ||
* <p> | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.openrewrite.java.migrate.lombok; | ||
|
||
import lombok.EqualsAndHashCode; | ||
import lombok.Setter; | ||
import lombok.Value; | ||
import org.jspecify.annotations.Nullable; | ||
import org.openrewrite.ExecutionContext; | ||
import org.openrewrite.Recipe; | ||
import org.openrewrite.TreeVisitor; | ||
import org.openrewrite.java.JavaIsoVisitor; | ||
import org.openrewrite.java.tree.Expression; | ||
import org.openrewrite.java.tree.J; | ||
|
||
import java.util.Collections; | ||
import java.util.Set; | ||
|
||
@Value | ||
@EqualsAndHashCode(callSuper = false) | ||
public class UseLombokSetter extends Recipe { | ||
|
||
@Override | ||
public String getDisplayName() { | ||
return "Convert setter methods to annotations"; | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return "Convert trivial setter methods to `@Setter` annotations on their respective fields."; | ||
} | ||
|
||
@Override | ||
public Set<String> getTags() { | ||
return Collections.singleton("lombok"); | ||
} | ||
|
||
@Override | ||
public TreeVisitor<?, ExecutionContext> getVisitor() { | ||
return new JavaIsoVisitor<ExecutionContext>() { | ||
@Override | ||
public J.@Nullable MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext ctx) { | ||
if (LombokUtils.isSetter(method)) { | ||
Expression assignmentVariable = ((J.Assignment) method.getBody().getStatements().get(0)).getVariable(); | ||
if (assignmentVariable instanceof J.FieldAccess && | ||
((J.FieldAccess) assignmentVariable).getName().getFieldType() != null) { | ||
doAfterVisit(new FieldAnnotator(Setter.class, | ||
((J.FieldAccess) assignmentVariable).getName().getFieldType(), | ||
LombokUtils.getAccessLevel(method))); | ||
return null; //delete | ||
|
||
} else if (assignmentVariable instanceof J.Identifier && | ||
((J.Identifier) assignmentVariable).getFieldType() != null) { | ||
doAfterVisit(new FieldAnnotator(Setter.class, | ||
((J.Identifier) assignmentVariable).getFieldType(), | ||
LombokUtils.getAccessLevel(method))); | ||
return null; //delete | ||
} | ||
} | ||
return method; | ||
} | ||
}; | ||
} | ||
} |
Oops, something went wrong.