Skip to content

Commit 241a923

Browse files
timtebeekclaude
andauthored
Fix RemoveUnusedPrivateFields for Lombok annotations after visibility modifiers (#759)
* Fix RemoveUnusedPrivateFields to detect annotations after visibility modifiers The recipe was incorrectly removing Lombok-annotated fields when the annotation was placed after the visibility modifier (e.g., `private @Getter String foo;`) instead of before it (e.g., `@Getter private String foo;`). The fix adds a helper method `hasAnyAnnotations()` that checks for both: - Leading annotations (before modifiers) - Type annotations (after modifiers, stored as J.AnnotatedType) Fixes #757 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> * Simplify approach --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent ce75f31 commit 241a923

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, Ex
8585
// RSPEC-S1068 does not apply serialVersionUID of Serializable classes, or fields with annotations.
8686
if (!(skipSerialVersionUID && isSerialVersionUid(vd)) &&
8787
vd.getLeadingAnnotations().isEmpty() &&
88+
!(vd.getTypeExpression() instanceof J.AnnotatedType) &&
8889
vd.hasModifier(J.Modifier.Type.Private)) {
8990
Statement nextStatement = i < statements.size() - 1 ? statements.get(i + 1) : null;
9091
checkFields.add(new CheckField(vd, nextStatement));
@@ -138,6 +139,7 @@ private boolean isSerialVersionUid(J.VariableDeclarations vd) {
138139
TypeUtils.isOfClassType(vd.getType(), "long") &&
139140
vd.getVariables().stream().anyMatch(it -> "serialVersionUID".equals(it.getSimpleName()));
140141
}
142+
141143
};
142144
return Preconditions.check(new NoMissingTypes(), Repeat.repeatUntilStable(visitor));
143145
}

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,32 @@ class A {
448448
);
449449
}
450450

451+
@Issue("https://github.com/openrewrite/rewrite-static-analysis/issues/757")
452+
@Test
453+
void doNotRemoveLombokAnnotatedFieldWhenAnnotationIsAfterModifier() {
454+
rewriteRun(
455+
spec -> spec.parser(JavaParser.fromJavaVersion().classpath("lombok")),
456+
java(
457+
"""
458+
import lombok.Getter;
459+
460+
class OnModifier {
461+
@Getter private String foo;
462+
}
463+
"""
464+
),
465+
java(
466+
"""
467+
import lombok.Getter;
468+
469+
class OnType {
470+
private @Getter String foo;
471+
}
472+
"""
473+
)
474+
);
475+
}
476+
451477
@Issue("https://github.com/openrewrite/rewrite-static-analysis/issues/524")
452478
@Test
453479
void removeUntilStable() {

0 commit comments

Comments
 (0)