Search code examples
scalareflectiontypesclassloader

Get classloader using typetag in scala 2.10


Is it possible in scala to get the classloader of a class using its typetag?

Using getClass().getClassLoader in the class that uses the typetag gets the classloader of the current class which may not be the same as the classloader for the type in the tag.

I want to create instances of a type whose only information I have is the typetag. I want to know if this is possible.


Solution

  • Mirrors have an associated classloader:

    scala> typeTag[Foo[Bar]].mirror.classLoader
    res6: ClassLoader = scala.tools.nsc.interpreter.IMain$TranslatingClassLoader@34b89ddd