Skip to content

Make ExtractExplicitConstructorInvocationArguments templates context-free#1140

Merged
knutwannheden merged 2 commits into
mainfrom
rapid-mule
Jun 23, 2026
Merged

Make ExtractExplicitConstructorInvocationArguments templates context-free#1140
knutwannheden merged 2 commits into
mainfrom
rapid-mule

Conversation

@knutwannheden

Copy link
Copy Markdown
Contributor

Motivation

ExtractExplicitConstructorInvocationArguments (JEP 513) applied both of its JavaTemplates with contextSensitive(). Context-sensitive templating reconstructs and reparses the enclosing class and method on every application, which is the dominant cost when the recipe actually fires. Neither template needs it: contextSensitive() only reconstructs the surrounding local scope (leading variable declarations, method parameters) into the parse stub — it does not govern type resolution. Every type these templates declare is the type of an existing super(..)/this(..) argument, so it is already present in the unit, resolves via the shared JavaTypeCache, and the spliced result is re-attributed against the real cursor afterward. Dropping contextSensitive() therefore produces byte-identical, fully type-attributed output while skipping the enclosing-context reparse.

Summary

  • Drop contextSensitive() from both templates (the local-variable declarations and the argument-list rewrite).
  • Bail on the cheapest decisive check — no method-invocation/object-creation argument — before any variable-name generation, cursor-scope scanning, or templating, so a constructor the recipe will not touch pays nothing.
  • Add two regression tests that lock in the type-attribution behavior the context-free path must preserve: one where the extracted variable's type is a source-path-only type defined in a sibling source, and one where the extracted variable is declared with a widening source-path supertype that is referenced nowhere else in the unit. They assert the rewritten reference keeps its variable binding (fieldType) and the extracted declaration keeps a resolved fully-qualified type (not Unknown).

Test plan

  • ExtractExplicitConstructorInvocationArgumentsTest passes (all existing cases plus the two new source-path regression tests).
  • New tests assert post-recipe type attribution via afterRecipe: the super(..) reference has a non-null type and fieldType, and the extracted declaration's type resolves to a real FQ type.

…free

The two JavaTemplate uses were context-sensitive, which reconstructs and
reparses the enclosing class and method on every application. Neither needs
it: contextSensitive() only reconstructs surrounding local scope into the
parse stub, not type resolution. Every type these templates declare is the
type of an existing super(..)/this(..) argument, so it is already present in
the unit and resolves via the shared type cache, and the spliced result is
re-attributed against the real cursor afterward.

Also bail on the cheapest decisive check (no method-invocation/object-creation
argument) before any variable-name generation, scope scanning, or templating.

Add regression tests covering a source-path parameter type and a widening
source-path supertype, asserting the rewritten reference keeps its variable
binding and the extracted declaration keeps a resolved type.
@github-project-automation github-project-automation Bot moved this from In Progress to Ready to Review in OpenRewrite Jun 23, 2026
- Replace the `anyComplex` flag + loop + `if` with a single `noneMatch` guard.
- Use the static `JavaTemplate.apply(..)` for the argument-list rewrite.
@knutwannheden knutwannheden merged commit 9105dbd into main Jun 23, 2026
1 check passed
@knutwannheden knutwannheden deleted the rapid-mule branch June 23, 2026 09:43
@github-project-automation github-project-automation Bot moved this from Ready to Review to Done in OpenRewrite Jun 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants