Thursday, 15 May 2014

scala reflection: getDeclaringTrait? -


When I search for a new library, I sometimes find it hard to find out the implementation of a method.

In Java, MathO provides the #getDeclaringClass class which declares a defined method. Then by walking again on class #getMethods, I can meet for each method, the category that declared it.

In Scala, the properties are converted into the Java interface, and a class which enhances the attribute means to define these methods by defining the methods of properties forwarded to those companions, which will definitely define these methods. That method will return #getDeclaringClass class, not attribute:

  scala> Attribute A {def foo = {println ("hi"}}} defined attribute A scale & gt; Class B is a defined class B scale & gt; ClassOf [B] .getMethods.find (_ getName () == "foo".) Get.getDeclaringClass res3:. Java.lang.Class [_] = class B  

What is the best way to work around this? Meaning, given a class, how can I get a list [(method, orbit)] where each tuple has been declared in a method and specialty / class?

itemprop = "text">

after scalar 2.8, you

ScalaSigParser to parse specific byte code information .

It can be used more stable.

  import tools.scalap.scalax.rules.scalasig._ import scala.runtime._ Val scalaSig = ScalaSigParser.parse (classOf Scala symptoms byte of classes and methods address [RichDouble the serialization format ]) received Val richDoubleSymbol = scalaSig.topLevelClasses.head Val methods = richDoubleSymbol.children filter (_ match. {case m: MethodSymbol = & gt; true case _ = & gt; false}) methods foreach println richDoubleSymbol.isTrait ScalaSigParser.parse (ClassOf [ordered [any]] get.topLevelClasses.head.isTrait  

Prints :.

  Scala & gt; Methods foreach println MethodSymbol (X, owner = 0, flag = 20,080,004, information = 23, No) MethodSymbol (& lt; init & gt;, owner = 0, no flags = 200, information = 33, a) [.. .] MethodSymbol (isPosInfinity, owner = 0, flag = 200, information = 117, no) method Sinbol (isNegInfinity, owner = 0, flag = 200, information = 117, no) Scale & gt; RichDoubleSymbol.isTrait res1: Boolean = false mark & ​​gt; ScalaSigParser.parse (classOf [Ordrd [a]]. Get.topLevelClasses.head.isTrait res2: The Boolean = true  

I think that following this road you reflection Scala API.


No comments:

Post a Comment