jeudi 26 février 2015

pass a function to java dynamic proxy's invoke function using args parameter

Suppose I have below scala code using dynamic proxy (my only question is embedded):



trait IClient
{
def execute[R](func: IClient=>R) : R
}

class MyInvocationHandler extends InvocationHandler {
def invoke(proxy: Any, method: Method, args: Array[AnyRef]): AnyRef = {
// can I restore func passed by args and execute it here?
null
}
}

class factory[P: ClassTag]() {
val clientClass = implicitly[ClassTag[P]].runtimeClass

def getProxy() : P = java.lang.reflect.Proxy.newProxyInstance(
getClass.getClassLoader,
Array(clientClass),
new MyInvocationHandler()).asInstanceOf[P]
}

def main(): Int = {
// can the function (a: IClient)=>{0} be passed to invoke to execute?
(new factory[IClient]).getProxy().execute((a: IClient) => { 0 })
}


Appreciate any clue!






Aucun commentaire:

Enregistrer un commentaire