Skip to content

Add PowerMockWhiteboxGetMethodToJavaReflection recipe#1026

Merged
MBoegers merged 2 commits into
mainfrom
MBoegers/whitebox-getmethod
Jun 18, 2026
Merged

Add PowerMockWhiteboxGetMethodToJavaReflection recipe#1026
MBoegers merged 2 commits into
mainfrom
MBoegers/whitebox-getmethod

Conversation

@MBoegers

@MBoegers MBoegers commented Jun 17, 2026

Copy link
Copy Markdown
Collaborator

What

Adds a per-API recipe migrating Whitebox.getMethod(Class, String, Class...) to Class.getDeclaredMethod(String, Class...) + setAccessible(true), registered in the PowerMockWhiteboxToJavaReflection composite. The result is the reflective Method, so the declared result variable is reused as the local:

Method m = Whitebox.getMethod(MyService.class, "greet", String.class);
// →
Method m = MyService.class.getDeclaredMethod("greet", String.class);
m.setAccessible(true);

How

A hasArrayArg helper on WhiteboxToReflectionVisitor skips calls that pass an explicit Class[] varargs array (left for manual migration); the parameter Class literals otherwise pass straight through.

The resultLocalName / fieldVarName / methodVarName helper group was shared with #1025 (getField). Now that #1025 has merged, this PR drops the duplicated helper block and retains only the hasArrayArg helper unique to this recipe.

Notes

  • getDeclaredMethod does not walk the class hierarchy — documented in the description.
  • Second of three new-API recipes (getFieldgetMethodinvokeConstructor).

Tests

4 cases: param type (@DocumentExample), no params, primitive param (int.class), and an explicit-Class[] no-op guard. recipeCsvValidate passes.

Whitebox.getMethod(Class, "m", paramTypes...) returns a reflective Method;
migrate it to Class.getDeclaredMethod("m", paramTypes...) + setAccessible(true),
reusing the result variable as the local. Calls passing an explicit Class[]
varargs array are left unchanged for manual migration. Registered in the
PowerMockWhiteboxToJavaReflection composite.

Adds hasArrayArg and the resultLocalName helper group to
WhiteboxToReflectionVisitor. The resultLocalName group is also introduced by
PR #1025 (getField); whichever merges first, the other rebases onto main and
drops the duplicate.
@MBoegers

Copy link
Copy Markdown
Collaborator Author

@copilot resolve the merge conflicts

Copilot AI commented Jun 18, 2026

Copy link
Copy Markdown
Contributor

@copilot resolve the merge conflicts

Done — merged main (including #1025) into this branch and resolved all three conflicts:

  • WhiteboxToReflectionVisitor.java: dropped the duplicated helper group (already landed via Add PowerMockWhiteboxGetFieldToJavaReflection recipe #1025), kept the hasArrayArg helper unique to this PR.
  • powermockito.yml: combined both getField and getMethod in the description and recipe list.
  • recipes.csv: included entries for both getField and getMethod.

Merge commit: 0cdd3db

@MBoegers MBoegers marked this pull request as ready for review June 18, 2026 14:19
@MBoegers MBoegers requested a review from timtebeek June 18, 2026 14:20
@github-project-automation github-project-automation Bot moved this from In Progress to Ready to Review in OpenRewrite Jun 18, 2026
@MBoegers MBoegers merged commit 20880b9 into main Jun 18, 2026
1 check passed
@MBoegers MBoegers deleted the MBoegers/whitebox-getmethod branch June 18, 2026 15:32
@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

Archived in project

Development

Successfully merging this pull request may close these issues.

3 participants