Two types of deep simulation Java dynamic Agent implementation mechanism

Source: Internet
Author: User

This time, what we're going to achieveFor any interface, any method of the specific agent

Here is not the same only proxy class, to implement all methods to proxy, then the focus isget all the methods of the interface

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;
Import Javax.tools.JavaCompiler.CompilationTask;

This class implements a specific proxy for any interface, any method
public class ProxyG2 {
Generating a new dynamic proxy class
public static Object Newproxyinstance (class intf) throws exception{//interface as a parameter, so that the proxy implements the class of any interface, and not only implements the moveable interface
String methodsstring = ""; Get all Method strings
String RT = "\ r \ n";

method[] methods = Intf.getmethods ();
For (Method m:methods) {
methodsstring + = "@Override" + rt+
"public void" +m.getname () + "() {" + "" +rt+
"Long start = System.currenttimemillis ();" +rt+
"System.out.println (\" Start time is \ "+start);" +rt+
"T." +m.getname () + "();" +rt+
"Long end = System.currenttimemillis ();" +rt+
"System.out.println (\" End time is \ "+end);" +rt+
"System.out.println (\" Time is \ "+ (End-start));" +rt+
"}" +RT;
        }


Dynamically compile the string to generate the proxy class
Implementation mode has the following hit: jdk6.0 complier API;  CGLib; Asm

String src =
"Public class Tanktimeproxy implements" +intf.getname () + "{" +rt+//directly with intf, is called the ToString method, before the string is added interface
"Moveable t;" +rt+

"Public Tanktimeproxy (" +intf.getname () + "T) {" +rt+
"THIS.T = t;" +rt+
"}" +rt+

methodsstring+
"}";

To compile
String fileName = "G:/src/tanktimeproxy.java";//place at specified location
File F = new file (fileName);
FileWriter FW = new FileWriter (f);
System.out.println (FileName);
Fw.write (SRC); Write content

Fw.flush ();
Fw.close ();

To compile
Get the compiler first
compiler is the Java compiler Javac
Get Compiler Object
Javacompiler compiler = Toolprovider.getsystemjavacompiler ();
System.out.println (Compiler.getclass (). GetName ());//Get class name
parameter meaning (compile diagnostics, Locale,charset)
Manage Dynamic Build D files
Standardjavafilemanager FileManager = Compiler.getstandardfilemanager (null,null,null);//Default value
Get multiple Java files based on parameters return Java file object
Iterable units = filemanager.getjavafileobjects (fileName);

The Compile Task object
Javacompiler.compilationtask task = Compiler.gettask (null,filemanager,null,null,null,units);
Task.call ();//Call
Filemanager.close ();
System.out.println ("***************************************");

The above process obtained the Java File source code, compiled the Java file generated by the class file *******
Load into memory, generate new objects
Class.load (0 is a class file that loads a path path
URLClassLoader is to load the class file from the hard drive into

Introducing local files via URLs
url[] urls = new Url[]{new URL ("file:/" + "g:/src/")}; Access local files specify where class files are stored
To specify a path to find a class file
URLClassLoader URLClassLoader = new URLClassLoader (URLs);

Class C = Urlclassloader.loadclass ("Tanktimeproxy");

System.out.println (c);

Perform
C.newinstance (); is to call the empty constructor method

Get the construction method
Depending on the Java virtual machine, each construction method is also equivalent to an object
Constructor Constructor = C.getconstructor (intf);

Generating new objects
Object m = constructor.newinstance (New Tank ()); New Tank () is the proxy object for the constructor method's argument

M.move ();

return m;
}
}

Two types of deep simulation Java dynamic Agent implementation mechanism

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.