Aoba-TelegramBot icon indicating copy to clipboard operation
Aoba-TelegramBot copied to clipboard

Kotlin Inline 函数泛型获取的对应 Array 类有误导致 Gson 反序列化出错

Open fython opened this issue 5 years ago • 0 comments

错误来源

https://github.com/fython/Aoba-TelegramBot/commit/c6f4d20140126a8cf08e98f4d0c7029c600786aa#diff-538cbd8f51f4037bd1e971ef3261215bR133

说明

Kotlin 中 Inline 函数允许为泛型标注 reified 修饰符避免类型擦除以获得该泛型的 Java Class,在这个例子当中我们试图获取该泛型对应的 Array 类(即 E -> Array<E>),实际运行获取到的 Array 类是 Object[],出现了预期外的类型擦除,导致最后 Gson 反序列化时使用了缺省的 LinkedHashMap 将 JsonArray 中的元素构建成了 Map 类型而非我们预期的 E 类型。

依赖版本

  • Kotlin: 1.3.50
  • Gson: 2.8.5 (问题与 Gson 无关)

修复方法

未知,感觉应该是 Kotlin 的 bug。

缓解方案

暂时停用 KVDatabase 中 listValuemutableListValue 的 Inline 版本,改用手动传入 Array 类。

fython avatar Oct 16 '19 03:10 fython