scala.reflect.runtime.universe.runtimeMirror Scala Examples
The following examples show how to use scala.reflect.runtime.universe.runtimeMirror.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
Example 1
Source File: ReflectionHelper.scala From spark-atlas-connector with Apache License 2.0 | 5 votes |
package com.hortonworks.spark.atlas.utils import scala.util.control.NonFatal object ReflectionHelper extends Logging { import scala.reflect.runtime.universe.{TermName, runtimeMirror, typeOf, TypeTag} private val currentMirror = runtimeMirror(getClass.getClassLoader) def reflectField[T, OUT](obj: Any, fieldName: String)(implicit ttag: TypeTag[T]): Option[OUT] = { val relMirror = currentMirror.reflect(obj) try { val method = typeOf[T].decl(TermName(fieldName)).asTerm.accessed.asTerm Some(relMirror.reflectField(method).get.asInstanceOf[OUT]) } catch { case NonFatal(_) => logWarn(s"Failed to reflect field $fieldName from $obj. " + s"Maybe missing to apply necessary patch?") None } } def reflectFieldWithContextClassloaderLoosenType(obj: Any, fieldName: String): Option[Any] = { val typeMirror = runtimeMirror(Thread.currentThread().getContextClassLoader) val instanceMirror = typeMirror.reflect(obj) val members = instanceMirror.symbol.typeSignature.members val field = members.find(_.name.decodedName.toString == fieldName) field match { case Some(f) => try { Some(instanceMirror.reflectField(f.asTerm).get) } catch { case NonFatal(e) => logWarn(s"Failed to reflect field $fieldName from $obj. " + s"Maybe missing to apply necessary patch? $e") None } case None => logWarn(s"Failed to reflect field $fieldName from $obj. " + s"Maybe missing to apply necessary patch?") None } } def reflectFieldWithContextClassloader[OUT](obj: Any, fieldName: String): Option[OUT] = { reflectFieldWithContextClassloaderLoosenType(obj, fieldName).map(_.asInstanceOf[OUT]) } def reflectMethodWithContextClassloaderLoosenType( obj: Any, methodName: String, params: Any*): Option[Any] = { val typeMirror = runtimeMirror(Thread.currentThread().getContextClassLoader) val instanceMirror = typeMirror.reflect(obj) val members = instanceMirror.symbol.typeSignature.members val method = members.find(_.name.decodedName.toString == methodName) method match { case Some(f) => try { Some(instanceMirror.reflectMethod(f.asMethod).apply(params)) } catch { case NonFatal(_) => logWarn(s"Failed to call method $methodName from $obj via reflection. " + s"Maybe missing to apply necessary patch?") None } case None => logWarn(s"Failed to call method $methodName from $obj via reflection. " + s"Maybe missing to apply necessary patch?") None } } def reflectMethodWithContextClassloader[OUT]( obj: Any, fieldName: String, params: Any*): Option[OUT] = { reflectMethodWithContextClassloaderLoosenType(obj, fieldName, params: _*) .map(_.asInstanceOf[OUT]) } def classForName(className: String): Class[_] = { Class.forName(className, true, getContextOrClassClassLoader) } private def getContextOrClassClassLoader: ClassLoader = Option(Thread.currentThread().getContextClassLoader).getOrElse(getClass.getClassLoader) }
Example 2
Source File: ConfigCreator.scala From spark-bench with Apache License 2.0 | 5 votes |
package com.ibm.sparktc.sparkbench.workload import com.ibm.sparktc.sparkbench.datageneration.GraphDataGen import com.ibm.sparktc.sparkbench.datageneration.mlgenerator.{KMeansDataGen, LinearRegressionDataGen} import com.ibm.sparktc.sparkbench.utils.GeneralFunctions.getOrThrowT import com.ibm.sparktc.sparkbench.utils.SparkBenchException import com.ibm.sparktc.sparkbench.workload.exercise._ import com.ibm.sparktc.sparkbench.workload.ml.{KMeansWorkload, LogisticRegressionWorkload} import com.ibm.sparktc.sparkbench.workload.sql.SQLWorkload object ConfigCreator { private val workloads: Map[String, WorkloadDefaults] = Set( PartitionAndSleepWorkload, KMeansWorkload, LogisticRegressionWorkload, CacheTest, SQLWorkload, Sleep, SparkPi, KMeansDataGen, LinearRegressionDataGen, GraphDataGen ).map(wk => wk.name -> wk).toMap private def loadCustom(name: String): WorkloadDefaults = { import scala.reflect.runtime.universe.runtimeMirror val mirror = runtimeMirror(scala.reflect.runtime.universe.getClass.getClassLoader) val module = mirror.staticModule(name) mirror.reflectModule(module).instance.asInstanceOf[WorkloadDefaults] } def mapToConf(m: Map[String, Any]): Workload = { val name = getOrThrowT[String](m, "name").toLowerCase val (displayName, conf) = if (name == "custom") { val className = getOrThrowT[String](m, "class") (className, Some(loadCustom(className))) } else (name, workloads.get(name)) conf match { case Some(wk) => wk.apply(m) case _ => throw SparkBenchException(s"Could not find workload $displayName") } } }
Example 3
Source File: JsonLifter.scala From diffy with GNU Affero General Public License v3.0 | 5 votes |
package ai.diffy.lifter import com.fasterxml.jackson.core.{JsonGenerator, JsonToken} import com.fasterxml.jackson.databind.annotation.JsonSerialize import com.fasterxml.jackson.databind.ser.std.StdSerializer import com.fasterxml.jackson.databind.{JsonNode, ObjectMapper, SerializerProvider} import com.fasterxml.jackson.module.scala.DefaultScalaModule import com.fasterxml.jackson.module.scala.experimental.ScalaObjectMapper import com.twitter.util.Try import scala.collection.JavaConversions._ import scala.language.postfixOps import scala.reflect.runtime.universe.runtimeMirror import scala.tools.reflect.ToolBox import scala.util.control.NoStackTrace object JsonLifter { @JsonSerialize(using = classOf[JsonNullSerializer]) object JsonNull object JsonParseError extends Exception with NoStackTrace val toolbox = runtimeMirror(getClass.getClassLoader).mkToolBox() val Mapper = new ObjectMapper with ScalaObjectMapper Mapper.registerModule(DefaultScalaModule) def apply(obj: Any): JsonNode = Mapper.valueToTree(obj) def lift(node: JsonNode): Any = node.asToken match { case JsonToken.START_ARRAY => node.elements.toSeq.map { element => lift(element) } case JsonToken.START_OBJECT => { val fields = node.fieldNames.toSet if (fields.exists{ field => Try(toolbox.parse(s"object ${field}123")).isThrow}) { node.fields map {field => (field.getKey -> lift(field.getValue))} toMap } else { FieldMap( node.fields map {field => (field.getKey -> lift(field.getValue))} toMap ) } } case JsonToken.VALUE_FALSE => false case JsonToken.VALUE_NULL => JsonNull case JsonToken.VALUE_NUMBER_FLOAT => node.asDouble case JsonToken.VALUE_NUMBER_INT => node.asLong case JsonToken.VALUE_TRUE => true case JsonToken.VALUE_STRING => node.textValue case _ => throw JsonParseError } def decode(json: String): JsonNode = Mapper.readTree(json) def encode(item: Any): String = Mapper.writer.writeValueAsString(item) } class JsonNullSerializer(clazz: Class[Any]) extends StdSerializer[Any](clazz) { def this() { this(null) } override def serialize(t: Any, jsonGenerator: JsonGenerator, serializerProvider: SerializerProvider): Unit = { jsonGenerator.writeNull() } }