scala reflection: getDeclaringTrait?

When I research a new library, I sometimes find it hard to locate the implementation of a method.

In Java, Metho#getDeclaringClass provides the class that declared a given method. So by iterating over Class#getMethods, I can find for each method, the class that declared it.

In Scala, traits are converted to Java interfaces and a class that extends a trait will implement the methods of the trait by forwarding them to a companion class defining these methods statically. This means, that Method#getDeclaringClass will return the class, not the trait:

scala> trait A { def foo = {println("hi")}}
defined trait A

scala> class B extends A
defined class B

scala> 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, Class)] where each tuple is a method and the trait/class it was declared in?

This question and answers originated from www.stackoverflow.com
Question by (12/2/2009 3:48:02 PM)

Answer

In Scala 2.8 you can use the ScalaSigParser to parse the scala specific byte code information.

This will be more stable than the byte code serialization format of scala traits, classes and methods.

import tools.scalap.scalax.rules.scalasig._
import scala.runtime._

val scalaSig = ScalaSigParser.parse(classOf[RichDouble]).get
val richDoubleSymbol = scalaSig.topLevelClasses.head
val methods = richDoubleSymbol.children filter ( _ match {
    case m : MethodSymbol => true
    case _ => false
})

methods foreach println
richDoubleSymbol.isTrait
ScalaSigParser.parse(classOf[Ordered[Any]]).get.topLevelClasses.head.isTrait

Prints:

scala> methods foreach println
MethodSymbol(x, owner=0, flags=20080004, info=23 ,None)
MethodSymbol(<init>, owner=0, flags=200, info=33 ,None)
[...]
MethodSymbol(isPosInfinity, owner=0, flags=200, info=117 ,None)
MethodSymbol(isNegInfinity, owner=0, flags=200, info=117 ,None)

scala> richDoubleSymbol.isTrait
res1: Boolean = false

scala> ScalaSigParser.parse(classOf[Ordered[Any]]).get.topLevelClasses.head.isTrait
res2: Boolean = true

I suppose following this road you can build a reflection API for Scala.

Answer by

Find More Answers
Related Topics  scala  reflection
Related Questions
  • scala reflection

    I want to create a hashmap to store parameters names and their values. The parameters however are with different types. I could use HashMap[String, Any], but I wouldn't know which types they are lat…
  • Reflection API for Scala

    Does anyone know the status of a fully-featured reflection API for Scala? I know that you can use Java's reflection API to do simple things but this does not work well with Scala's language feat…
  • Scala enumeration and reflection

    After working in Java for a long time, I started to get interested in Scala. As a learning project, I am trying to duplicate a java library that stores and retrieves state objects from the database…
  • Type from Scala reflection

    Suppose that I have: trait A class B extends A compiled into class files. Later I load those using reflection: val a = Class forName "A" val b = Class forName "B" Could anyone tell me…
  • scala reflection error java

    I'm trying to use scala.reflect to get the class attributes and to write them XML. However I'm getting a strange error def toXml(): xml.Elem = { <node>{ for(field: scala.reflect.Field &l…
  • scala reflection field class

    I'm trying to get the type of an attribute that refers to a custom class, I just get that it's of type Object My code: class Edge[N <% Node](var from : N, var to : N) { def toXml(c: Class…
  • Is there a good reflection library available for Scala?

    I'm working on a library that needs reflection, and needs Scala-specific information as opposed to what is available via the standard Java reflection API. Right now I'm using the undocumented code i…
  • Which Scala features are internally implemented using reflection?

    It is a well known fact that structural types are implemented through reflection. Are there maybe any other language constructs which use reflection?
  • What reflection capabilities can we expect from Scala 2.10?

    Scala 2.10 brings reflection other than that provided the JVM (or I guess CLR). What in particular do we have to look forward to, and how will it improve on the platform? For example, will the…
  • reflection and type checking in scala

    I have the following scala code def invokeMethods(instance: AnyRef, clazz: Class[_]) { assert(clazz.isInstance(instance) // <- is there a way to check this statically? for {method <-…