Skip to content

Commit 1b942fa

Browse files
committed
Do not remove private fields with missing types
Fixes #524
1 parent c5c225c commit 1b942fa

File tree

2 files changed

+43
-5
lines changed

2 files changed

+43
-5
lines changed

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,11 @@
1818
import lombok.EqualsAndHashCode;
1919
import lombok.Value;
2020
import org.jspecify.annotations.Nullable;
21-
import org.openrewrite.Cursor;
22-
import org.openrewrite.ExecutionContext;
23-
import org.openrewrite.Recipe;
24-
import org.openrewrite.TreeVisitor;
21+
import org.openrewrite.*;
2522
import org.openrewrite.java.AnnotationMatcher;
2623
import org.openrewrite.java.JavaIsoVisitor;
2724
import org.openrewrite.java.JavaVisitor;
25+
import org.openrewrite.java.NoMissingTypes;
2826
import org.openrewrite.java.service.AnnotationService;
2927
import org.openrewrite.java.tree.J;
3028
import org.openrewrite.java.tree.Space;
@@ -62,7 +60,7 @@ public Duration getEstimatedEffortPerOccurrence() {
6260

6361
@Override
6462
public TreeVisitor<?, ExecutionContext> getVisitor() {
65-
return new JavaIsoVisitor<ExecutionContext>() {
63+
JavaIsoVisitor<ExecutionContext> visitor = new JavaIsoVisitor<ExecutionContext>() {
6664
@Value
6765
class CheckField {
6866
J.VariableDeclarations declarations;
@@ -147,6 +145,7 @@ private boolean isSerialVersionUid(J.VariableDeclarations vd) {
147145
vd.getVariables().stream().anyMatch(it -> "serialVersionUID".equals(it.getSimpleName()));
148146
}
149147
};
148+
return Preconditions.check(new NoMissingTypes(), Repeat.repeatUntilStable(visitor));
150149
}
151150

152151
private static class VariableUses {

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.openrewrite.java.JavaParser;
2424
import org.openrewrite.test.RecipeSpec;
2525
import org.openrewrite.test.RewriteTest;
26+
import org.openrewrite.test.TypeValidation;
2627

2728
import static org.openrewrite.java.Assertions.java;
2829

@@ -446,4 +447,42 @@ class A {
446447
)
447448
);
448449
}
450+
451+
@Issue("https://github.com/openrewrite/rewrite-static-analysis/issues/524")
452+
@Test
453+
void removeUntilStable() {
454+
rewriteRun(
455+
//language=java
456+
java(
457+
"""
458+
public class Test {
459+
private String a = "a";
460+
private String ab = a + "b";
461+
private String abc = ab + "c";
462+
}
463+
""",
464+
"""
465+
public class Test {
466+
}
467+
"""
468+
)
469+
);
470+
}
471+
472+
@Issue("https://github.com/openrewrite/rewrite-static-analysis/issues/524")
473+
@Test
474+
void doNotRemoveWhenThereAreMissingTypes() {
475+
rewriteRun(
476+
spec -> spec.typeValidationOptions(TypeValidation.all().identifiers(false)),
477+
//language=java
478+
java(
479+
"""
480+
import can.not.be.Found;
481+
public class Test {
482+
private Found notUsed;
483+
}
484+
"""
485+
)
486+
);
487+
}
449488
}

0 commit comments

Comments
 (0)