From 1fa051eb6b497ef6c55b0ebfd811e783329221ba Mon Sep 17 00:00:00 2001 From: "shaojin.wensj" Date: Sun, 7 Apr 2024 13:24:59 +0800 Subject: [PATCH] support graalvm native-image, for issue #2406 --- .../java/com/alibaba/fastjson2/util/JDKUtils.java | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/core/src/main/java/com/alibaba/fastjson2/util/JDKUtils.java b/core/src/main/java/com/alibaba/fastjson2/util/JDKUtils.java index 0a8632b061..3d31f7cd0c 100644 --- a/core/src/main/java/com/alibaba/fastjson2/util/JDKUtils.java +++ b/core/src/main/java/com/alibaba/fastjson2/util/JDKUtils.java @@ -71,22 +71,13 @@ public class JDKUtils { Unsafe unsafe; long offset, charOffset; try { - Field theUnsafeField = null; - for (Field field : Unsafe.class.getDeclaredFields()) { - String fieldName = field.getName(); - if (fieldName.equals("theUnsafe") - || fieldName.equals("THE_ONE") // android - ) { - theUnsafeField = field; - break; - } - } + Field theUnsafeField = Unsafe.class.getDeclaredField("theUnsafe"); theUnsafeField.setAccessible(true); unsafe = (Unsafe) theUnsafeField.get(null); offset = unsafe.arrayBaseOffset(byte[].class); charOffset = unsafe.arrayBaseOffset(char[].class); } catch (Throwable e) { - throw new JSONException("init unsafe error"); + throw new JSONException("init unsafe error", e); } UNSAFE = unsafe;