Skip to content

fix: avoid NPE in trustedLookup on environments without IMPL_LOOKUP, for issue #4011#7633

Open
wenshao wants to merge 1 commit intomainfrom
fix/issue-4011-android-methodhandles-npe
Open

fix: avoid NPE in trustedLookup on environments without IMPL_LOOKUP, for issue #4011#7633
wenshao wants to merge 1 commit intomainfrom
fix/issue-4011-android-methodhandles-npe

Conversation

@wenshao
Copy link
Copy Markdown
Member

@wenshao wenshao commented Apr 25, 2026

Summary

  • On Android the static initializer leaves JDKUtils.IMPL_LOOKUP null (the Unsafe-based access to MethodHandles.Lookup.IMPL_LOOKUP is intentionally gated to !ANDROID). trustedLookup(Class) then NPE'd at IMPL_LOOKUP.in(...), producing the user-visible "Attempt to invoke virtual method MethodHandles$Lookup.in(...) on a null object reference" when parsing Kotlin data classes via JSON.parseObject on Android 16 (API 36).
  • JDKUtils.trustedLookup: short-circuit and return null when IMPL_LOOKUP is unavailable. Most existing callers already wrap usage in try/catch and fall back to reflection-based access, so a null Lookup is the cleanest signal.
  • ObjectReaderCreator.lambdaSetter / createBuildFunctionLambda: handle the null lookup explicitly and return null so callers fall back to reflection instead of throwing JSONException(\"create fieldReader error\").

Fixes #4011.

Test plan

  • Existing JDKUtilsTest (uses trustedLookup with non-null IMPL_LOOKUP) still passes.
  • ObjectReaderCreator*Test*, LambdaSetterTest* — all 38 tests pass.
  • Verified on Android (would need an Android device run; reproducer in issue body).

…for issue #4011

On Android, the static initializer leaves IMPL_LOOKUP null because the
Unsafe-based access to MethodHandles.Lookup.IMPL_LOOKUP is gated to
!ANDROID. trustedLookup(Class) then NPE'd at IMPL_LOOKUP.in(...),
producing the user-visible "Attempt to invoke virtual method
MethodHandles$Lookup.in(...) on a null object reference" when parsing
Kotlin data classes via JSON.parseObject on Android 16 (API 36).

- JDKUtils.trustedLookup: short-circuit and return null when IMPL_LOOKUP
  is unavailable. Most existing callers already wrap usage in try/catch
  and fall back to reflection-based access, so a null Lookup is the
  cleanest signal.
- ObjectReaderCreator.lambdaSetter / createBuildFunctionLambda: handle
  the null Lookup explicitly and return null so callers fall back to
  reflection instead of throwing JSONException("create fieldReader error").
@CLAassistant
Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.


wenshao seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

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.

[BUG] NullPointerException: java.lang.invoke.MethodHandles$Lookup java.lang.invoke.MethodHandles$Lookup.in(java.lang.Class)

2 participants