Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FinalizeLocalVariables fails when processing fields of anonymous classes #181

Closed
AlejandroBertolo opened this issue Oct 2, 2023 · 1 comment · Fixed by #182
Closed
Assignees
Labels
bug Something isn't working

Comments

@AlejandroBertolo
Copy link
Contributor

AlejandroBertolo commented Oct 2, 2023

Greetings. Thanks everyone for your work in this useful library.

We've found a issue with the FinalizeLocalVariables recipe, by which fields of anonymous classes that are reassigned in overriden methods are finalized, generating compilation problems in processed files.

What version of OpenRewrite are you using?

staticAnalysis 1.0.7
RecipeBom 2.3.0
RewriteMavenPlugin 5.7.1

How are you running OpenRewrite?

Through Maven plugin, in a multi-module project.

What is the smallest, simplest way to reproduce the problem?

The following test exposes the problem.

package Test;

import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;

    class Test {

      private final IntegerProperty cellCount = new SimpleIntegerProperty(this, "cellCount", 0) {
        private int oldCount = 0;

        @Override
        protected void invalidated() {
          final int currentCellCount = this.get();

          final boolean countChanged = this.oldCount != currentCellCount;
          if (countChanged) {
            this.oldCount = currentCellCount;
          }
        }
      };
    }
}

What did you expect to see?

No changes should be applied to the previous code.

What did you see instead?

oldCount var was finalized, regardless of assignment made in invalidated().

package Test;

import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;

    class Test {

      private final IntegerProperty cellCount = new SimpleIntegerProperty(this, "cellCount", 0) {
        private final int oldCount = 0;

        @Override
        protected void invalidated() {
          final int currentCellCount = this.get();

          final boolean countChanged = this.oldCount != currentCellCount;
          if (countChanged) {
            this.oldCount = currentCellCount;
          }
        }
      };
    }
}

Are you interested in contributing a fix to OpenRewrite?

Yes, although the fix we have is to identify fields of inner classes and simply skip them. This is a workaround to avoid breaking compilation in processed projects.

workaround

@AlejandroBertolo AlejandroBertolo added the bug Something isn't working label Oct 2, 2023
@timtebeek
Copy link
Contributor

Thanks for pointing this out! Seems best indeed to at the very least skip fields of anonymous classes for now, such that we don't break any code. We could then in the future revisit this with proper guards, although I'd hope this pattern to be rare.

Would appreciate if you'd indeed want to contribute that; seems like you're near that already. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

2 participants