rewrite-static-analysis
rewrite-static-analysis copied to clipboard
FinalizePrivateFields finalizes inner class fields when only assigned to from outer class
The FinalizePrivateFields recipe finalizes inner class fields even when there is an assignment to the field from an outer class. This breaks the code...
The following test to add to org.openrewrite.java.cleanup.FinalizePrivateFieldsTest.java fails:
@Test
void nestedClassFieldAssignedFromOutsideIgnored() {
rewriteRun(
java(
"""
class OuterClass {
private final InnerClass inner = new InnerClass();
void assign() {
inner.b = 2;
}
class InnerClass {
private int b = 1;
}
}
"""
)
);
}
I've a problem which also relates to Inner Classes, perhaps there is a similar root cause. Recipe RemoveUnusedPrivateFields. If I have an innerClassVariable variable in the inner class and it used like this in the outer class the innerClassVariable variable is removed in the inner class after running the recipe and the code no longer compiles.
selection = new InnerClass();
selection.innerVlassVariable = 123;