I would like to programmatically determine all properties of a Scala class at runtime. For instance, for the following Scala class I would like to determine that the methods name1, name3, name4, and name5 are getters for properties of A:
class A(val name1: String, private val name2: String) {
val name3 = ""
var name4 = ""
def name5 = ""
def name6() = ""
}
Most of the work can be done with Java’s reflection API. Unfortunately I was not able to detect the difference between name5 and name6(). Therefore I started a next trial using the ScalaSigParser, but ScalaSig’s flags for name5 and name6() are unfortunately also the same. Here is my code:
def gettersOf(clazz: Class[_]) = {
for (ssig <- ScalaSigParser.parse(clazz))
yield {
ssig.symbols.toList.collect{
case m: MethodSymbol => m
}.filter(m => (m.symbolInfo.flags & 0xFFFFF) == 0x200)
}
}
gettersOf(classOf[A]).get.foreach{m =>
println(m.name + ": " + m)
}
As you can see in the following output both methods differ only in the info value:
name1: MethodSymbol(name1, owner=0, flags=28400200, info=22 ,None)
<init>: MethodSymbol(<init>, owner=0, flags=200, info=38 ,None)
name3: MethodSymbol(name3, owner=0, flags=8400200, info=45 ,None)
name4: MethodSymbol(name4, owner=0, flags=8000200, info=45 ,None)
name4_$eq: MethodSymbol(name4_$eq, owner=0, flags=8000200, info=54 ,None)
name5: MethodSymbol(name5, owner=0, flags=200, info=45 ,None)
name6: MethodSymbol(name6, owner=0, flags=200, info=66 ,None)
However, info does not seem to return static constants. If you add another method to the class the info value of name6 will change while there seems to be some stability:
name3,name4, andname5have always the sameinfovaluename6andname5have always differentinfovalues
Does someone know the meaning of info and how one can use it to surely determine which kind of method it is?
Related questions:
Your best bet is to use a reflection API of the coming Scala 2.10.
Here’s how to get the properties of a type in 2.10.0-M6:
Unfortunately there’s not much reading material on it yet. There are some answered questions on SO though. For instance, this question might be of interest to you. Also please check out this blogpost by Daniel Sobral.