1

According to the docs here, declarations should be a subset of members for the things declared in the class, not inherited. Then why do various classes report no declarations?

scala> import scala.reflect.runtime.universe._
scala> typeTag[java.lang.System].tpe.declarations
res5: reflect.runtime.universe.MemberScope = SynchronizedOps()
Rob N
  • 15,024
  • 17
  • 92
  • 165
  • 1
    The class `java.lang.System` does not have any members in Scala. The static members are on the companion object. This post tells you how to get it: http://stackoverflow.com/questions/11020746/get-companion-object-instance-with-new-scala-reflection-api – gzm0 Sep 10 '13 at 21:04
  • That code throws an exception if I try it with `java.lang.System`, because it looks for `System$`. It's not obvious how to get a list of static methods that way. – Rob N Sep 11 '13 at 00:10
  • 4
    scala> typeOf[System].typeSymbol.companionSymbol.typeSignature.declarations res5: reflect.runtime.universe.MemberScope = SynchronizedOps(value in, value out, value err, ...) – Eugene Burmako Sep 11 '13 at 07:09

1 Answers1

3

The reason is that both members and declarations only take into account object members. However, all functions declared in java.lang.System are static.

This makes sense because from the scala point of view there are no static members. The equivalent of a static member is a method/value defined in a module (using object instead of class). So scala-reflection will act as if static members of a Java-class are defined in a module --- more specifically in the companion object of the java-class. (Note that in contrast to scala defined companion objects these "java-companion-objects" do not exist on a VM level).

I'm no expert in scala reflection, so I can't tell you how you would find the static members :-(

subsub
  • 1,857
  • 10
  • 21