Simulate JDK dynamic proxy (Java design mode)

Source: Internet
Author: User

Jdk6 provides javacompiler, standardjavafilemanager,IterableCompile the. Java file.

When implementing proxy, the framework can use cglib to directly generate a binary file without generating a. Java file.

// Interface public interface moveable {void move (); void stop ();}

// Implement the moveable interface public class tank implements moveable {public void move () {system. out. println ("tank move ()... ");} public void stop () {system. out. println ("tank stop ()... ");}}

Import java. Lang. Reflect. method; // create an interface besides moveable invocationhandlerpublic interface invocationhandler {void invoke (Object o, method M );}

Import Java. lang. reflect. method; // implements the invocationhandler interface, customizes the content to be added, and creates the handlerpublic class loghandler implements invocationhandler {object target; Public loghandler (object target) implements this.tar get = target ;} public void invoke (Object o, method M) {system. out. println ("loghandler start... "); try {M. invoke (target);} catch (exception e) {e. printstacktrace ();} system. out. println ("loghandler end... ");}}

Import Java. io. file; import Java. io. filewriter; import Java. lang. reflect. constructor; import Java. lang. reflect. method; import java.net. URL; import java.net. urlclassloader; import javax. tools. javacompiler; import javax. tools. standardjavafilemanager; import javax. tools. toolprovider; // simulate JDK's proxy class proxypublic class proxy {// simulate JDK's method for creating proxy classes. There is also a classloader parameter in JDK. Here, the interface and the logic handler to be added are passed in, the result returns a proxy object. Public static object newproxyinstance (class INF, invocationhandler h) throws exception {string methodstr = ""; string RT = "\ r \ n"; method [] Methods = inf. getmethods (); For (method M: Methods) {methodstr + = "@ override" + RT + "Public void" + M. getname () + "() {" + RT + "try {" + RT + "method MD =" + INF. getname () + ". class. getmethod (\ "" + M. getname () + "\"); "+ RT +" H. invoke (this, MD); "+ RT +"} Ca Tch (exception e) {e. printstacktrace () ;}" + RT + "}" ;}// generate the proxy object class string src = "package proxy;" + RT + "Import Java. lang. reflect. method; "+ RT +" public class $ proxy1 implements "+ INF. getname () + "{" + RT + "proxy. invocationhandler h; "+ RT +" Public $ proxy1 (invocationhandler h) {"+ RT +" this. H = H; "+ RT +"} "+ RT + methodstr +"} "; string filename =" D:/share/test/Proxy/$ proxy1.java "; file F = new F Ile (filename); filewriter fw = new filewriter (f); FW. write (SRC); // generate. java file FW. flush (); FW. close (); // compile the class javacompiler compiler = toolprovider. getsystemjavacompiler (); standardjavafilemanager filemanager = compiler. getstandardfilemanager (null, null, null); iterable Units = filemanager. getjavafileobjects (filename); compiler. gettask (null, filemanager, null, units ). call (); // compile and generate. class file. Filemanager. close (); Url [] url = new URL [] {new URL ("file:/D:/share/test /")}; urlclassloader loader = new urlclassloader (URL); constructor c = loader. loadclass ("proxy. $ proxy1 "). getconstructor (invocationhandler. class); // The constructor has the return C. newinstance (h); // load. class file, generating objects .}}
Public class client {public static void main (string [] ARGs) throws exception {invocationhandler H = new loghandler (new tank (); // tell the proxy class what logic I need to add. Moveable M = (moveable) proxy. newproxyinstance (moveable. Class, H); // return the proxy object. M. Move (); // actually, this is the method that calls the logic-added proxy class. M. Stop ();}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.