Obtain Companion object given a Class Manifest

502 views
Skip to first unread message

Raul Raja Martinez

unread,
Apr 3, 2013, 11:37:25 AM4/3/13
to scala...@googlegroups.com
I have a case where I need the companion object given a class manifest.
Looking through the docs and old stackoverflow questions all solutions and references I've found are previous to 2.10.

Is there a recommended way to do it with the new reflection API's ?

Thanks!

Flavio W. Brasil

unread,
Apr 3, 2013, 2:59:31 PM4/3/13
to Raul Raja Martinez, scala...@googlegroups.com
Example using SMirror:

scala> class Test; object Test { def a = "a" }
defined class Test
defined module Test

scala> implicit val mirror = scala.reflect.runtime.currentMirror
mirror: reflect.runtime.universe.Mirror = JavaMirror with scala.tool…

scala> import net.fwbrasil.smirror._
import net.fwbrasil.smirror._

scala> val clazz = sClassOf[Test]
clazz: net.fwbrasil.smirror.SClass[Test] = Test

scala> clazz.companionObjectOption
res0: Option[net.fwbrasil.smirror.SInstance[Test]] = Some(Test (bound to Test$@2a51b326))

scala> clazz.companionSClassOption
res1: Option[net.fwbrasil.smirror.SCompanionClass[Test]] = Some(Test)

scala> clazz.companionSClassOption.get.methods
res3: List[net.fwbrasil.smirror.SMethod[Test]] = List(a(): java.lang.String)

-- 
Flávio W. Brasil
{persistence as it should be}

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Raul Raja Martinez

unread,
Apr 3, 2013, 4:59:58 PM4/3/13
to Flavio W. Brasil, scala...@googlegroups.com
Thanks Flávio, I will look into it and use SMirror if I can't find a simpler way. I was hoping for something more lightweight than a library like a straight up class.companion or something built in given companion objects are so common and supported by scala as first class citizens

Eugene Burmako

unread,
Apr 3, 2013, 5:04:41 PM4/3/13
to Raul Raja Martinez, Flavio W. Brasil, scala...@googlegroups.com
00:04 ~/Projects/210x (2.10.x)$ scala
Welcome to Scala version 2.10.2-20130329-164357-00fcd46ed0 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_43).
Type in expressions to have them evaluated.
Type :help for more information.

scala> class C; object C
defined class C
defined module C

scala> import scala.reflect.runtime.{currentMirror => cm}
import scala.reflect.runtime.{currentMirror=>cm}

scala> cm.classSymbol(classOf[C]).companionSymbol
res0: reflect.runtime.universe.Symbol = object C

Eugene Burmako

unread,
Apr 3, 2013, 5:06:19 PM4/3/13
to Raul Raja Martinez, Flavio W. Brasil, scala...@googlegroups.com
This will get the instance of the module symbol we got in the previous email:

scala> cm.reflectModule(res0.asModule).instance
res2: Any = C$@5dcde3f3

Raul Raja Martinez

unread,
Apr 3, 2013, 5:51:14 PM4/3/13
to Eugene Burmako, Flavio W. Brasil, scala...@googlegroups.com
Eugene, thanks so much, that worked!
Reply all
Reply to author
Forward
0 new messages