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 71 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

@mkarg
Copy link

mkarg commented Dec 1, 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 ;)

Does that imply that this PR will change or that it will get revoked? I am actively waiting for this visitors to be finally usable. Thanks for updating.

@MBoegers
Copy link
Contributor Author

MBoegers commented Dec 2, 2024

Hi @mkarg ATM I have not the resources to dig deep into the FlowAPI to implement the alternative solution. I would suggest to postpone this reimplementation but stick with the API fur a further versions. Would that work for you?

@mkarg
Copy link

mkarg commented Dec 2, 2024

I personally have no strong feelings how the visitors are implemented as long as there is a stable API to call them.

package com.sample;
public class Foo{
void test() {
StringBuilder /*~~>*/sb = new StringBuilder();
Copy link

Choose a reason for hiding this comment

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

As other is itself non-escaping, sb essentially is non-escpaing, too.

package com.sample;
public class Foo{
void test() {
StringBuilder /*~~>*/sb = new StringBuilder();
Copy link

Choose a reason for hiding this comment

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

As other is itself non-escaping, sb essentially is non-escpaing, too.

Jenson3210 and others added 10 commits December 18, 2024 17:13
…penrewrite#4659)

* Keep multiple whitelines and same comments from the gitignore file

* Keep multiple whitelines and same comments from the gitignore file
…penrewrite#4657)

* working draft

* IntelliJ formatting

* Update rewrite-yaml/src/main/java/org/openrewrite/yaml/search/FindProperty.java

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

---------

Co-authored-by: Sam Snyder <sam@moderne.io>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
* Update for rewrite 8.0

* Cosmetics

* Fix a few more issues

* Fix a few more test failures

* Fix a few more test failures

* Upgrade recipes to new `JavaTemplate` API

* Replace `getRecipeList()` overrides with `@Getter(lazy = true)` property

* Consistently use `latest.release` or `latest.integration`

* Revert "Replace `getRecipeList()` overrides with `@Getter(lazy = true)` property"

This reverts commit 897d59e79c9515564da45911ce2246934c5b1ae7.

* Migrate to new `Recipe#getInitialState(ExecutionContext)` API

* Polish

* Fix a broken test

Due to reversal of logic after inlining `doAfterVisit()` logic.

* Fix another broken test

We should change the `UpgradeDependencyVersion` in `rewrite-java-dependencies` so that workarounds like this are not necessary.

* Fix last failing test (use `doAfterVisit()` again)

---------

Co-authored-by: Sam Snyder <sam@moderne.io>
rlsanders4 and others added 30 commits December 18, 2024 17:13
* Add test

* Added compilation unit test

* Swap expected and actual

* Restore previous behavior

---------

Co-authored-by: Laurens Westerlaken <laurens.westerlaken@jdriven.com>
…te#4673)

* Polish tests

* Polish

---------

Co-authored-by: Tim te Beek <tim@moderne.io>
…penrewrite#4677)

* Remove redundant space for DeleteMethodArgument when argumentIndex=0

Signed-off-by: kuchang <kuchang@ebay.com>

* Update FindPluginsTest.java

* Adopt prefix of removed element

---------

Signed-off-by: kuchang <kuchang@ebay.com>
Co-authored-by: Tim te Beek <tim@moderne.io>
This is especially important for the YAML implementation, which applies the `ReplaceAliasWithAnchorValueVisitor` visitor to all elements.
Only apply `ReplaceAliasWithAnchorValueVisitor` to `Yaml.Document` elements.

See: openrewrite#4650
* Add some initial tests

* Add append functionality

* Add markers

* Fix constructor after merge

* Polish code

* Verify default behavior retained for declarative recipes through use of `null` in tests

* Add test that fails due to unexpected second cycle

* Adopt Markers.findFirst

* Add newline to indicate Marker is on assignment

* Fix test and polish

* Update rewrite-java/src/main/java/org/openrewrite/java/AddOrUpdateAnnotationAttribute.java

Co-authored-by: Tim te Beek <tim@moderne.io>

---------

Co-authored-by: Tim te Beek <tim@moderne.io>
…4688)

* Make `FindMissingTypes` stricter for method invocations

Validate argument count with respect to parameter count.

* Try to fix linking in `ReloadableJava17JavadocVisitor`

* Try to fix `TypeUtils#isAssignableTo()`

* Also update Java 11 and Java 21 Javadoc visitors

* Also update Java 11 and Java 21 Javadoc visitors

* More updates and simplifications

* Corner case in `TypeUtils`
I didn't even realize the recipe had this feature.
* Add `TypeUtils` tests for primitives

* Correct `TypeUtils#isAssignableTo()` for primitive array types

* New `TypeUtils#isAssignableTo()` API

We need to consider the "position" when performing the assignability check. I.e. check if a parameterized type compared against a generic type variable is used in a context where the generic type parameter is bound by the parameterized type or not.

* Implement new API

* Remove one more `stream()` call

* Update Javadoc visitors to use `TypeUtils#isAssignableTo()` correctly

* Correction

* Corrections

* Corrections

* More Javadoc fixes

* Add missing `@MinimumJava11` annotation
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.