rewrite-static-analysis icon indicating copy to clipboard operation
rewrite-static-analysis copied to clipboard

FinalizePrivateFields finalizes inner class fields when only assigned to from outer class

Open arthurvl opened this issue 2 years ago • 1 comments

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;
                  }
              }
              """
          )
        );
    }

arthurvl avatar Mar 09 '23 14:03 arthurvl

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;

jbessels avatar Mar 30 '23 14:03 jbessels