3

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, and name5 have always the same info value
  • name6 and name5 have always different info 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:

Community
  • 1
  • 1
Stefan Endrullis
  • 4,150
  • 2
  • 32
  • 45
  • 1
    Not an answer directly, but you are aware that Scala 2.10 is just around the corner and has the new reflection api? You might try a milestone for that. – Brian Smith Aug 13 '12 at 12:57
  • @BrianSmith: Thanks for the hint. I would actually like to step over to Scala 2.10 immediately. Unfortunately most of my dependencies are not yet available for 2.10. – Stefan Endrullis Aug 13 '12 at 13:08

3 Answers3

9

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:

scala> import reflect.runtime.universe._
import reflect.runtime.universe._

scala> typeOf[A].members.view.filter{_.isValue}.filter{!_.isMethod}.toList
res0: List[reflect.runtime.universe.Symbol] = List(variable name4, value name3, value name2, value name1)

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.

Community
  • 1
  • 1
Nikita Volkov
  • 42,792
  • 11
  • 94
  • 169
3
import reflect.runtime.universe._
typeOf[A].members map (m => m -> m.typeSignature) collect { 
  case (m, nm: NullaryMethodType) => m 
}
Daniel C. Sobral
  • 295,120
  • 86
  • 501
  • 681
0

I finally found a solution. Instead of using the primitive info value one can use the more informative infoType which seems to return a NullaryMethodType for all methods not having parenthesis:

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 && m.infoType.isInstanceOf[NullaryMethodType])
  }
}

In the end we get the desired result without name6 and without the hidden <init> method:

name1: MethodSymbol(name1, owner=0, flags=28400200, info=22 ,None)
name3: MethodSymbol(name3, owner=0, flags=8400200, info=45 ,None)
name4: MethodSymbol(name4, owner=0, flags=8000200, info=45 ,None)
name5: MethodSymbol(name5, owner=0, flags=200, info=45 ,None)
Stefan Endrullis
  • 4,150
  • 2
  • 32
  • 45