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

父类泛型方法,子类反序列化结果可能会用到父类方法,反序列化失败 #3227

Open
hanmist opened this issue Jun 2, 2020 · 2 comments

Comments

@hanmist
Copy link

hanmist commented Jun 2, 2020

使用版本 1.2.69版本,未发现解决公告

com.alibaba.fastjson.util.JavaBeanInfo类,如果父类存在泛型方法
public T getCode() {
return code;
}
public void setCode(T code) {
this.code = code;
}
子类存在方法
public int getCode() {
return code;
}

public void setCode(int code) {
this.code = code;
}
使用 clazz.getMethods()方法在不同环境得到的结果顺序可能不同;

f
对于父类之类方法参数类型存在继承关系,然后add方法仅判断了 if (item.fieldClass.isAssignableFrom(field.fieldClass)),所以在getMethods得到的方法排序中,如果父类方法顺序在前,没有问题;如果父类方法顺序在后且int result = item.compareTo(field);结果小于0,后续序列化会使用父类方法,如果又恰好用到强制类型转化,反序列化会失败

static boolean add(List fieldList, FieldInfo field) {
for (int i = fieldList.size() - 1; i >= 0; --i) {
FieldInfo item = fieldList.get(i);

        if (item.name.equals(field.name)) {
            if (item.getOnly && !field.getOnly) {
                continue;
            }

            if (item.fieldClass.isAssignableFrom(field.fieldClass)) {
                fieldList.set(i, field);
                return true;
            }

            int result = item.compareTo(field);

            if (result < 0) {
                fieldList.set(i, field);
                return true;
            } else {
                return false;
            }
        }
    }
    fieldList.add(field);

    return true;
}
@ZivYan
Copy link
Contributor

ZivYan commented Jun 27, 2020

  1. 子类实现泛型,重写 get 和 set 方法,生成新的 method,这个没问题,确实会存在顺序上不同的情况
  2. 在调用 method.invoke(object, value),尝试多次确实会存在内转换异常

@AdeGitHub
Copy link

我也遇到此问题,有解决方法吗?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants