Skip to content

Expand PowerMockWhiteboxToJavaReflection to cover more Whitebox APIs#1013

Closed
MBoegers wants to merge 1 commit into
mainfrom
MBoegers/powermock-whitebox-reflection-coverage
Closed

Expand PowerMockWhiteboxToJavaReflection to cover more Whitebox APIs#1013
MBoegers wants to merge 1 commit into
mainfrom
MBoegers/powermock-whitebox-reflection-coverage

Conversation

@MBoegers

@MBoegers MBoegers commented Jun 8, 2026

Copy link
Copy Markdown
Collaborator

The PowerMock → Mockito migration removes the org.powermock:powermock-reflect dependency, but PowerMockWhiteboxToJavaReflection previously migrated only setInternalState/getInternalState/invokeMethod, leaving the rest of the Whitebox API as uncompilable references after the dependency is dropped.

This expands the recipe to also migrate, to plain java.lang.reflect:

  • getField / getMethodgetDeclaredField / getDeclaredMethod + setAccessible
  • invokeConstructor (both overloads) → Constructor.newInstance
  • static invokeMethod(Class, …)invoke(null, …)
  • the Class where getInternalState / setInternalState overloads
  • non-literal field/method names (fall back to a generated local)

Notes:

  • Primitive results are cast through their wrapper ((Integer) etc.), since a direct (int) object cast does not compile.
  • The explicit-Class[] / varargs-array overloads of invokeMethod/invokeConstructor are intentionally left untouched — there is no faithful mechanical translation (a follow-up recipe flags them).
  • Field/constructor lookups use getDeclaredField/getDeclaredConstructor on the named class, which differs from PowerMock for members inherited from a superclass (documented in the recipe description).

Adds unit tests covering each new signature plus the primitive-cast and array-overload cases.

}

private @Nullable String buildReplacementTemplate(Statement statement, J.MethodInvocation mi,
private JavaType.@Nullable Method resolveFor(J.MethodInvocation mi) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might suggest making the name more reflect the fact it's just for the invoke methods

@MBoegers MBoegers force-pushed the MBoegers/powermock-whitebox-reflection-coverage branch 2 times, most recently from 5fc96ec to 4ad8941 Compare June 8, 2026 15:29
Adds migration of getField, getMethod, invokeConstructor, static
invokeMethod(Class,..), the where-Class get/setInternalState overloads,
and non-literal field/method names, alongside the existing
setInternalState/getInternalState/invokeMethod support. Primitive results
are cast through their wrapper type, and the explicit Class[]/varargs-array
overloads of invokeMethod/invokeConstructor are deliberately left untouched
(no faithful mechanical translation).
@MBoegers MBoegers force-pushed the MBoegers/powermock-whitebox-reflection-coverage branch from 4ad8941 to dfdc2d0 Compare June 12, 2026 10:35
@MBoegers

Copy link
Copy Markdown
Collaborator Author

I will reshape the PR.

@MBoegers MBoegers closed this Jun 15, 2026
@github-project-automation github-project-automation Bot moved this from In Progress to Done in OpenRewrite Jun 15, 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