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
, andname5
have always the sameinfo
valuename6
andname5
have always differentinfo
values
Does someone know the meaning of info
and how one can use it to surely determine which kind of method it is?
Related questions: