|
30 | 30 | import java.io.StringWriter;
|
31 | 31 | import java.lang.annotation.Retention;
|
32 | 32 | import java.lang.annotation.RetentionPolicy;
|
| 33 | +import java.lang.reflect.Field; |
| 34 | +import java.lang.reflect.Modifier; |
33 | 35 | import java.lang.reflect.ParameterizedType;
|
34 | 36 | import java.lang.reflect.Type;
|
35 | 37 | import java.net.UnknownHostException;
|
36 | 38 | import java.text.ParseException;
|
37 | 39 | import java.text.SimpleDateFormat;
|
| 40 | +import java.util.ArrayList; |
38 | 41 | import java.util.Arrays;
|
| 42 | +import java.util.Collection; |
39 | 43 | import java.util.Date;
|
40 | 44 | import java.util.Formatter;
|
41 | 45 | import java.util.Iterator;
|
| 46 | +import java.util.List; |
42 | 47 | import java.util.Locale;
|
| 48 | +import java.util.Map; |
43 | 49 | import java.util.Set;
|
44 | 50 | import java.util.concurrent.ExecutorService;
|
45 | 51 | import java.util.concurrent.Executors;
|
@@ -341,11 +347,15 @@ private static String formatObject(Object object) {
|
341 | 347 | return iFormatter.format(object);
|
342 | 348 | }
|
343 | 349 | }
|
| 350 | + if (object instanceof String || object instanceof JSONObject || object instanceof JSONArray) |
| 351 | + return object.toString(); |
344 | 352 | if (object.getClass().isArray()) return LogFormatter.array2String(object);
|
345 | 353 | if (object instanceof Throwable) return LogFormatter.throwable2String((Throwable) object);
|
346 | 354 | if (object instanceof Bundle) return LogFormatter.bundle2String((Bundle) object);
|
347 | 355 | if (object instanceof Intent) return LogFormatter.intent2String((Intent) object);
|
348 |
| - return object.toString(); |
| 356 | + if (object instanceof Map) return LogFormatter.map2String((Map) object); |
| 357 | + if (object instanceof Collection) return LogFormatter.collection2String((Collection) object); |
| 358 | + return LogFormatter.object2String(object); |
349 | 359 | }
|
350 | 360 |
|
351 | 361 | private static void print2Console(final int type,
|
@@ -982,6 +992,53 @@ static String intent2String(Intent intent) {
|
982 | 992 | return sb.toString();
|
983 | 993 | }
|
984 | 994 |
|
| 995 | + static String map2String(Map map) { |
| 996 | + JSONObject jsonObject = new JSONObject(map); |
| 997 | + return jsonObject.toString(); |
| 998 | + } |
| 999 | + |
| 1000 | + static String collection2String(Collection collection) { |
| 1001 | + JSONObject jsonObject = new JSONObject(); |
| 1002 | + try { |
| 1003 | + jsonObject.put("size", collection.size()); |
| 1004 | + jsonObject.put("data", collection); |
| 1005 | + return jsonObject.toString(); |
| 1006 | + } catch (JSONException ignore) { |
| 1007 | + return collection.toString(); |
| 1008 | + } |
| 1009 | + } |
| 1010 | + |
| 1011 | + static String object2String(Object object) { |
| 1012 | + Class<?> clazz = object.getClass(); |
| 1013 | + List<Field> tmp = Arrays.asList(clazz.getDeclaredFields()); |
| 1014 | + ArrayList<Field> list = new ArrayList<>(tmp); |
| 1015 | + while (clazz != Object.class) { |
| 1016 | + clazz = clazz.getSuperclass(); |
| 1017 | + if (clazz == null) { |
| 1018 | + break; |
| 1019 | + } |
| 1020 | + Field[] fields = clazz.getDeclaredFields(); |
| 1021 | + for (Field field : fields) { |
| 1022 | + int modifier = field.getModifiers(); |
| 1023 | + if (Modifier.isPublic(modifier)) { |
| 1024 | + list.add(field); |
| 1025 | + } |
| 1026 | + } |
| 1027 | + } |
| 1028 | + Field[] a = new Field[list.size()]; |
| 1029 | + Field[] fields = list.toArray(a); |
| 1030 | + JSONObject jsonObject = new JSONObject(); |
| 1031 | + for (Field field : fields) { |
| 1032 | + String fieldName = field.getName(); |
| 1033 | + try { |
| 1034 | + Object obj = field.get(object); |
| 1035 | + jsonObject.put(fieldName, obj); |
| 1036 | + } catch (Exception ignore) { |
| 1037 | + } |
| 1038 | + } |
| 1039 | + return jsonObject.toString(); |
| 1040 | + } |
| 1041 | + |
985 | 1042 | @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
|
986 | 1043 | private static void clipData2String(ClipData clipData, StringBuilder sb) {
|
987 | 1044 | ClipData.Item item = clipData.getItemAt(0);
|
|
0 commit comments