Capture method calls in Java -
i need capture method call in java , not want use jpda or jdi; want happen in original jvm.
for instance:
public class { public void m() {} } public class main { public static void main(string[] args) { a = new a(); a.m(); } }
i not want let method execute @ time, need capture , schedule in queue. thus, aop not me in regard. thought proxying method. such as:
public class { public void m() { methodqueue.add(new methodinvocation() { public void invoke() {m_orig();} }); } private void m_orig(){} }
any ideas? in advance.
you can use technique called dynamic proxies in java. described in detail in following document: dynamic proxies
the solution problem (with little changes necessary):
public interface { void m(); } public class aimpl implements { public void m() {} } public class enqueueproxy implements java.lang.reflect.invocationhandler { private object obj; public static object newinstance(object obj) { return java.lang.reflect.proxy.newproxyinstance( obj.getclass().getclassloader(), obj.getclass().getinterfaces(), new enqueueproxy(obj)); } private enqueueproxy(object obj) { this.obj = obj; } public object invoke(object proxy, method m, object[] args) throws throwable { try { methodqueue mq = ... // queue want mq.add(new methodinvocation(obj, m, args) } catch (invocationtargetexception e) { throw e.gettargetexception(); } catch (exception e) { throw new runtimeexception("unexpected invocation exception: " + e.getmessage()); } return null; } }
then construct enqueueproxy implementation of interface , call m method:
a = (a) enqueueproxy.newinstance(new aimpl()); a.m();
Comments
Post a Comment