title: Accessing Scala nested objects through reflection
body: <p>I've got some nested objects like this:</p>
  
  <pre><code>object Routes {
    object admin {
      def home = "/admin/home/"
      def module(m:Module) = "/admin/module/"+m.code
    }
  }
  </code></pre>
  
  <p>Accessible via <code>Routes.admin.module(mymod)</code> and so on.</p>
  
  <p>Given the Routes object, I want to be able to access the admin object by its name "admin", in order to expose it to the templating engine I'm using (Freemarker). This is the solution based on the standard Java Reflection API I've come up with that seems to work:</p>
  
  <pre><code>val obj = Routes
  val key = "admin"
  Class.forName(obj.getClass.getName+key+"$").getField("MODULE$").get(null)
  </code></pre>
  
  <p>This finds for the static field <code>Routes$admin$.MODULE$</code> which is where the singleton object lives. Is there a neater way that doesn't require embedding knowledge of the naming conventions for Scala bytecode?</p>