找不到类 java.lang.Object 的 ScalaSig [英] Can't find ScalaSig for class java.lang.Object

查看:64
本文介绍了找不到类 java.lang.Object 的 ScalaSig的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这些主题中没有找到答案:首先第二

Didn't find the answer in these topics: first, second

有下一个问题.我有一个名为 Foo 的案例类:

Have the next problem. I have a case class named Foo:

case class Foo(a: Int, b: List[Int])

当我需要制作此类的 AST 时,我调用 Extraction.decompose() 并获得 foo 实例的 AST 表示.

When I need to make an AST of this class I invoke Extraction.decompose(<instance of Foo>) and get an AST represenation of foo instance.

但是如果我将字段 b 设为私有

But if I make field b as private

case class Foo(a: Int, private val b: List[Int])

我收到 org.json4s.package$MappingException: Can't find ScalaSig for class java.lang.Object 异常.

这仅适用于作为集合的私有字段.

This is only true for private fields that are collections.

如果私有字段是简单对象,它就不会出现在 AST 中.为什么会发生这种异常?

If private field is simple object it simply doesn't get appeared in AST. Why this exception does happen?

编辑如果我有一个 case 类使用 val 或 lazy val 字段扩展某些特征,则会出现同样的异常:

EDIT The same exception arises if I have a case class extending some trait with val or lazy val fields:

trait Bar {
   val list: List[Int] = List(1,2,3)
}

case class Example(field: Double) extends Bar

推荐答案

我认为这与我遇到的问题类似.

I think that this is the similar to the issue that I'm running into.

首先,ScalaSigreader#42

if (current == null)

应该

if (current == classOf[java.lang.Object])

然后,您将收到一条更有用的错误消息:无法从 Foo 中找到字段 b".但这本身并不能解决问题.

Then, you'll get a more helpful error message: "Can't find field b from Foo". But this by itself doesn't fix the issue.

我还没有研究解决私有字段问题的方法.但是,我确实解决了界面问题.对于这个问题,ScalaSigReader#45 需要修改.

I haven't looked into a fix for the private field issue. However, I do have a fix for the issue with the interface. For that problem, ScalaSigReader#45 needs to be modified.

当前,如果未找到该字段,则搜索超类:

Currently if the field isn't found then the superclass is searched:

findField(findClass(current), name).getOrElse(read(current.getSuperclass))

还必须搜索接口:

findField(current, name)
    .orElse(current.getInterfaces.flatMap(findField(_, name)).headOption)
    .getOrElse(read(current.getSuperclass))

private def findField(clazz: Class[_], name: String): Option[MethodSymbol] =
    findField(findClass(clazz), name)

另见:

问题 #403

拉#436

这篇关于找不到类 java.lang.Object 的 ScalaSig的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆