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

Analyse escaping for local vars #4658

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

MBoegers
Copy link
Contributor

@MBoegers MBoegers commented Nov 8, 2024

What's changed?

I added two visitors that help decide if variables escape and where.

What's your motivation?

When migrating existing code, the question "Is this instance ever leaking its defining scope?" raises from time to time.
Take the migration from StringBuffer to StringBuilder.
StringBuilder is generally in favor, if no synchronization is needed (see JavaDoc StringBuilder).
Synchronization is not needed if the instance never escapes its defining scope.
These helping visitors enable such migration by answering the question of escaping vars.

Anything in particular you'd like reviewers to focus on?

Additional ways to leak an instance.

Anyone you would like to review specifically?

Have you considered any alternatives or workarounds?

I considered the Dataflow API, but I am not fluent enough to archive the same..

Any additional context

This visitor and its helper methods will help @mkarg to realize his migrations for the OpenJDK project.

Checklist

  • I've added unit tests to cover both positive and negative cases
  • I've read and applied the recipe conventions and best practices
  • I've used the IntelliJ IDEA auto-formatter on affected files

@shanman190
Copy link
Contributor

Not specifically related to this PR, but thought that I'd share a bit simpler data flow API example given I too found rewrite-security-frameworks a bit difficult to follow originally. I ended up using the data flow API over in rewrite-feature-flags to great effect.

https://github.com/openrewrite/rewrite-feature-flags/blob/main/src/main/java/org/openrewrite/featureflags/search/FindFeatureFlag.java#L99-L112

@MBoegers
Copy link
Contributor Author

MBoegers commented Nov 9, 2024

Thanks @shanman190 for the reference, I'll definitely have a look. I think the Dataflow API needs some introduction material. After I had a breakthrough I'll share some ;)

@timtebeek timtebeek marked this pull request as draft November 11, 2024 23:07
timtebeek and others added 2 commits November 12, 2024 00:08
…iableEscapeLocationsTest.java

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
@timtebeek timtebeek added the enhancement New feature or request label Nov 11, 2024
Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some suggestions could not be made:

  • rewrite-java-test/src/test/java/org/openrewrite/java/JavaTemplateTest.java
    • lines 44-44

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some suggestions could not be made:

  • rewrite-java-test/src/test/java/org/openrewrite/java/JavaTemplateTest.java
    • lines 44-44

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: In Progress
Development

Successfully merging this pull request may close these issues.

3 participants