Skip to content

Add PowerMockWhiteboxInvokeConstructorToJavaReflection recipe#1027

Merged
timtebeek merged 1 commit into
mainfrom
MBoegers/whitebox-invokeconstructor
Jun 18, 2026
Merged

Add PowerMockWhiteboxInvokeConstructorToJavaReflection recipe#1027
timtebeek merged 1 commit into
mainfrom
MBoegers/whitebox-invokeconstructor

Conversation

@MBoegers

Copy link
Copy Markdown
Collaborator

What

Adds the final per-API recipe migrating Whitebox.invokeConstructor(..) to java.lang.reflect.Constructor lookup + newInstance(), registered in the PowerMockWhiteboxToJavaReflection composite. Handles both the Object... varargs overload (parameter types resolved from the unambiguously matched constructor, else arg.getClass()) and the explicit Class[]/Object[] overload. Arrays passed to the Object... form are ambiguous and left unchanged.

MyService s = Whitebox.invokeConstructor(MyService.class, "Alice", 42);
// →
Constructor<MyService> c = MyService.class.getDeclaredConstructor(String.class, int.class);
c.setAccessible(true);
MyService s = c.newInstance("Alice", 42);

How

Reuses the base hasArrayArg helper; constructor resolution and class-literal helpers are kept private to the recipe, matching the existing InvokeMethod recipe — no shared-base changes. This completes Whitebox coverage (setInternalState, getInternalState, invokeMethod, getField, getMethod, invokeConstructor).

Tests

5 cases incl. explicit param types, bare-statement, and the Object[] varargs no-op guard. recipeCsvValidate passes.

Whitebox.invokeConstructor(Class, args...) -> Constructor lookup +
newInstance() on the named class:

    Constructor<MyService> c = MyService.class.getDeclaredConstructor(String.class);
    c.setAccessible(true);
    MyService s = c.newInstance("Alice");

Handles both the Object... varargs overload (param types resolved from the
unambiguously matched constructor, else arg.getClass()) and the explicit
Class[]/Object[] overload. Arrays passed to the Object... varargs form are
ambiguous and left unchanged. Registered in the composite.

Reuses the base hasArrayArg helper; the constructor resolution and class-literal
helpers are kept private to the recipe, matching the existing InvokeMethod
recipe (no shared-base changes).
@github-project-automation github-project-automation Bot moved this from In Progress to Ready to Review in OpenRewrite Jun 18, 2026
@timtebeek timtebeek merged commit b700d7b into main Jun 18, 2026
2 checks passed
@timtebeek timtebeek deleted the MBoegers/whitebox-invokeconstructor branch June 18, 2026 16:17
@github-project-automation github-project-automation Bot moved this from Ready to Review to Done in OpenRewrite Jun 18, 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