Last active
February 16, 2017 03:04
-
-
Save pichsenmeister/6167223 to your computer and use it in GitHub Desktop.
scala reflection: create instance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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