Hi all:
  I'm a newbie to spark and scala and now I am working on 
SPARK-5682<https://issues.apache.org/jira/browse/SPARK-5682>(Add encrypted 
shuffle in spark). I met a problem:is there any api in spark like 
getInstance(className:String):AnyRef ? I saw org.apache.spark.sql.hive
.thriftserver.ReflectionUtils.scala, but not provide getInstance function in it.

Now i only can implement this function by following code:
object ReflectionUtils1 {
  import scala.reflect.runtime.universe
  abstract case class CryptoCodec() {

  }

  class JceAesCtrCryptoCodec extends CryptoCodec {

  }

  class OpensslAesCtrCryptoCodec extends CryptoCodec {

  }

  def main(args: Array[String]) = {
    val className:String =  "JceAesCtrCryptoCodec"
    val obj = getInstance(className)
    val codec:CryptoCodec = obj.asInstanceOf[CryptoCodec]
    println(codec)
  }

  def getInstance(className:String):AnyRef={
    val m = universe.runtimeMirror(getClass.getClassLoader)
    var c: CryptoCodec = null
    if (className.equals("JceAesCtrCryptoCodec")) {
      val classCryptoCodec = universe.typeOf[JceAesCtrCryptoCodec]
        .typeSymbol.asClass
      val cm = m.reflectClass(classCryptoCodec)
      val ctor = universe.typeOf[JceAesCtrCryptoCodec].declaration(
        universe.nme.CONSTRUCTOR).asMethod
      val ctorm = cm.reflectConstructor(ctor)
      val p = ctorm()
      c = p.asInstanceOf[CryptoCodec]
    } else {
      val classCryptoCodec = universe.typeOf[OpensslAesCtrCryptoCodec]
        .typeSymbol.asClass
      val cm = m.reflectClass(classCryptoCodec)
      val ctor = universe.typeOf[OpensslAesCtrCryptoCodec].declaration(
        universe.nme.CONSTRUCTOR).asMethod
      val ctorm = cm.reflectConstructor(ctor)
      val p = ctorm()
      c = p.asInstanceOf[CryptoCodec]
    }
   c
  }
}


in my getInstance(className:String), i judge classname with 
"JceAesCtrCryptoCodec" and
"OpensslAesCtrCryptoCodec" and if the name equals "JceAesCtrCryptoCodec", it 
creates the instance by scala.reflect.runtime.universe api. The code can be 
better like following way but I do not know how to write it:
   def getInstance1(className:String):AnyRef={
       val m = universe.runtimeMirror(getClass.getClassLoader)
       var classLoader: ClassLoader = Thread.currentThread.getContextClassLoader
       val aClass:Class[_] =   Class.forName(className, true, classLoader)
       val aType: scala.reflect.api.TypeTags.TypeTag =  // how to write this 
line?
       val classCryptoCodec = universe.typeOf[aType]
         .typeSymbol.asClass
       val cm = m.reflectClass(classCryptoCodec)
       val ctor = universe.typeOf[aType].declaration(
         universe.nme.CONSTRUCTOR).asMethod
       val ctorm = cm.reflectConstructor(ctor)
       val p = ctorm()
       p
     }

Guidance/advice appreciated!



Best regards
Kelly Zhang/Zhang,Liyun

Reply via email to