I. Overview
1. Goal: Do not write your own proxy class, using proxy.newproxyinstance () dynamic generation
2. Points of knowledge used:
(1) //Compile the source code, generate class, note that the compilation environment to be replaced by JDK compiler, simple JRE no compiler, NULL pointer error
Javacompiler JC = Toolprovider.getsystemjavacompiler ();
(2)//File Steward
Standardjavafilemanager filemgr = Jc.getstandardfilemanager (null, NULL, NULL);
(3)//compilation unit
Iterable units = filemgr.getjavafileobjects (file);
(4)//compilation task
Compilationtask t = jc.gettask (NULL, filemgr, NULL, NULL, NULL, units);
(5)//Compile
T.call ();
(6) //Load the class into memory
url[] urls = new url[] {new URL ("file:/" +system.getproperty ("User.dir") + "/src/proxy/tanktimeproxy.class")};
URLClassLoader UC = new URLClassLoader (URLs);
Class C = Uc.loadclass ("Proxy. Tanktimeproxy ");
(7)//Generate instance
return C.newinstance (); C.newinstance () will call the parameterless Construtor if the class does not have an constructor with no arguments
Constructor ctr = c.getconstructor (Movable.class);
Return Ctr.newinstance (New Tank ());
Second, the Code
1.movable.java
2.tank.java
3.proxy.java
4.client.java
1.movable.java
1 Package proxy; 2 3 Public Interface Movable {4 Public void move (); 5 }
2.tank.java
package proxy; import Java.util.Random; public class Tank implements movable {@Override public void move () {System.out.println ( "Tank moving ..." try {Thread.Sleep ( new Random (). Nextint (2000 catch (Interruptedexception e) {e. Printstacktrace (); } }}
3.proxy.java
1 Packageproxy;2 3 ImportJava.io.File;4 ImportJava.io.FileWriter;5 ImportJava.lang.reflect.Constructor;6 ImportJava.net.URL;7 ImportJava.net.URLClassLoader;8 9 ImportJavax.tools.JavaCompiler;Ten ImportJavax.tools.JavaCompiler.CompilationTask; One ImportJavax.tools.StandardJavaFileManager; A ImportJavax.tools.ToolProvider; - - Public classProxy { the - Public StaticObject newproxyinstance ()throwsException { -String RT = "\n\r"; - + //the source of dynamic proxy files -String str = +"Package proxy;" + RT + A at"Public class Tanktimeproxy implements movable {" +rt+ - -"Private movable m;" + RT + - -"Public tanktimeproxy (movable m) {" + RT + -"THIS.M = m;" + RT + in"}" + RT + - to"@Override" + RT + +"public void Move () {" + RT + -"System.out.println (\" Time Proxy start...........\ ");" + RT + the"Long start = System.currenttimemillis ();" + RT + *"M.move ();" + RT + $"Long end = System.currenttimemillis ();" + RT +Panax Notoginseng"System.out.println (\" spend time: \ "+ (End-start));" + RT + -"System.out.println (\" Time Proxy end...........\ ");" + RT + the"}" + RT + + A"}" ; the + //write the source code in the Java file. -File File =NewFile (System.getproperty ("User.dir") + "/src/proxy/tanktimeproxy.java"); $FileWriter FW =NewFileWriter (file); $ fw.write (str); - Fw.flush (); - fw.close (); the - //Compile the source code, generate class, note that the compilation environment to be replaced by the JDK compiler, simple JRE no compiler, will be null pointer errorWuyiJavacompiler JC =Toolprovider.getsystemjavacompiler (); the - //file Steward WuStandardjavafilemanager filemgr = Jc.getstandardfilemanager (NULL,NULL,NULL); - About //Compilation Unit $Iterable units =filemgr.getjavafileobjects (file); - - //Compiling Tasks -Compilationtask t = jc.gettask (NULL, Filemgr,NULL,NULL,NULL, units); A + //compiling the T.call (); - filemgr.close (); $ the //load the class into memory theurl[] URLs =NewUrl[] {NewURL ("file:/" +system.getproperty ("User.dir") + "/src/proxy/tanktimeproxy.class")}; theURLClassLoader UC =NewURLClassLoader (URLs); theClass C = Uc.loadclass ("Proxy. Tanktimeproxy "); - in //Build Instance the //return c.newinstance ();//c.newinstance () will call the parameterless Construtor if the class does not have an constructor with no arguments theConstructor ctr = c.getconstructor (movable.class); About returnCtr.newinstance (NewTank ()); the } the}
4.client.java
1 Packageproxy;2 3 Importjava.io.IOException;4 5 Importorg.junit.Test;6 7 Public classClient {8 9 @TestTen Public voidTestproxy ()throwsexception{ One AMovable m =(movable) proxy.newproxyinstance (); - M.move (); - the } -}
Iii. Results of operation
Java Dynamic Agent Call Proxy.newproxyinstance ()