Add recipe to extract complex explicit constructor invocation arguments (JEP 513)#1135
Merged
Conversation
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.
ExtractSuperConstructorArgument recipe for JEP 513The 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.
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.
ExtractSuperConstructorArgumentrecipe (wired intoUpgradeToJava25, gated on Java 25+) that hoists complexsuper(..)arguments — method invocations and object creations — into local variables declared right before thesuper(..)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 wholesuper(..)is skipped when no argument does real work, when a parameter type isn't safely denotable (generics/arrays/type variables), or forthis(..)delegation. Relates to Move statements beforesuper(..)in Java constructor #733, scoped deliberately to the safe subset — statements that followsuper(..)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.