Java Dynamic Agent Summary

Source: Internet
Author: User

Dynamic agent, usually through the generation of bytecode to complete, but the timing of the generation is different, and bytecode dynamic generation technology, not only for dynamic agents, such as jar package, hot swap and other technologies are dynamic byte code application.

The library that generates bytecode, such as ASM, is basically operating in the format defined by the bytecode specification, which is relatively low-level, and the tool like Cglib is developed based on ASM. Some tools, like Javassist, have been abstracted.

It's not much different to writing Java files directly.

1. Run-time Agent

In the runtime to create a proxy class bytecode, this technology has the JDK native proxy, but also useful cglib implement Proxy

1.1 JDK Agent

Both agents generate proxy classes at run time, first using the JDK dynamic proxy, such as the XX interface agent,

xx x = (xx) proxy.newproxyinstance (ClassLoader, XX, handler);

This sentence is to create an XX proxy class, handler inside generally will be combined into an implementation of XX, so that in the call handler invoke method when you can invoke the concrete implementation of the method.

Newproxyinstance dynamically generated class is actually an implementation of the XX interface class, and this class receives handler as a construction method, so call the generated XX implementation of the method of the class can call handler method,

The code that writes a simple proxy is exactly the same, except that the proxy class is dynamically generated by the JDK at run time.

1.2 Cglib Agent

In principle, there is no difference between the cglib agent and the JDK proxy, which also generates bytecode at runtime and creates instances through ClassLoader, but Cglib solves the problem that the proxy target must be an interface.

For example, now there is an OO class that needs to be represented,

Enhancer enhancer = new Enhancer ();

Enhancer.setsuperclass (OO);

Enhancer.setcallback (handler);

Oo o = (oo) enhancer.create ();

Dynamic generation is not an implementation class of an interface, but a proxy class that inherits the class of Oo, which overrides the public method of Oo, when invoking the Oo method, by invoking the corresponding method of handler.

To implement, because it is through inheritance, rewrite the way, so like static method, protected method, Finnal class can not proxy.

1.3 through JVMTI or jvmagent.

Jvmagent is also based on JVMTI, JVMTI is the JVM runtime of some hook,aspectj one of the weaving method is based on this implementation, when the start must load a javaagent

2. Compile-time

Java classes need to be compiled into a. Class bytecode file, a dynamic proxy that modifies bytecode at compile time, but requires a special compiler, which represents ASPECTJ

2.1 AspectJ

First of all, there are their own syntax, previously compiled with Javac, now requires a special compiler tool, this MAVEN has ASPECTJ plug-ins. The compiler does not need to get a proxy class, directly the agent to do some things coded into the context of the previous code can be. This method is basically anything to proxy.

Then you need to explain that the compile time can be divided into two, one is with the Java source files are woven together, there is a kind of weaving into the existing. Class bytecode files, which is not much different in nature

Java Dynamic Agent Summary

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.