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 {
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'
}

View File

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

View File

@ -5,7 +5,7 @@ import android.util.Log;
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" +

View File

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

View File

@ -5,7 +5,7 @@ import static org.junit.Assert.assertEquals;
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;

View File

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

View File

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

View File

@ -1,6 +1,6 @@
package com.alibaba.fastjson2.android;
import org.junit.jupiter.api.Test;
import org.junit.Test;
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.JSONField;
import org.junit.jupiter.api.Test;
import org.junit.Test;
public class ValueTest {
@Test

View File

@ -17,6 +17,7 @@ import java.util.concurrent.ConcurrentMap;
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];
}

View File

@ -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,6 +144,7 @@ public class JDKUtils {
if (JVM_VERSION == 8) {
Field field = null;
long fieldOffset = -1;
if (!ANDROID) {
try {
field = String.class.getDeclaredField("value");
field.setAccessible(true);
@ -143,6 +152,7 @@ public class JDKUtils {
} catch (Exception ignored) {
FIELD_STRING_VALUE_ERROR = true;
}
}
FIELD_STRING_VALUE = field;
FIELD_STRING_VALUE_OFFSET = fieldOffset;
@ -153,23 +163,27 @@ public class JDKUtils {
} else {
Field fieldValue = null;
long fieldValueOffset = -1;
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;
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<String, byte[]> 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;