fix android support

This commit is contained in:
shaojin.wensj 2023-10-05 13:21:33 +08:00
parent df5ce43cad
commit 72d9f88561
11 changed files with 46 additions and 31 deletions

View File

@ -39,11 +39,11 @@ android {
dependencies { dependencies {
implementation 'com.alibaba:fastjson:1.1.73.android' implementation 'com.alibaba:fastjson:1.1.73.android'
implementation 'com.alibaba.fastjson2:fastjson2:2.0.34.1.android4' implementation 'com.alibaba.fastjson2:fastjson2:2.0.41-SNAPSHOT'
implementation ('com.alibaba.fastjson2:fastjson2-codegen:2.0.34') { implementation ('com.alibaba.fastjson2:fastjson2-codegen:2.0.40') {
exclude group: 'com.alibaba.fastjson2', module: 'fastjson2' 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' exclude group: 'com.alibaba.fastjson2', module: 'fastjson2'
} }

View File

@ -4,7 +4,7 @@ import static org.junit.Assert.assertEquals;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import org.junit.jupiter.api.Test; import org.junit.Test;
public class Compatible1xTest { public class Compatible1xTest {
@Test @Test

View File

@ -5,7 +5,7 @@ import android.util.Log;
import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.android.eishay.MediaContent; import com.alibaba.fastjson2.android.eishay.MediaContent;
import org.junit.jupiter.api.Test; import org.junit.Test;
public class EishayTest { public class EishayTest {
static final String str = "{\"images\": [{\n" + static final String str = "{\"images\": [{\n" +

View File

@ -4,7 +4,7 @@ import com.alibaba.fastjson2.JSONB;
import com.alibaba.fastjson2.JSONReader; import com.alibaba.fastjson2.JSONReader;
import com.alibaba.fastjson2.JSONWriter; import com.alibaba.fastjson2.JSONWriter;
import org.junit.jupiter.api.Test; import org.junit.Test;
public class Issue715 { public class Issue715 {
@Test @Test

View File

@ -5,7 +5,7 @@ import static org.junit.Assert.assertEquals;
import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.annotation.JSONField; import com.alibaba.fastjson2.annotation.JSONField;
import org.junit.jupiter.api.Test; import org.junit.Test;
import java.util.Date; import java.util.Date;

View File

@ -1,6 +1,6 @@
package com.alibaba.fastjson2.android; package com.alibaba.fastjson2.android;
import org.junit.jupiter.api.Test; import org.junit.Test;
import static org.junit.Assert.*; import static org.junit.Assert.*;

View File

@ -1,6 +1,6 @@
package com.alibaba.fastjson2.android; package com.alibaba.fastjson2.android;
import org.junit.jupiter.api.Test; import org.junit.Test;
import static org.junit.Assert.*; import static org.junit.Assert.*;

View File

@ -1,6 +1,6 @@
package com.alibaba.fastjson2.android; package com.alibaba.fastjson2.android;
import org.junit.jupiter.api.Test; import org.junit.Test;
import java.lang.reflect.Field; import java.lang.reflect.Field;

View File

@ -6,7 +6,7 @@ import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.annotation.JSONCreator; import com.alibaba.fastjson2.annotation.JSONCreator;
import com.alibaba.fastjson2.annotation.JSONField; import com.alibaba.fastjson2.annotation.JSONField;
import org.junit.jupiter.api.Test; import org.junit.Test;
public class ValueTest { public class ValueTest {
@Test @Test

View File

@ -17,6 +17,7 @@ import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.*; import java.util.concurrent.atomic.*;
import java.util.function.Consumer; import java.util.function.Consumer;
import static com.alibaba.fastjson2.util.JDKUtils.ANDROID_SDK_INT;
import static com.alibaba.fastjson2.util.JDKUtils.JVM_VERSION; import static com.alibaba.fastjson2.util.JDKUtils.JVM_VERSION;
/** /**
@ -61,7 +62,7 @@ public abstract class BeanUtils {
}; };
public static String[] getRecordFieldNames(Class<?> recordType) { public static String[] getRecordFieldNames(Class<?> recordType) {
if (JVM_VERSION < 14) { if (JVM_VERSION < 14 && ANDROID_SDK_INT < 33) {
return new String[0]; return new String[0];
} }

View File

@ -40,6 +40,7 @@ public class JDKUtils {
public static final boolean ANDROID; public static final boolean ANDROID;
public static final boolean GRAAL; public static final boolean GRAAL;
public static final boolean OPENJ9; public static final boolean OPENJ9;
public static final int ANDROID_SDK_INT;
// Android not support // Android not support
public static final Class CLASS_TRANSIENT; public static final Class CLASS_TRANSIENT;
@ -82,7 +83,7 @@ public class JDKUtils {
ARRAY_BYTE_BASE_OFFSET = offset; ARRAY_BYTE_BASE_OFFSET = offset;
ARRAY_CHAR_BASE_OFFSET = charOffset; ARRAY_CHAR_BASE_OFFSET = charOffset;
int jvmVersion = -1; int jvmVersion = -1, android_sdk_int = -1;
boolean openj9 = false, android = false, graal = false; boolean openj9 = false, android = false, graal = false;
try { try {
String jmvName = System.getProperty("java.vm.name"); String jmvName = System.getProperty("java.vm.name");
@ -101,6 +102,12 @@ public class JDKUtils {
if (javaSpecVer.indexOf('.') == -1) { if (javaSpecVer.indexOf('.') == -1) {
jvmVersion = Integer.parseInt(javaSpecVer); jvmVersion = Integer.parseInt(javaSpecVer);
} }
if (android) {
android_sdk_int = Class.forName("android.os.Build$VERSION")
.getField("SDK_INT")
.getInt(null);
}
} catch (Throwable e) { } catch (Throwable e) {
initErrorLast = e; initErrorLast = e;
} }
@ -108,6 +115,7 @@ public class JDKUtils {
OPENJ9 = openj9; OPENJ9 = openj9;
ANDROID = android; ANDROID = android;
GRAAL = graal; GRAAL = graal;
ANDROID_SDK_INT = android_sdk_int;
boolean hasJavaSql = true; boolean hasJavaSql = true;
Class dataSourceClass = null; Class dataSourceClass = null;
@ -136,12 +144,14 @@ public class JDKUtils {
if (JVM_VERSION == 8) { if (JVM_VERSION == 8) {
Field field = null; Field field = null;
long fieldOffset = -1; long fieldOffset = -1;
try { if (!ANDROID) {
field = String.class.getDeclaredField("value"); try {
field.setAccessible(true); field = String.class.getDeclaredField("value");
fieldOffset = UNSAFE.objectFieldOffset(field); field.setAccessible(true);
} catch (Exception ignored) { fieldOffset = UNSAFE.objectFieldOffset(field);
FIELD_STRING_VALUE_ERROR = true; } catch (Exception ignored) {
FIELD_STRING_VALUE_ERROR = true;
}
} }
FIELD_STRING_VALUE = field; FIELD_STRING_VALUE = field;
@ -153,22 +163,26 @@ public class JDKUtils {
} else { } else {
Field fieldValue = null; Field fieldValue = null;
long fieldValueOffset = -1; long fieldValueOffset = -1;
try { if (!ANDROID) {
fieldValue = String.class.getDeclaredField("value"); try {
fieldValueOffset = UNSAFE.objectFieldOffset(fieldValue); fieldValue = String.class.getDeclaredField("value");
} catch (Exception ignored) { fieldValueOffset = UNSAFE.objectFieldOffset(fieldValue);
FIELD_STRING_VALUE_ERROR = true; } catch (Exception ignored) {
FIELD_STRING_VALUE_ERROR = true;
}
} }
FIELD_STRING_VALUE_OFFSET = fieldValueOffset; FIELD_STRING_VALUE_OFFSET = fieldValueOffset;
FIELD_STRING_VALUE = fieldValue; FIELD_STRING_VALUE = fieldValue;
Field fieldCode = null; Field fieldCode = null;
long fieldCodeOffset = -1; long fieldCodeOffset = -1;
try { if (!ANDROID) {
fieldCode = String.class.getDeclaredField("coder"); try {
fieldCodeOffset = UNSAFE.objectFieldOffset(fieldCode); fieldCode = String.class.getDeclaredField("coder");
} catch (Exception ignored) { fieldCodeOffset = UNSAFE.objectFieldOffset(fieldCode);
FIELD_STRING_CODER_ERROR = true; } catch (Exception ignored) {
FIELD_STRING_CODER_ERROR = true;
}
} }
FIELD_STRING_CODER_OFFSET = fieldCodeOffset; FIELD_STRING_CODER_OFFSET = fieldCodeOffset;
FIELD_STRING_CODER = fieldCode; FIELD_STRING_CODER = fieldCode;
@ -204,7 +218,7 @@ public class JDKUtils {
Function<String, byte[]> stringValue = null; Function<String, byte[]> stringValue = null;
MethodHandles.Lookup trustedLookup = null; MethodHandles.Lookup trustedLookup = null;
{ if (!ANDROID) {
try { try {
Class lookupClass = MethodHandles.Lookup.class; Class lookupClass = MethodHandles.Lookup.class;
Field implLookup = lookupClass.getDeclaredField("IMPL_LOOKUP"); Field implLookup = lookupClass.getDeclaredField("IMPL_LOOKUP");
@ -216,8 +230,8 @@ public class JDKUtils {
if (trustedLookup == null) { if (trustedLookup == null) {
trustedLookup = MethodHandles.lookup(); trustedLookup = MethodHandles.lookup();
} }
IMPL_LOOKUP = trustedLookup;
} }
IMPL_LOOKUP = trustedLookup;
int vector_bit_length = -1; int vector_bit_length = -1;
boolean vector_support = false; boolean vector_support = false;