Skip to content

Fix/change method target to static constructor wildcard#7399

Merged
timtebeek merged 3 commits intoopenrewrite:mainfrom
kdvolder:fix/change-method-target-to-static-constructor-wildcard
Apr 16, 2026
Merged

Fix/change method target to static constructor wildcard#7399
timtebeek merged 3 commits intoopenrewrite:mainfrom
kdvolder:fix/change-method-target-to-static-constructor-wildcard

Conversation

@kdvolder
Copy link
Copy Markdown
Contributor

@kdvolder kdvolder commented Apr 16, 2026

What's changed?

Added a guard clause to ChangeMethodTargetToStatic.visitNewClass so it only transforms constructors into static method calls when the method pattern explicitly targets a constructor (e.g., using <constructor> or <init>).

What's your motivation?

This fixes a bug where ChangeMethodTargetToStatic was incorrectly matching and transforming all new object creations when provided with a broad wildcard method pattern like *(..). This broke declarative recipes that used wildcards to match all methods on a class.

Anything in particular you'd like reviewers to focus on?

Have you considered any alternatives or workarounds?

An alternative would be changing MethodMatcher itself so that the * wildcard does not match <init>, but that would be a much larger breaking change to core matching behavior that has been in place since 2022.

Checklist

  • I've added unit tests to cover both positive and negative cases
  • I've read and applied the recipe conventions and best practices
  • I've used the IntelliJ IDEA auto-formatter on affected files

@timtebeek
Copy link
Copy Markdown
Member

Yes makes sense; thanks for the immediate fix @kdvolder !

@github-project-automation github-project-automation bot moved this from In Progress to Ready to Review in OpenRewrite Apr 16, 2026
@timtebeek timtebeek merged commit cd544e2 into openrewrite:main Apr 16, 2026
1 check passed
@github-project-automation github-project-automation bot moved this from Ready to Review to Done in OpenRewrite Apr 16, 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