Detect internal objects in scala object

I want to get a list of internal objects of a scala object. Code example:

object Outer { val v = "-" def d = "-" object O1 object O2 } object Main { def main(args: Array[String]) { Outer.getClass.getDeclaredMethods.map(_.getName) foreach println // prints d and v // Outer.getClass.get ... Objects??? } } 

I can find v and d, but how to find O1 and O2?

+6
source share
2 answers

With the new reflection library in Scala 2.10 (starting with Milestone 4), you can get internal objects:

 scala> import scala.reflect.runtime.{universe => u} import scala.reflect.runtime.{universe=>u} scala> val outer = u.typeOf[Outer.type] outer: reflect.runtime.universe.Type = Outer.type scala> val objects = outer.declarations.filter(_.isModule).toList objects: List[reflect.runtime.universe.Symbol] = List(object O1, object O2) 
+6
source

The O1 and O2 objects are nested classes and are not part of the Outer object.

  println(Outer.O1.getClass.getName) //Outer$O1$ println(Outer.getClass.getName) //Outer$ println(Outer.O2.getClass.getName) //Outer$O2$ 
+1
source

Source: https://habr.com/ru/post/917417/


All Articles