Skip to content

Support changing from a constructor to a factory method in ChangeMethodTargetToStatic#7281

Merged
timtebeek merged 1 commit intomainfrom
tim/constructor-to-static
Apr 6, 2026
Merged

Support changing from a constructor to a factory method in ChangeMethodTargetToStatic#7281
timtebeek merged 1 commit intomainfrom
tim/constructor-to-static

Conversation

@timtebeek
Copy link
Copy Markdown
Member

@timtebeek timtebeek commented Apr 6, 2026

Summary

ChangeMethodTargetToStatic now handles <constructor> method patterns by adding a visitNewClass override that converts matched new Foo(args) calls into TargetType.Foo(args) static method invocations. The constructor's simple class name becomes the initial method name, so users chain ChangeMethodName to get the final name (e.g., getLogger). Anonymous class instantiations and inner class constructors are skipped. The visitor base class was changed from JavaIsoVisitor to JavaVisitor to allow returning J.MethodInvocation from visitNewClass.

Test plan

  • constructorToStaticMethod — full chain with ChangeMethodName matching the issue's use case
  • constructorToStaticMethodWithReturnType — verifies the returnType option works
  • constructorWithAnonymousBodyNotChanged — anonymous class bodies left unchanged
  • All existing ChangeMethodTargetToStaticTest tests still pass

@github-project-automation github-project-automation Bot moved this to In Progress in OpenRewrite Apr 6, 2026
@timtebeek timtebeek merged commit 445d977 into main Apr 6, 2026
1 check passed
@timtebeek timtebeek deleted the tim/constructor-to-static branch April 6, 2026 09:54
@github-project-automation github-project-automation Bot moved this from In Progress to Done in OpenRewrite Apr 6, 2026
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.

Support changing from a constructor to a factory method in ChangeMethodTargetToStatic

1 participant