From 72d9f88561e266f2c0d43a9efb463fefb646eb35 Mon Sep 17 00:00:00 2001 From: "shaojin.wensj" Date: Thu, 5 Oct 2023 13:21:33 +0800 Subject: [PATCH] fix android support --- android-test/app/build.gradle | 6 +-- .../fastjson2/android/Compatible1xTest.java | 2 +- .../alibaba/fastjson2/android/EishayTest.java | 2 +- .../alibaba/fastjson2/android/Issue715.java | 2 +- .../fastjson2/android/IssueIssue1349.java | 2 +- .../alibaba/fastjson2/android/JSONTest.java | 2 +- .../alibaba/fastjson2/android/UnsafeTest.java | 2 +- .../fastjson2/android/UnsafeUtilsTest.java | 2 +- .../alibaba/fastjson2/android/ValueTest.java | 2 +- .../com/alibaba/fastjson2/util/BeanUtils.java | 3 +- .../com/alibaba/fastjson2/util/JDKUtils.java | 52 ++++++++++++------- 11 files changed, 46 insertions(+), 31 deletions(-) diff --git a/android-test/app/build.gradle b/android-test/app/build.gradle index e0cbb8844..c97f1c77e 100644 --- a/android-test/app/build.gradle +++ b/android-test/app/build.gradle @@ -39,11 +39,11 @@ android { dependencies { implementation 'com.alibaba:fastjson:1.1.73.android' - implementation 'com.alibaba.fastjson2:fastjson2:2.0.34.1.android4' - implementation ('com.alibaba.fastjson2:fastjson2-codegen:2.0.34') { + implementation 'com.alibaba.fastjson2:fastjson2:2.0.41-SNAPSHOT' + implementation ('com.alibaba.fastjson2:fastjson2-codegen:2.0.40') { exclude group: 'com.alibaba.fastjson2', module: 'fastjson2' } - implementation ('com.alibaba.fastjson2:fastjson2-kotlin:2.0.34') { + implementation ('com.alibaba.fastjson2:fastjson2-kotlin:2.0.40') { exclude group: 'com.alibaba.fastjson2', module: 'fastjson2' } diff --git a/android-test/app/src/androidTest/java/com/alibaba/fastjson2/android/Compatible1xTest.java b/android-test/app/src/androidTest/java/com/alibaba/fastjson2/android/Compatible1xTest.java index 89b027568..1d82f854f 100644 --- a/android-test/app/src/androidTest/java/com/alibaba/fastjson2/android/Compatible1xTest.java +++ b/android-test/app/src/androidTest/java/com/alibaba/fastjson2/android/Compatible1xTest.java @@ -4,7 +4,7 @@ import com.alibaba.fastjson.JSON; -import org.junit.jupiter.api.Test; +import org.junit.Test; public class Compatible1xTest { @Test diff --git a/android-test/app/src/androidTest/java/com/alibaba/fastjson2/android/EishayTest.java b/android-test/app/src/androidTest/java/com/alibaba/fastjson2/android/EishayTest.java index 71a052cba..15a500f85 100644 --- a/android-test/app/src/androidTest/java/com/alibaba/fastjson2/android/EishayTest.java +++ b/android-test/app/src/androidTest/java/com/alibaba/fastjson2/android/EishayTest.java @@ -5,7 +5,7 @@ import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.android.eishay.MediaContent; -import org.junit.jupiter.api.Test; +import org.junit.Test; public class EishayTest { static final String str = "{\"images\": [{\n" + diff --git a/android-test/app/src/androidTest/java/com/alibaba/fastjson2/android/Issue715.java b/android-test/app/src/androidTest/java/com/alibaba/fastjson2/android/Issue715.java index 73b5b59e8..afdc57b05 100644 --- a/android-test/app/src/androidTest/java/com/alibaba/fastjson2/android/Issue715.java +++ b/android-test/app/src/androidTest/java/com/alibaba/fastjson2/android/Issue715.java @@ -4,7 +4,7 @@ import com.alibaba.fastjson2.JSONReader; import com.alibaba.fastjson2.JSONWriter; -import org.junit.jupiter.api.Test; +import org.junit.Test; public class Issue715 { @Test diff --git a/android-test/app/src/androidTest/java/com/alibaba/fastjson2/android/IssueIssue1349.java b/android-test/app/src/androidTest/java/com/alibaba/fastjson2/android/IssueIssue1349.java index 9a3470648..1976d7941 100644 --- a/android-test/app/src/androidTest/java/com/alibaba/fastjson2/android/IssueIssue1349.java +++ b/android-test/app/src/androidTest/java/com/alibaba/fastjson2/android/IssueIssue1349.java @@ -5,7 +5,7 @@ import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.annotation.JSONField; -import org.junit.jupiter.api.Test; +import org.junit.Test; import java.util.Date; diff --git a/android-test/app/src/androidTest/java/com/alibaba/fastjson2/android/JSONTest.java b/android-test/app/src/androidTest/java/com/alibaba/fastjson2/android/JSONTest.java index cc1bd048b..3c4258032 100644 --- a/android-test/app/src/androidTest/java/com/alibaba/fastjson2/android/JSONTest.java +++ b/android-test/app/src/androidTest/java/com/alibaba/fastjson2/android/JSONTest.java @@ -1,6 +1,6 @@ package com.alibaba.fastjson2.android; -import org.junit.jupiter.api.Test; +import org.junit.Test; import static org.junit.Assert.*; diff --git a/android-test/app/src/androidTest/java/com/alibaba/fastjson2/android/UnsafeTest.java b/android-test/app/src/androidTest/java/com/alibaba/fastjson2/android/UnsafeTest.java index 4efda6d96..097acbb9d 100644 --- a/android-test/app/src/androidTest/java/com/alibaba/fastjson2/android/UnsafeTest.java +++ b/android-test/app/src/androidTest/java/com/alibaba/fastjson2/android/UnsafeTest.java @@ -1,6 +1,6 @@ package com.alibaba.fastjson2.android; -import org.junit.jupiter.api.Test; +import org.junit.Test; import static org.junit.Assert.*; diff --git a/android-test/app/src/androidTest/java/com/alibaba/fastjson2/android/UnsafeUtilsTest.java b/android-test/app/src/androidTest/java/com/alibaba/fastjson2/android/UnsafeUtilsTest.java index 891e138bb..e0a9cfc2d 100644 --- a/android-test/app/src/androidTest/java/com/alibaba/fastjson2/android/UnsafeUtilsTest.java +++ b/android-test/app/src/androidTest/java/com/alibaba/fastjson2/android/UnsafeUtilsTest.java @@ -1,6 +1,6 @@ package com.alibaba.fastjson2.android; -import org.junit.jupiter.api.Test; +import org.junit.Test; import java.lang.reflect.Field; diff --git a/android-test/app/src/androidTest/java/com/alibaba/fastjson2/android/ValueTest.java b/android-test/app/src/androidTest/java/com/alibaba/fastjson2/android/ValueTest.java index 52f1c3216..3a6d27ab4 100644 --- a/android-test/app/src/androidTest/java/com/alibaba/fastjson2/android/ValueTest.java +++ b/android-test/app/src/androidTest/java/com/alibaba/fastjson2/android/ValueTest.java @@ -6,7 +6,7 @@ import com.alibaba.fastjson2.annotation.JSONCreator; import com.alibaba.fastjson2.annotation.JSONField; -import org.junit.jupiter.api.Test; +import org.junit.Test; public class ValueTest { @Test diff --git a/core/src/main/java/com/alibaba/fastjson2/util/BeanUtils.java b/core/src/main/java/com/alibaba/fastjson2/util/BeanUtils.java index 9e75db36f..045efe0b6 100644 --- a/core/src/main/java/com/alibaba/fastjson2/util/BeanUtils.java +++ b/core/src/main/java/com/alibaba/fastjson2/util/BeanUtils.java @@ -17,6 +17,7 @@ import java.util.concurrent.atomic.*; import java.util.function.Consumer; +import static com.alibaba.fastjson2.util.JDKUtils.ANDROID_SDK_INT; import static com.alibaba.fastjson2.util.JDKUtils.JVM_VERSION; /** @@ -61,7 +62,7 @@ public abstract class BeanUtils { }; public static String[] getRecordFieldNames(Class recordType) { - if (JVM_VERSION < 14) { + if (JVM_VERSION < 14 && ANDROID_SDK_INT < 33) { return new String[0]; } 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 681dc2f5c..244fbd325 100644 --- a/core/src/main/java/com/alibaba/fastjson2/util/JDKUtils.java +++ b/core/src/main/java/com/alibaba/fastjson2/util/JDKUtils.java @@ -40,6 +40,7 @@ public class JDKUtils { public static final boolean ANDROID; public static final boolean GRAAL; public static final boolean OPENJ9; + public static final int ANDROID_SDK_INT; // Android not support public static final Class CLASS_TRANSIENT; @@ -82,7 +83,7 @@ public class JDKUtils { ARRAY_BYTE_BASE_OFFSET = offset; ARRAY_CHAR_BASE_OFFSET = charOffset; - int jvmVersion = -1; + int jvmVersion = -1, android_sdk_int = -1; boolean openj9 = false, android = false, graal = false; try { String jmvName = System.getProperty("java.vm.name"); @@ -101,6 +102,12 @@ public class JDKUtils { if (javaSpecVer.indexOf('.') == -1) { jvmVersion = Integer.parseInt(javaSpecVer); } + + if (android) { + android_sdk_int = Class.forName("android.os.Build$VERSION") + .getField("SDK_INT") + .getInt(null); + } } catch (Throwable e) { initErrorLast = e; } @@ -108,6 +115,7 @@ public class JDKUtils { OPENJ9 = openj9; ANDROID = android; GRAAL = graal; + ANDROID_SDK_INT = android_sdk_int; boolean hasJavaSql = true; Class dataSourceClass = null; @@ -136,12 +144,14 @@ public class JDKUtils { if (JVM_VERSION == 8) { Field field = null; long fieldOffset = -1; - try { - field = String.class.getDeclaredField("value"); - field.setAccessible(true); - fieldOffset = UNSAFE.objectFieldOffset(field); - } catch (Exception ignored) { - FIELD_STRING_VALUE_ERROR = true; + if (!ANDROID) { + try { + field = String.class.getDeclaredField("value"); + field.setAccessible(true); + fieldOffset = UNSAFE.objectFieldOffset(field); + } catch (Exception ignored) { + FIELD_STRING_VALUE_ERROR = true; + } } FIELD_STRING_VALUE = field; @@ -153,22 +163,26 @@ public class JDKUtils { } else { Field fieldValue = null; long fieldValueOffset = -1; - try { - fieldValue = String.class.getDeclaredField("value"); - fieldValueOffset = UNSAFE.objectFieldOffset(fieldValue); - } catch (Exception ignored) { - FIELD_STRING_VALUE_ERROR = true; + if (!ANDROID) { + try { + fieldValue = String.class.getDeclaredField("value"); + fieldValueOffset = UNSAFE.objectFieldOffset(fieldValue); + } catch (Exception ignored) { + FIELD_STRING_VALUE_ERROR = true; + } } FIELD_STRING_VALUE_OFFSET = fieldValueOffset; FIELD_STRING_VALUE = fieldValue; Field fieldCode = null; long fieldCodeOffset = -1; - try { - fieldCode = String.class.getDeclaredField("coder"); - fieldCodeOffset = UNSAFE.objectFieldOffset(fieldCode); - } catch (Exception ignored) { - FIELD_STRING_CODER_ERROR = true; + if (!ANDROID) { + try { + fieldCode = String.class.getDeclaredField("coder"); + fieldCodeOffset = UNSAFE.objectFieldOffset(fieldCode); + } catch (Exception ignored) { + FIELD_STRING_CODER_ERROR = true; + } } FIELD_STRING_CODER_OFFSET = fieldCodeOffset; FIELD_STRING_CODER = fieldCode; @@ -204,7 +218,7 @@ public class JDKUtils { Function stringValue = null; MethodHandles.Lookup trustedLookup = null; - { + if (!ANDROID) { try { Class lookupClass = MethodHandles.Lookup.class; Field implLookup = lookupClass.getDeclaredField("IMPL_LOOKUP"); @@ -216,8 +230,8 @@ public class JDKUtils { if (trustedLookup == null) { trustedLookup = MethodHandles.lookup(); } - IMPL_LOOKUP = trustedLookup; } + IMPL_LOOKUP = trustedLookup; int vector_bit_length = -1; boolean vector_support = false;