jeudi 26 février 2015

How to preserve caller informaion in java dynamic proxy

Suppose I have below pseudo scala code using dynamic proxy:



trait IClient {
def multiply(a : Int) = Int
}

class MyInvocationHandler extends InvocationHandler {
def invoke(proxy: Any, method: Method, args: Array[AnyRef]): AnyRef = {
//do nothing
}}

class factory[P]() {
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 = {
(new factory[IClient]).getProxy().multiply(1)
}


Now I want to pass below information to MyInvocationHandler.invoke: 1. multiply(1)'s caller method "main()" 2. multiply(1)'s caller class where main() resides 3. multiply function name


I don't want to add more parameters into multiply(1) such as



multiply(callerClassName, callerMethodName, calleeName, 1)


Is there a way to pass these manifest info to MyInvocationHandler.invoke while keeping original interface multiply(Int):Int?


Appreciate any clue!






Aucun commentaire:

Enregistrer un commentaire