Skip to content

Commit 9bb285a

Browse files
authored
Skip RenamePrivateFieldsToCamelCase when using lombok (#268)
Fixes #267
1 parent 3dac84a commit 9bb285a

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

src/main/java/org/openrewrite/staticanalysis/RenamePrivateFieldsToCamelCase.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

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

4447
@Override
4548
public String getDisplayName() {
@@ -83,6 +86,15 @@ protected boolean shouldRename(Set<String> hasNameKey, J.VariableDeclarations.Na
8386
);
8487
}
8588

89+
@Override
90+
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext ctx) {
91+
// Skip classes annotated with Lombok annotations, as their fields might be set or exposed by Lombok.
92+
if (service(AnnotationService.class).matches(getCursor(), LOMBOK_ANNOTATION)) {
93+
return classDecl;
94+
}
95+
return super.visitClassDeclaration(classDecl, ctx);
96+
}
97+
8698
@SuppressWarnings("all")
8799
@Override
88100
public J.VariableDeclarations.NamedVariable visitVariable(J.VariableDeclarations.NamedVariable variable, ExecutionContext ctx) {
@@ -123,6 +135,10 @@ public J.VariableDeclarations.NamedVariable visitVariable(J.VariableDeclarations
123135

124136
@Override
125137
public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations multiVariable, ExecutionContext ctx) {
138+
if (service(AnnotationService.class).matches(getCursor(), LOMBOK_ANNOTATION)) {
139+
return multiVariable;
140+
}
141+
126142
J.VariableDeclarations vds = super.visitVariableDeclarations(multiVariable, ctx);
127143
if (getCursor().getMessage("ADD_STATIC", false)) {
128144
return vds.withModifiers(ListUtils.insert(vds.getModifiers(),

src/test/java/org/openrewrite/staticanalysis/RenamePrivateFieldsToCamelCaseTest.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.openrewrite.DocumentExample;
2020
import org.openrewrite.Issue;
2121
import org.openrewrite.Recipe;
22+
import org.openrewrite.java.JavaParser;
2223
import org.openrewrite.java.marker.JavaVersion;
2324
import org.openrewrite.test.RecipeSpec;
2425
import org.openrewrite.test.RewriteTest;
@@ -585,4 +586,38 @@ public record MyRecord(
585586
)
586587
);
587588
}
589+
590+
@Test
591+
@Issue("https://github.com/openrewrite/rewrite-static-analysis/issues/267")
592+
void doNotChangeLombokAnnotatedClasses() {
593+
rewriteRun(
594+
spec -> spec.parser(JavaParser.fromJavaVersion().classpath("lombok")),
595+
//language=java
596+
java(
597+
"""
598+
@lombok.RequiredArgsConstructor
599+
class Test {
600+
private String D_TYPE_CONNECT = "";
601+
}
602+
"""
603+
)
604+
);
605+
}
606+
607+
@Test
608+
@Issue("https://github.com/openrewrite/rewrite-static-analysis/issues/267")
609+
void doNotChangeLombokAnnotatedFields() {
610+
rewriteRun(
611+
spec -> spec.parser(JavaParser.fromJavaVersion().classpath("lombok")),
612+
//language=java
613+
java(
614+
"""
615+
class Test {
616+
@lombok.Setter
617+
private String D_TYPE_CONNECT = "";
618+
}
619+
"""
620+
)
621+
);
622+
}
588623
}

0 commit comments

Comments
 (0)