Skip to content

Instantly share code, notes, and snippets.

@pichsenmeister
Last active February 16, 2017 03:04
Show Gist options
  • Save pichsenmeister/6167223 to your computer and use it in GitHub Desktop.
Save pichsenmeister/6167223 to your computer and use it in GitHub Desktop.
scala reflection: create instance
def getObjectInstance(clsName: String): ModuleMirror = {
val mirror = runtimeMirror(getClass.getClassLoader)
val module = mirror.staticModule(clsName)
mirror.reflectModule(module).instance
}
def getClassInstance(clsName: String): Any = {
val mirror = runtimeMirror(getClass.getClassLoader)
val cls = mirror.classSymbol(Class.forName(clsName))
val module = cls.companionSymbol.asModule
mirror.reflectModule(module).instance
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment