Skip to content

Refactor invokeSpecialMethod for reflection parity#568

Merged
JingMatrix merged 1 commit intomasterfrom
jni
Mar 14, 2026
Merged

Refactor invokeSpecialMethod for reflection parity#568
JingMatrix merged 1 commit intomasterfrom
jni

Conversation

@JingMatrix
Copy link
Copy Markdown
Owner

We overhaul the invokeSpecialMethod backend to make it robust against JNI aborts and closer to standard Java reflection behavior, while maintaining reasonable performance.

Key improvements:

  • Fix illegal jclass{nullptr} usage during return value boxing by globally caching primitive wrapper classes (Integer, Double, etc.).
  • Eliminate heap allocations for argument mapping by replacing std::vector with stack-allocated memory via alloca.
  • Prevent JNI type confusion crashes by introducing strict type validation (IsInstanceOf) before unboxing arguments.
  • Safely catch native exceptions from the target method and wrap them in InvocationTargetException, mirroring java.lang.reflect.Method.invoke.
  • Gracefully handle null receivers and zero-length argument arrays.

We overhaul the `invokeSpecialMethod` backend to make it robust against JNI aborts and closer to standard Java reflection behavior, while maintaining reasonable performance.

Key improvements:
- Fix illegal `jclass{nullptr}` usage during return value boxing by globally caching primitive wrapper classes (`Integer`, `Double`, etc.).
- Eliminate heap allocations for argument mapping by replacing `std::vector` with stack-allocated memory via `alloca`.
- Prevent JNI type confusion crashes by introducing strict type validation (`IsInstanceOf`) before unboxing arguments.
- Safely catch native exceptions from the target method and wrap them in `InvocationTargetException`, mirroring `java.lang.reflect.Method.invoke`.
- Gracefully handle `null` receivers and zero-length argument arrays.
@JingMatrix JingMatrix merged commit 8db1921 into master Mar 14, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants