Skip to content

Add recipe to extract complex explicit constructor invocation arguments (JEP 513)#1135

Merged
timtebeek merged 5 commits into
mainfrom
tim/super-statement-move-check
Jun 22, 2026
Merged

Add recipe to extract complex explicit constructor invocation arguments (JEP 513)#1135
timtebeek merged 5 commits into
mainfrom
tim/super-statement-move-check

Conversation

@timtebeek

@timtebeek timtebeek commented Jun 22, 2026

Copy link
Copy Markdown
Member
  • Adds a new ExtractSuperConstructorArgument recipe (wired into UpgradeToJava25, gated on Java 25+) that hoists complex super(..) arguments — method invocations and object creations — into local variables declared right before the super(..) call, made possible by JEP 513. The transformation is strictly behavior-preserving: super arguments already evaluate before the super constructor body and can never reference the instance under construction, so multiple arguments are extracted in their original left-to-right order while trivial side-effect-free arguments (literals and local/parameter references) are left in place. The whole super(..) is skipped when no argument does real work, when a parameter type isn't safely denotable (generics/arrays/type variables), or for this(..) delegation. Relates to Move statements before super(..) in Java constructor #733, scoped deliberately to the safe subset — statements that follow super(..) are not moved, since reordering them relative to the super constructor's side effects could change behavior.

Tests run under JDK 25 (@EnabledForJreRange(min = JAVA_25)) covering single/multi-argument extraction, inline-safe siblings, and the negative cases.

Extracts complex `super(..)` arguments (method invocations and object
creations) into local variables declared right before the `super(..)`
call, now that JEP 513 permits statements before the explicit
constructor invocation.

The transformation is strictly behavior preserving: argument expressions
already evaluate before the super constructor body, and a super argument
can never reference the instance under construction. Arguments are
extracted in their original left-to-right order; trivial side-effect-free
arguments (literals and local/parameter references) are left in place.
The recipe handles both super(..) and this(..) explicit constructor
invocations, so the name now reflects the JLS umbrella term rather than
only the super case.
@timtebeek timtebeek changed the title Add ExtractSuperConstructorArgument recipe for JEP 513 Add recipe to extract complex explicit constructor invocation arguments (JEP 513) Jun 22, 2026
The argument-replacement visitor descended into nested/local classes and
rewrote their super(..)/this(..) calls with the outer constructor's argument
list. Guard against descending into nested class declarations.

Also move ExtractExplicitConstructorInvocationArguments out of the default
UpgradeToJava25 upgrade and into JavaBestPractices, since it restyles
already-valid code rather than migrating it.

Add tests for the nested-class case, varargs bail, statements before super,
static-field extraction, and name-collision avoidance.
@timtebeek timtebeek merged commit a602847 into main Jun 22, 2026
1 check passed
@timtebeek timtebeek deleted the tim/super-statement-move-check branch June 22, 2026 11:54
@github-project-automation github-project-automation Bot moved this from In Progress to Done in OpenRewrite Jun 22, 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.

1 participant