Skip to content

Commit

Permalink
fix android support
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Oct 5, 2023
1 parent df5ce43 commit 72d9f88
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 31 deletions.
6 changes: 3 additions & 3 deletions android-test/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import com.alibaba.fastjson.JSON;

import org.junit.jupiter.api.Test;
import org.junit.Test;

public class Compatible1xTest {
@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.alibaba.fastjson2.android;

import org.junit.jupiter.api.Test;
import org.junit.Test;

import static org.junit.Assert.*;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.alibaba.fastjson2.android;

import org.junit.jupiter.api.Test;
import org.junit.Test;

import static org.junit.Assert.*;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.alibaba.fastjson2.android;

import org.junit.jupiter.api.Test;
import org.junit.Test;

import java.lang.reflect.Field;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion core/src/main/java/com/alibaba/fastjson2/util/BeanUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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];
}

Expand Down
52 changes: 33 additions & 19 deletions core/src/main/java/com/alibaba/fastjson2/util/JDKUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand All @@ -101,13 +102,20 @@ 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;
}

OPENJ9 = openj9;
ANDROID = android;
GRAAL = graal;
ANDROID_SDK_INT = android_sdk_int;

boolean hasJavaSql = true;
Class dataSourceClass = null;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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");
Expand All @@ -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;
Expand Down

0 comments on commit 72d9f88

Please sign in to comment.