Public Interface Animalinterface { publicvoid Cry ();}
Public class Implements Animalinterface { publicvoid Cry () { // TODO auto-generated method Stub System.out.println ("Crying");} }
Public classMyproxyImplementsInvocationhandler {PrivateObject proxied; Privatemyproxy (Object proxied) { This. proxied =proxied; } Public StaticObject GetProxy (object proxied) {returnproxy.newproxyinstance (Proxied.getclass (). getClassLoader (), Proxied.getclass (). Getinterfaces (), Newmyproxy (proxied)); } Publicobject Invoke (Object proxy, Method method, object[] args)throwsThrowable {//TODO auto-generated Method StubObject ret; System.out.println ("Before Method Invoke"); RET=Method.invoke (proxied, args); System.out.println ("After Method Invoke"); returnret; }}
Public class Test { publicstaticvoid main (string[] args) { = ( Animalinterface) myproxy . GetProxy (new Animalimpl ()); Animal.cry (); }}
JDK Dynamic Agent