Extend UseTryWithResources to non-consecutive declarations#833
Merged
Extend UseTryWithResources to non-consecutive declarations#833
Conversation
Add support for transforming try/finally blocks where the variable declaration is separated from the try block by intervening statements. Uses Java 9 syntax (try (varName)) to keep the declaration in place. Includes validation that the variable is not reassigned between declaration and try block. Adds 6 new test cases covering positive and negative scenarios.
Member
Author
|
Now that we added the ability to use Java 9 syntax it was easier to add support for non-consecutive statements. |
Verifies the recipe correctly handles a finally block with closes for different variables, transforming each across two cycles.
Consolidate tests that exercise the same code paths: close pattern variants, non-consecutive duplicates, and overlapping negative cases.
src/test/java/org/openrewrite/staticanalysis/UseTryWithResourcesTest.java
Show resolved
Hide resolved
steve-aom-elliott
approved these changes
Mar 19, 2026
Contributor
steve-aom-elliott
left a comment
There was a problem hiding this comment.
Somehow hadn't clued into exactly the way the Java 9 syntax presents, but I suppose this works now that I understand it.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds support for transforming try/finally blocks where the variable declaration is separated from the try block by intervening statements. The recipe now uses Java 9 syntax (
try (varName)) to keep the declaration in place while safely moving the resource into the try-with-resources statement.New logic extracts the closed variable name from finally blocks and searches backwards for matching declarations, validating that the variable isn't reassigned in the intervening statements.
Includes 6 new test cases covering positive transformations and edge cases.