def handleMethodCall()

in scala/src/main/org/apache/spark/api/csharp/CSharpBackendHandler.scala [118:199]


  def handleMethodCall(
                        isStatic: Boolean,
                        objId: String,
                        methodName: String,
                        numArgs: Int,
                        dis: DataInputStream,
                        dos: DataOutputStream): Unit = {
    var obj: Object = null
    var args: Array[java.lang.Object] = null
    var methods: Array[java.lang.reflect.Method] = null
    try {
      val cls = if (isStatic) {
        Utils.classForName(objId)
      } else {
        JVMObjectTracker.get(objId) match {
          case None => throw new IllegalArgumentException("Object not found " + objId)
          case Some(o) =>
            obj = o
            o.getClass
        }
      }

      args = readArgs(numArgs, dis)
      methods = cls.getMethods
      val selectedMethods = methods.filter(m => m.getName == methodName)
      if (selectedMethods.length > 0) {
        methods = selectedMethods.filter { x =>
          matchMethod(numArgs, args, x.getParameterTypes)
        }
        if (methods.isEmpty) {
          logWarning(s"cannot find matching method ${cls}.$methodName. "
            + s"Candidates are:")
          selectedMethods.foreach { method =>
            logWarning(s"$methodName(${method.getParameterTypes.mkString(",")})")
          }
          throw new Exception(s"No matched method found for $cls.$methodName")
        }

        val ret = methods.head.invoke(obj, args: _*)

        // Write status bit
        writeInt(dos, 0)
        writeObject(dos, ret.asInstanceOf[AnyRef])
      } else if (methodName == "<init>") {
        // methodName should be "<init>" for constructor
        val ctor = cls.getConstructors.filter { x =>
          matchMethod(numArgs, args, x.getParameterTypes)
        }.head

        val obj = ctor.newInstance(args: _*)

        writeInt(dos, 0)
        writeObject(dos, obj.asInstanceOf[AnyRef])
      } else {
        throw new IllegalArgumentException("invalid method " + methodName + " for object " + objId)
      }
    } catch {
      case e: Exception =>
        val jvmObj = JVMObjectTracker.get(objId)
        val jvmObjName = jvmObj match {
          case Some(jObj) => jObj.getClass.getName
          case None => "NullObject"
        }
        logError(s"On object of type $jvmObjName failed", e)
        if (methods != null) {
          logError("methods:")
          methods.foreach(m => logError(m.toString))
        }
        if (args != null) {
          logError("args:")
          args.foreach(arg => {
            if (arg != null) {
              logError(s"argType: ${arg.getClass.getCanonicalName}, argValue: $arg")
            } else {
              logError("arg: NULL")
            }
          })
        }
        writeInt(dos, -1)
        writeString(dos, Utils.exceptionString(e.getCause))
    }
  }