Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] java.util.Arrays$ArrayList反序列化失败 #341

Closed
dyj2012 opened this issue May 25, 2022 · 0 comments
Closed

[BUG] java.util.Arrays$ArrayList反序列化失败 #341

dyj2012 opened this issue May 25, 2022 · 0 comments
Labels
bug Something isn't working
Milestone

Comments

@dyj2012
Copy link

dyj2012 commented May 25, 2022

问题描述

java.util.Arrays$ArrayList反序列化失败。

重现步骤

如何操作可以重现该问题:

  1. 使用 Arrays.asList("cacheObject", "cacheObject2", "cacheObject3") 存字符串,用JSONB反解析失败
  2. 输入 Arrays.asList("cacheObject", "cacheObject2", "cacheObject3") 数据
  3. 出现 Caused by: java.lang.InstantiationException: java.util.Arrays$ArrayList at java.lang.Class.newInstance(Class.java:427) at com.alibaba.fastjson2.reader.FieldReaderListStrMethod.readFieldValue(FieldReaderListStrMethod.java:54) ... 49 more Caused by: java.lang.NoSuchMethodException: java.util.Arrays$ArrayList.<init>() at java.lang.Class.getConstructor0(Class.java:3082) at java.lang.Class.newInstance(Class.java:412) ... 50 more 错误

期待的正确结果

对您期望发生的结果进行清晰简洁的描述。

相关日志输出

请复制并粘贴任何相关的日志输出。
org.springframework.data.redis.serializer.SerializationException: Could not deserialize: create instance error class java.util.Arrays$ArrayList; nested exception is com.alibaba.fastjson2.JSONException: create instance error class java.util.Arrays$ArrayList

附加信息

修改了FieldReaderListStrMethod源码解决了这个问题,增加了listType == ObjectReaderImplList.CLASS_ARRAYS_LIST

package com.alibaba.fastjson2.reader;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONException;
import com.alibaba.fastjson2.JSONReader;
import com.alibaba.fastjson2.util.Fnv;
import com.alibaba.fastjson2.util.TypeUtils;

import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.*;

final class FieldReaderListStrMethod<T>
        extends FieldReaderObjectMethod<T>
        implements FieldReaderList<T, Object> {
    final long fieldClassHash;

    FieldReaderListStrMethod(String fieldName, Type fieldType, Class fieldClass, int ordinal, long features, String format, Method method) {
        super(fieldName, fieldType, fieldClass, ordinal, features, format, method);
        this.fieldClassHash = Fnv.hashCode64(TypeUtils.getTypeName(fieldClass));
    }

    @Override
    public Type getItemType() {
        return String.class;
    }

    @Override
    public void readFieldValue(JSONReader jsonReader, T object) {
        List value;
        if (jsonReader.isJSONB()) {
            Class listType = fieldClass;
            ObjectReader objectReader = jsonReader.checkAutoType(fieldClass, fieldClassHash, features);
            if (objectReader != null) {
                listType = objectReader.getObjectClass();
            }

            int itemCnt = jsonReader.startArray();
            if (itemCnt == -1) {
                value = null;
            } else if (listType == Collection.class
                    || listType == AbstractCollection.class
                    || listType == List.class
                    || listType == AbstractList.class
                    || listType == ArrayList.class
                    || listType == ObjectReaderImplList.CLASS_ARRAYS_LIST) {
                value = new ArrayList(itemCnt);
            } else if (listType == LinkedList.class) {
                value = new LinkedList();
            } else if (listType == JSONArray.class) {
                value = new JSONArray(itemCnt);
            } else {
                try {
                    value = (List) listType.newInstance();
                } catch (InstantiationException | IllegalAccessException e) {
                    throw new JSONException("create instance error " + listType, e);
                }
            }

            for (int i = 0; i < itemCnt; ++i) {
                value.add(jsonReader.readString());
            }
        } else if (jsonReader.current() == '[') {
            List list = createList();
            jsonReader.next();
            for (; ; ) {
                if (jsonReader.nextIfMatch(']')) {
                    break;
                }

                list.add(jsonReader.readString());

                if (jsonReader.nextIfMatch(',')) {
                    continue;
                }
            }
            accept(object, list);

            jsonReader.nextIfMatch(',');

            value = list;
        } else {
            throw new JSONException("json format error : " + jsonReader.current());
        }
        try {
            method.invoke(object, value);
        } catch (Exception e) {
            throw new JSONException("set " + fieldName + " error", e);
        }
    }
}
@dyj2012 dyj2012 added the bug Something isn't working label May 25, 2022
@dyj2012 dyj2012 changed the title [BUG] [BUG] java.util.Arrays$ArrayList反序列化失败 May 25, 2022
@wenshao wenshao added this to the 2.0.5 milestone May 25, 2022
@dyj2012 dyj2012 closed this as completed May 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants