Skip to content

Instantly share code, notes, and snippets.

@ottomata
Created January 22, 2018 20:48
Show Gist options
  • Save ottomata/dc3733b3e92c173c5484d130d05904ea to your computer and use it in GitHub Desktop.
Save ottomata/dc3733b3e92c173c5484d130d05904ea to your computer and use it in GitHub Desktop.
/**
* Given a fully qualified String package.ObjectName and String method name, this
* Function will return a scala.reflect.runtime.universe.MethodMirror that can be
* used for calling the method on the object. Note that MethodMirror is not a direct
* reference to the actual method, and as such does not have compile time type
* and signature checking. You must ensure that you call the method with exactly the
* same arguments and types that the method expects, or you will get a runtime exception.
*
* @param objectName Fully qualified name for an object, e.g. org.wikimedia.analytics.refinery.core.DeduplicateEventLogging
* @param methodName Name of method in the object. Default "apply".
* @return
*/
def getObjectMethod(objectName: String, methodName: String = "apply", typeSignature: Option[universe.Type] = None): universe.MethodMirror = {
val mirror = universe.runtimeMirror(getClass.getClassLoader)
val module = mirror.staticModule(objectName)
val method = module.typeSignature.member(universe.newTermName(methodName)).asMethod
mirror.reflect(mirror.reflectModule(module).instance).reflectMethod(method)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment