Skip to content

Add PowerMockWhiteboxGetFieldToJavaReflection recipe#1025

Merged
timtebeek merged 1 commit into
mainfrom
MBoegers/whitebox-getfield
Jun 17, 2026
Merged

Add PowerMockWhiteboxGetFieldToJavaReflection recipe#1025
timtebeek merged 1 commit into
mainfrom
MBoegers/whitebox-getfield

Conversation

@MBoegers

Copy link
Copy Markdown
Collaborator

What

Adds a per-API recipe migrating Whitebox.getField(Class, String) to Class.getDeclaredField(String) + setAccessible(true), and registers it in the PowerMockWhiteboxToJavaReflection composite. The migrated call's result is the reflective Field, so the recipe reuses the declared result variable as the local:

Field f = Whitebox.getField(MyService.class, "name");
// →
Field f = MyService.class.getDeclaredField("name");
f.setAccessible(true);

How

A shared resultLocalName helper on WhiteboxToReflectionVisitor returns the declared variable when present (result-is-the-object APIs like getField/getMethod), else generates a readable name (nameField, with INCREMENT_NUMBER uniqueness).

Notes

  • Like the existing recipes, getDeclaredField does not walk the class hierarchy — documented in the recipe description.
  • First of three new-API recipes extending Whitebox coverage (getFieldgetMethodinvokeConstructor).

Tests

@DocumentExample for the migration plus a no-op guard confirming other Whitebox APIs are untouched. recipeCsvValidate (completeness + content) passes.

Whitebox.getField(Class, "name") returns a reflective Field; migrate it to
Class.getDeclaredField("name") + setAccessible(true), reusing the result
variable as the local. Registered in the PowerMockWhiteboxToJavaReflection
composite.

A shared resultLocalName helper (reuse the declared result variable when the
call's result IS the reflective object) is added to WhiteboxToReflectionVisitor.
@github-project-automation github-project-automation Bot moved this from In Progress to Ready to Review in OpenRewrite Jun 17, 2026
@timtebeek timtebeek merged commit 96f93cc into main Jun 17, 2026
1 check passed
@timtebeek timtebeek deleted the MBoegers/whitebox-getfield branch June 17, 2026 10:35
@github-project-automation github-project-automation Bot moved this from Ready to Review to Done in OpenRewrite Jun 17, 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.

2 participants