Skip to content

Drop the rewrite-analysis dependency from URLConstructorToURICreate - #1226

Merged
timtebeek merged 2 commits into
mainfrom
tim/remove-rewrite-analysis-dep
Aug 26, 2026
Merged

Drop the rewrite-analysis dependency from URLConstructorToURICreate#1226
timtebeek merged 2 commits into
mainfrom
tim/remove-rewrite-analysis-dep

Conversation

@timtebeek

@timtebeek timtebeek commented Aug 25, 2026

Copy link
Copy Markdown
Member

URLConstructorToURICreate was the only consumer of org.openrewrite.meta:rewrite-analysis, using ConstantFold to resolve a String identifier back to the literal it was initialized with. That lookup is now a walk over the enclosing blocks for the matching variable declaration, which drops the RecipeRunException catch that existed solely because ConstantFold throws on a lambda parameter.

To keep the previous behaviour, the walk matches on JavaType.Variable rather than on the name — the owner distinguishes a local shadowing a field from the field itself — and refuses to fold a variable that is assigned to anywhere in the file, including through this, which is the condition ConstantFold expressed as a single assigned value.

Tests cover the local-variable case that was previously untested, plus the reassigned, shadowed-field, and reassigned-through-this cases; all 14 pass.

A single recipe pulled in the whole module, only to resolve a `String`
identifier back to the literal it was initialized with. Walking the enclosing
blocks for the matching variable declaration covers the fields and locals that
the recipe actually sees, and it drops the `RecipeRunException` catch that was
there solely because `ConstantFold` throws on a lambda parameter.

The lookup is by name rather than by dataflow, so a variable reassigned before
use, or shadowed elsewhere, can now fold to a different value. That only shifts
whether the path is judged valid; the argument is passed through to
`URI.create` untouched either way.
Matching the declaration on its name alone crossed two boundaries the previous
`ConstantFold` respected. A local shadowing a field is a distinct variable, so
comparing `JavaType.Variable` — which carries the owner — keeps the walk going
past the shadowing declaration to the one the identifier actually binds to. And
a variable written to anywhere in the file no longer holds the value it was
declared with, which is the condition `ConstantFold` expressed as a single
assigned value.

An assignment through `this` reaches the same variable as a bare name, so the
target is unwrapped before comparing.
@timtebeek
timtebeek marked this pull request as ready for review August 25, 2026 21:41
@timtebeek
timtebeek requested a review from MBoegers August 25, 2026 21:41

@MBoegers MBoegers left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This will not work for external constants Assume:

// src/main/Constants.java
public class Constants {
   public static final String requestURL = "..";
   //...
}

where Constants.requestURL is used in different locations.
Not common, as it breaks configurability and multiple coding best practices, but still possible to encounter in small tools.

Okay to remove the overhead but want to flag the new gap.

@github-project-automation github-project-automation Bot moved this from In Progress to Ready to Review in OpenRewrite Aug 26, 2026
@timtebeek

Copy link
Copy Markdown
Member Author

Indeed a known gap, much as we saw on the rewrite-spring module as well:

@timtebeek
timtebeek merged commit cc86d51 into main Aug 26, 2026
1 check passed
@timtebeek
timtebeek deleted the tim/remove-rewrite-analysis-dep branch August 26, 2026 09:54
@github-project-automation github-project-automation Bot moved this from Ready to Review to Done in OpenRewrite Aug 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

2 participants