Skip to content

Commit 8a65c7c

Browse files
wenshaowenshao
authored andcommitted
fix: avoid NPE in trustedLookup on environments without IMPL_LOOKUP, 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").
1 parent 3697c2d commit 8a65c7c

2 files changed

Lines changed: 18 additions & 0 deletions

File tree

‎core/src/main/java/com/alibaba/fastjson2/reader/ObjectReaderCreator.java‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2186,6 +2186,9 @@ public <T, R> Function<T, R> createBuildFunction(Method builderMethod) {
21862186
*/
21872187
<T, R> Function<T, R> createBuildFunctionLambda(Method builderMethod) {
21882188
MethodHandles.Lookup lookup = JDKUtils.trustedLookup(builderMethod.getDeclaringClass());
2189+
if (lookup == null) {
2190+
return null;
2191+
}
21892192
try {
21902193
MethodHandle target = lookup.findVirtual(builderMethod.getDeclaringClass(),
21912194
builderMethod.getName(),
@@ -3994,6 +3997,12 @@ protected <T> FieldReader createFieldReaderLambda(
39943997

39953998
protected Object lambdaSetter(Class objectClass, Class fieldClass, Method method) {
39963999
MethodHandles.Lookup lookup = JDKUtils.trustedLookup(objectClass);
4000+
if (lookup == null) {
4001+
// Trusted MethodHandles.Lookup not available (e.g. Android, where
4002+
// IMPL_LOOKUP cannot be obtained via Unsafe). Skip lambda creation
4003+
// and let the caller fall back to reflection-based access.
4004+
return null;
4005+
}
39974006

39984007
Class<?> returnType = method.getReturnType();
39994008
LambdaSetterInfo lambdaInfo = methodTypeMapping.get(fieldClass);

‎core/src/main/java/com/alibaba/fastjson2/util/JDKUtils.java‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,15 @@ public static char[] getCharArray(String str) {
470470
}
471471

472472
public static MethodHandles.Lookup trustedLookup(Class objectClass) {
473+
// IMPL_LOOKUP is null on Android (see static init guard) and may also be
474+
// null on environments where neither the Unsafe-based access nor
475+
// MethodHandles.lookup() were available. All callers wrap usage in a
476+
// try/catch and fall back to plain reflection, so signal that here by
477+
// returning null instead of NPE'ing on the .in(...) call below.
478+
if (IMPL_LOOKUP == null) {
479+
return null;
480+
}
481+
473482
if (!CONSTRUCTOR_LOOKUP_ERROR) {
474483
try {
475484
int TRUSTED = -1;

0 commit comments

Comments
 (0)