如何获取伴生对象的 ScalaSig?
scalap 库具有从类文件中解析 Scala 特定信息的功能。对于常规课程,我可以获得如下信息:
scala> ScalaSigParser.parse(classOf[BasicCase])
res46: Option[scala.tools.scalap.scalax.rules.scalasig.ScalaSig] =
Some(ScalaSig version 5.0
0: ClassSymbol(BasicCase, owner=<empty>, flags=40000040, info=5 ,None)
1: BasicCase
2: <empty>
3: <empty>
4: NoSymbol
5: ClassInfoType(ClassSymbol(BasicCase, owner=<empty>, flags=40000040, info=5 ,None),List(TypeRefType(ThisType(java.lang),java.lang.Object,List()), TypeRefType(ThisType(scala),scala.ScalaObject,List()), TypeRefType(ThisType(scala),scala.Product,List())))
6: TypeRefType(ThisType(java.lang),java.lang.Object,List())
7: ThisType(java.lang)
8: java.lang
9: lang
10: java
11: java
12: java.lang.Object
13: Object
14: TypeRefType(ThisType(scala),scala.ScalaObject,List())
15: ThisType(scala)
16: scala
17: scala
18: scala.ScalaObject
19: ScalaObject
20: TypeRefType(ThisType(scala),scala.Pr...
scala>
...并从那里获得有关内部结构的各种好东西。但对于伴随对象:
scala> ScalaSigParser.parse(classOf[BasicCase$])
res47: Option[scala.tools.scalap.scalax.rules.scalasig.ScalaSig] = None
......我什么也没得到。然而,Scalap将在伴随对象类上工作:
scalap -cp . BasicCase$
package BasicCase$;
final class BasicCase$ extends scala.runtime.AbstractFunction3 with java.io.Serializable with scala.ScalaObject {
def this(): scala.Unit;
def apply(scala.Any, scala.Any, scala.Any): scala.Any;
def readResolve(): scala.Any;
def apply(scala.Int, java.lang.String, scala.Option): BasicCase;
def unapply(BasicCase): scala.Option;
}
object BasicCase$ {
final val MODULE$: BasicCase$;
}
所以一定有一种方法,我只是还没有找到它。有人有什么想法吗?
编辑:我找到了它,但仍然需要更多:
scala> bcSig.topLevelObjects
res59: List[scala.tools.scalap.scalax.rules.scalasig.ObjectSymbol] = List(ObjectSymbol(BasicCase, owner=<empty>, flags=200402, info=124 ))
scala> bcSig.topLevelObjects.head
res60: scala.tools.scalap.scalax.rules.scalasig.ObjectSymbol = ObjectSymbol(BasicCase, owner=<empty>, flags=200402, info=124 )
不幸的是,子方法似乎没有给我方法:
scala> res60.children
res70: Seq[scala.tools.scalap.scalax.rules.scalasig.Symbol] = List()
与常规类不同:
scala> bcSym.children
res71: Seq[scala.tools.scalap.scalax.rules.scalasig.Symbol] = List(MethodSymbol(id, owner=0, flags=29400200, info=25 ,None), MethodSymbol(id , owner=0, flags=21080004, info=26 ,None), MethodSymbol(name, owner=0, flags=29400200, info=33 ,None), MethodSymbol(name , owner=0, flags=21080004, info=34 ,None), MethodSymbol(data, owner=0, flags=29400200, info=45 ,None), MethodSymbol(data , owner=0, flags=21080004, info=46 ,None), MethodSymbol(<init>, owner=0, flags=200, info=53 ,None), MethodSymbol(copy, owner=0, flags=200200, info=61 ,None), MethodSymbol(copy$default$1, owner=0, flags=2200200, info=67 ,None), MethodSymbol(copy$default$2, owner=0, flags=2200200, info=80 ,None), MethodSymbol(copy$default$3, owner=0, flags=2200200, info=84 ,None), MethodSymbol(hashCode, owner=0, flags=40000220, i...
The scalap library has functionality for parsing Scala-specific information out of classfiles. For a regular class, I can obtain information as such:
scala> ScalaSigParser.parse(classOf[BasicCase])
res46: Option[scala.tools.scalap.scalax.rules.scalasig.ScalaSig] =
Some(ScalaSig version 5.0
0: ClassSymbol(BasicCase, owner=<empty>, flags=40000040, info=5 ,None)
1: BasicCase
2: <empty>
3: <empty>
4: NoSymbol
5: ClassInfoType(ClassSymbol(BasicCase, owner=<empty>, flags=40000040, info=5 ,None),List(TypeRefType(ThisType(java.lang),java.lang.Object,List()), TypeRefType(ThisType(scala),scala.ScalaObject,List()), TypeRefType(ThisType(scala),scala.Product,List())))
6: TypeRefType(ThisType(java.lang),java.lang.Object,List())
7: ThisType(java.lang)
8: java.lang
9: lang
10: java
11: java
12: java.lang.Object
13: Object
14: TypeRefType(ThisType(scala),scala.ScalaObject,List())
15: ThisType(scala)
16: scala
17: scala
18: scala.ScalaObject
19: ScalaObject
20: TypeRefType(ThisType(scala),scala.Pr...
scala>
...and from there get all sorts of goodies about the internals. But for the companion object:
scala> ScalaSigParser.parse(classOf[BasicCase$])
res47: Option[scala.tools.scalap.scalax.rules.scalasig.ScalaSig] = None
...I get nothing. However, Scalap will work on the companion object class:
scalap -cp . BasicCase$
package BasicCase$;
final class BasicCase$ extends scala.runtime.AbstractFunction3 with java.io.Serializable with scala.ScalaObject {
def this(): scala.Unit;
def apply(scala.Any, scala.Any, scala.Any): scala.Any;
def readResolve(): scala.Any;
def apply(scala.Int, java.lang.String, scala.Option): BasicCase;
def unapply(BasicCase): scala.Option;
}
object BasicCase$ {
final val MODULE$: BasicCase$;
}
So there must be a way, I just haven't been able to find it. Anyone have any ideas?
EDIT: I found it, but still need more:
scala> bcSig.topLevelObjects
res59: List[scala.tools.scalap.scalax.rules.scalasig.ObjectSymbol] = List(ObjectSymbol(BasicCase, owner=<empty>, flags=200402, info=124 ))
scala> bcSig.topLevelObjects.head
res60: scala.tools.scalap.scalax.rules.scalasig.ObjectSymbol = ObjectSymbol(BasicCase, owner=<empty>, flags=200402, info=124 )
Unfortunately the children method doesn't seem to give me methods:
scala> res60.children
res70: Seq[scala.tools.scalap.scalax.rules.scalasig.Symbol] = List()
Unlike with the regular class:
scala> bcSym.children
res71: Seq[scala.tools.scalap.scalax.rules.scalasig.Symbol] = List(MethodSymbol(id, owner=0, flags=29400200, info=25 ,None), MethodSymbol(id , owner=0, flags=21080004, info=26 ,None), MethodSymbol(name, owner=0, flags=29400200, info=33 ,None), MethodSymbol(name , owner=0, flags=21080004, info=34 ,None), MethodSymbol(data, owner=0, flags=29400200, info=45 ,None), MethodSymbol(data , owner=0, flags=21080004, info=46 ,None), MethodSymbol(<init>, owner=0, flags=200, info=53 ,None), MethodSymbol(copy, owner=0, flags=200200, info=61 ,None), MethodSymbol(copy$default$1, owner=0, flags=2200200, info=67 ,None), MethodSymbol(copy$default$2, owner=0, flags=2200200, info=80 ,None), MethodSymbol(copy$default$3, owner=0, flags=2200200, info=84 ,None), MethodSymbol(hashCode, owner=0, flags=40000220, i...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为可以使用
infoType
获取同伴的方法,我使用List
作为示例,res17 相当于您的 res60 :注意
lefts
和rights
方法,List 类中不存在这些方法。I think could get the companion's methods using the
infoType
, I usedList
as my sample and res17 is equivalent to your res60 :Note the
lefts
andrights
methods, which don't exist in List class.