Skip to content

Fix ExtractExplicitConstructorInvocationArguments dropping extracted declarations - #1297

Merged
timtebeek merged 3 commits into
mainfrom
tim/rewrite-migrate-java-issue-1289
Oct 15, 2026
Merged

timtebeek merged 3 commits into
mainfrom
tim/rewrite-migrate-java-issue-1289

Conversation

@timtebeek

@timtebeek timtebeek commented Oct 15, 2026 •

Copy link
Copy Markdown
Member

Problem

ExtractExplicitConstructorInvocationArguments replaced super(..) / this(..) arguments with references to new local variables, but never inserted the declarations, so the result didn't compile:

// before
Child(String name) {
    super(Objects.requireNonNull(name));
}

// after (broken)
Child(String name) {
    super(name1);
}

The root cause was in JavaTemplate, which treated the constructor call as an expression and silently dropped the inserted statements. That is fixed upstream in openrewrite/rewrite#9206, which this PR relies on.

Changes

  • Give the new declarations the constructor's parameter types. A context-free template can't resolve types declared in other sources of the project, so these were left untyped and their imports weren't added.
  • Point the arguments at the typed variables from those declarations, instead of rewriting the argument list with a second template.
  • Add a test where the extracted variable's type needs a new import.
  • Fix an assertion in extractWithSourcePathWideningSupertype that also matched a constructor parameter.

Why the tests didn't catch it

The test class is gated on @EnabledForJreRange(min = JAVA_25), and CI runs tests on an older JVM, so all of them are skipped. When run on Java 25, the existing tests fail without these changes, and all 20 pass with them.

…declarations

A constructor invocation is typed as returning the constructed class, so the
context-free template treated super(..)/this(..) as an expression and silently
dropped the inserted declarations, leaving references to undeclared variables.

Fixes #1289
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.

ExtractExplicitConstructorInvocationArguments drops the extracted variable declaration when run via rewrite-maven-plugin

1 participant