Skip to content

Commit

Permalink
fix(android): compatible HashMap and ArrayList for ArgumentUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
siguangli2018 committed Mar 22, 2024
1 parent 3a4fe64 commit d591b3b
Showing 1 changed file with 16 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import com.tencent.mtt.hippy.modules.Promise;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
Expand Down Expand Up @@ -125,8 +127,12 @@ private static void parseObjectGotoArray(HippyArray array, Object obj) throws JS
array.pushBoolean((Boolean) obj);
} else if (cls == HippyArray.class) {
array.pushArray((HippyArray) obj);
} else if (cls == ArrayList.class || cls == List.class) {
array.pushArray(new HippyArray((List) obj));
} else if (cls == HippyMap.class) {
array.pushMap((HippyMap) obj);
} else if (cls == HashMap.class || cls == Map.class) {
array.pushMap(new HippyMap((Map) obj));
} else if (cls == JSONArray.class) {
HippyArray arr = new HippyArray();
JSONArray jsonArr = (JSONArray) obj;
Expand Down Expand Up @@ -228,9 +234,9 @@ private static void objectToJson(StringBuilder builder, Object obj) {
builder.append(obj);
} else if (obj.getClass().isAssignableFrom(float.class) || obj instanceof Float) {
builder.append(Float.isNaN((float) obj) ? 0 : obj);
} else if (obj instanceof HippyArray) {
} else if (obj instanceof HippyArray || obj instanceof ArrayList) {
builder.append("[");
HippyArray array = (HippyArray) obj;
HippyArray array = (obj instanceof HippyArray) ? (HippyArray) obj : new HippyArray((ArrayList) obj);
int length = array.size();
for (int i = 0; i < length; i++) {
objectToJson(builder, array.getObject(i));
Expand All @@ -239,9 +245,9 @@ private static void objectToJson(StringBuilder builder, Object obj) {
}
}
builder.append("]");
} else if (obj instanceof HippyMap) {
} else if (obj instanceof HippyMap || obj instanceof HashMap) {
builder.append("{");
HippyMap map = (HippyMap) obj;
HippyMap map = (obj instanceof HippyMap) ? (HippyMap) obj : new HippyMap((HashMap) obj);;
Set<String> keys = map.keySet();
boolean hasComma = false;
for (String key : keys) {
Expand Down Expand Up @@ -285,11 +291,11 @@ public static Object parseArgument(Type paramCls, HippyArray array, int index) {
return (float) array.getDouble(index);
}

if (paramCls == HippyArray.class) {
if (paramCls == HippyArray.class || paramCls == ArrayList.class || paramCls == List.class) {
return array.getArray(index);
}

if (paramCls == HippyMap.class) {
if (paramCls == HippyMap.class || paramCls == HashMap.class || paramCls == Map.class) {
return array.getMap(index);
}

Expand Down Expand Up @@ -321,11 +327,11 @@ public static Object parseArgument(Type paramCls, HippyMap map, String key) {
return map.getBoolean(key);
}

if (paramCls == HippyArray.class) {
if (paramCls == HippyArray.class || paramCls == ArrayList.class || paramCls == List.class) {
return map.getArray(key);
}

if (paramCls == HippyMap.class) {
if (paramCls == HippyMap.class || paramCls == HashMap.class || paramCls == Map.class) {
return map.getMap(key);
}

Expand Down Expand Up @@ -541,6 +547,8 @@ private static boolean isSupportedObject(Class clazz) {
|| clazz == String.class
|| clazz == HippyArray.class
|| clazz == HippyMap.class
|| clazz == ArrayList.class
|| clazz == HashMap.class
|| clazz == Promise.class
|| clazz.getAnnotation(HippyTurboObj.class) != null) {
return true;
Expand Down

0 comments on commit d591b3b

Please sign in to comment.