Skip to content

Commit

Permalink
Skip RenamePrivateFieldsToCamelCase when using lombok (#268)
Browse files Browse the repository at this point in the history
Fixes #267
  • Loading branch information
timtebeek committed Mar 12, 2024
1 parent 3dac84a commit 9bb285a
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

import org.openrewrite.*;
import org.openrewrite.internal.ListUtils;
import org.openrewrite.java.AnnotationMatcher;
import org.openrewrite.java.service.AnnotationService;
import org.openrewrite.java.tree.Flag;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaType;
Expand All @@ -40,6 +42,7 @@
* - The recipe will not rename fields if the result already exists in a class or the result will be a java reserved keyword.
*/
public class RenamePrivateFieldsToCamelCase extends Recipe {
private static final AnnotationMatcher LOMBOK_ANNOTATION = new AnnotationMatcher("@lombok.*");

@Override
public String getDisplayName() {
Expand Down Expand Up @@ -83,6 +86,15 @@ protected boolean shouldRename(Set<String> hasNameKey, J.VariableDeclarations.Na
);
}

@Override
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext ctx) {
// Skip classes annotated with Lombok annotations, as their fields might be set or exposed by Lombok.
if (service(AnnotationService.class).matches(getCursor(), LOMBOK_ANNOTATION)) {
return classDecl;
}
return super.visitClassDeclaration(classDecl, ctx);
}

@SuppressWarnings("all")
@Override
public J.VariableDeclarations.NamedVariable visitVariable(J.VariableDeclarations.NamedVariable variable, ExecutionContext ctx) {
Expand Down Expand Up @@ -123,6 +135,10 @@ public J.VariableDeclarations.NamedVariable visitVariable(J.VariableDeclarations

@Override
public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations multiVariable, ExecutionContext ctx) {
if (service(AnnotationService.class).matches(getCursor(), LOMBOK_ANNOTATION)) {
return multiVariable;
}

J.VariableDeclarations vds = super.visitVariableDeclarations(multiVariable, ctx);
if (getCursor().getMessage("ADD_STATIC", false)) {
return vds.withModifiers(ListUtils.insert(vds.getModifiers(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.openrewrite.DocumentExample;
import org.openrewrite.Issue;
import org.openrewrite.Recipe;
import org.openrewrite.java.JavaParser;
import org.openrewrite.java.marker.JavaVersion;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;
Expand Down Expand Up @@ -585,4 +586,38 @@ public record MyRecord(
)
);
}

@Test
@Issue("https://github.com/openrewrite/rewrite-static-analysis/issues/267")
void doNotChangeLombokAnnotatedClasses() {
rewriteRun(
spec -> spec.parser(JavaParser.fromJavaVersion().classpath("lombok")),
//language=java
java(
"""
@lombok.RequiredArgsConstructor
class Test {
private String D_TYPE_CONNECT = "";
}
"""
)
);
}

@Test
@Issue("https://github.com/openrewrite/rewrite-static-analysis/issues/267")
void doNotChangeLombokAnnotatedFields() {
rewriteRun(
spec -> spec.parser(JavaParser.fromJavaVersion().classpath("lombok")),
//language=java
java(
"""
class Test {
@lombok.Setter
private String D_TYPE_CONNECT = "";
}
"""
)
);
}
}

0 comments on commit 9bb285a

Please sign in to comment.