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] 父类Kotlin子类Java,序列化出现java.lang.ArrayIndexOutOfBoundsException: length=0; index=0 #1266

Closed
magic-zagan opened this issue Mar 22, 2023 · 3 comments
Labels
bug Something isn't working fixed
Milestone

Comments

@magic-zagan
Copy link

magic-zagan commented Mar 22, 2023

问题描述

父类为Kotlin定义的基类,Java语言的子类继承于这个Kotlin语言定义的父类,在序列化时出现ArrayIndexOutOfBoundsException错误

环境信息

请填写以下信息:

  • OS信息:Win10 16G
  • JDK信息: JDK1.8
  • 版本信息:Fastjson2 2.0.26

重现步骤

父类为Kotlin语言定义为:

@NoArg
public open class ProIdRpcReq(
    @field:ApiModelProperty(value = "ID", example = "1", required = true)
    var id: Long
) : ProRpcReq()

子类为Java语言定义为:

@Data
@NoArgsConstructor
@AllArgsConstructor
public class VerifyImageCaptchaRpcReq extends ProIdRpcReq {

    @Length(min = 4, max = 6)
    @Schema(description = "验证码内容", required = true)
    private String captcha;

}

测试代码为:

        val req = VerifyImageCaptchaRpcReq()
        req.id = 1L
        req.captcha = "TEST"
        Logger.d(JSON.toJSONString(req))

期待的正确结果

期望能够正确进行序列化

相关日志输出

java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
        at com.alibaba.fastjson2.writer.ObjectWriterBaseModule$WriterAnnotationProcessor.getFieldInfo(ObjectWriterBaseModule.java:840)
        at com.alibaba.fastjson2.writer.ObjectWriterBaseModule$WriterAnnotationProcessor.getFieldInfo(ObjectWriterBaseModule.java:791)
        at com.alibaba.fastjson2.writer.ObjectWriterProvider.getFieldInfo(ObjectWriterProvider.java:208)
        at com.alibaba.fastjson2.writer.ObjectWriterCreator.lambda$createObjectWriter$2$com-alibaba-fastjson2-writer-ObjectWriterCreator(ObjectWriterCreator.java:387)
        at com.alibaba.fastjson2.writer.ObjectWriterCreator$$ExternalSyntheticLambda2.accept(Unknown Source:17)
        at com.alibaba.fastjson2.util.BeanUtils.getters(BeanUtils.java:1010)
        at com.alibaba.fastjson2.writer.ObjectWriterCreator.createObjectWriter(ObjectWriterCreator.java:383)
        at com.alibaba.fastjson2.writer.ObjectWriterProvider.getObjectWriter(ObjectWriterProvider.java:333)
        at com.alibaba.fastjson2.JSON.toJSONString(JSON.java:2311)
        at cloud.fastdev.and.module.ar.account.ui.fragment.LoginFragment.initData(LoginFragment.kt:233)

附加信息

Kotlin使用@noarg开启了为Java生成空构造方法的特性(但猜测应该没有关系)

根据DEBUG,具体崩溃的点位于:
com.alibaba.fastjson2.writer.ObjectWriterBaseModule的840行的 beanInfo.creatorConstructor.getParameterAnnotations()[i];

if (beanInfo.kotlin && beanInfo.creatorConstructor != null && beanInfo.createParameterNames != null) {
                String fieldName = BeanUtils.getterName(method, null);
                for (int i = 0; i < beanInfo.createParameterNames.length; i++) {
                    if (fieldName.equals(beanInfo.createParameterNames[i])) {
                        // ↓ 这里
                        Annotation[] parameterAnnotations = beanInfo.creatorConstructor.getParameterAnnotations()[i];
                        processAnnotations(fieldInfo, parameterAnnotations);
                        break;
                    }
                }
            }
@magic-zagan magic-zagan added the bug Something isn't working label Mar 22, 2023
@wenshao wenshao added this to the 2.0.27 milestone Mar 23, 2023
@wenshao
Copy link
Member

wenshao commented Mar 23, 2023

https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.27-SNAPSHOT/
问题已修复,请帮忙用2.0.27-SNAPSHOT版本验证

@wenshao wenshao added the fixed label Mar 23, 2023
wenshao added a commit that referenced this issue Mar 23, 2023
@magic-zagan
Copy link
Author

已在测试demo和项目中测试通过,COOL~!

@wenshao
Copy link
Member

wenshao commented Apr 8, 2023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixed
Projects
None yet
Development

No branches or pull requests

2 participants