What is ASM? ASM is the abbreviation for assembly, the title of the assembly, and for Java, ASM is the bytecode-level programming.
The ASM here refers to the ObjectWeb ASM, an open source project for the. Class code generator.
ASM is a Java bytecode generation architecture that dynamically generates stub classes or other proxy classes in binary format.
Or, the class is dynamically modified before the class is loaded into memory by the Java Virtual machine.
Now a lot of popular frameworks have been used in ASM. So from AOP it came back to this.
1. What is ObjectWeb ASM
ObjectWeb ASM is a lightweight Java bytecode processing framework. It can dynamically generate stub classes or other proxy classes in binary format, or dynamically modify classes before classes are loaded into memory by a Java virtual machine.
ASM provides features similar to the Bcel and SERP, which are only 22K in size and are quite small compared to the 350K Bcel and 150K serp, and have higher execution efficiency.
It is 7 times times bcel, 11 times times more than SERP.
In my opinion, ObjectWeb ASM has several very attractive features as follows
* Compact and efficient
* Source code implementation is very concise and elegant, is simply the Gof "design mode" very good annotations
* Byte code-level control to enable more efficient byte-code control
ObjectWeb ASM has 2 groups of interfaces:
* Event-driven interface, XML-like sax interface, visitor mode, callback when access to a section of class definition, implementation of the above tree interface efficient, less memory consumption
* Tree-based interface, an XML-like DOM interface that parses a class definition into a tree
Here we will use the ObjectWeb ASM event-driven interface
2. Goal
We will enhance the existing bytecode, collect information about the entry method and exit method, which mainly solves the bytecode enhancement portion of Method monitor,
Do not do a more in-depth study of the collected data processing, for demonstration purposes, we define the following methods of collecting access information,
In practice, we may collect more data in a better format, use asynchronous processing to improve performance, use batch processing to improve processing power, use friendly UI display information, and more.
This section is not discussed here
1. Package blackstar.methodmonitor.instrutment.monitor;
2. public class Monitorutil
3. {
4. Public final static String class_name = MonitorUtil.class.getName ()
5.. replaceall ("\ \", "/");
6. Public final static String Entry_method = "Entrymethod";
7. Public final static String Exit_method = "Exitmethod";
8. Public final static String METHOD = "(ljava/lang/string; ljava/lang/string;) V ";
9.
public static void Entrymethod (String className, String methodName)
11. {
System.out.println ("entry:" + ClassName + "." + MethodName);
13.}
14.
public static void Exitmethod (String className, String methodName)
16. {
System.out.println ("Exit:" + ClassName + "." + MethodName);
18.}
19.}
3. Start with the byte code
In fact, for the monitored code, we need to implement the following functions, the red part of the code is that we need to plug in the dynamic phase in the middle of the bytecode
Public xxx method (...)
{
Try
{
Methodentry (...)
Methodcode
}
Finally
{
Methodexit (...)
}
}
The problem seems simple, but actually it's not that easy, because in the JVM bytecode design, bytecode does not directly support the finally statement, but instead uses try...catch to simulate it, let's take a look at an example
Java code
1. Package blackstar.methodmonitor.instrutment.test;
2.
3. public class Test
4. {
5. public void SayHello () throws Exception
6. {
7. Try
8. {
9. SYSTEM.OUT.PRINTLN ("Hi");
Ten.} catch (Exception e)
11. {
SYSTEM.OUT.PRINTLN ("exception");
return;
.} finally
15. {
System.out.println ("finally");
17.}
18.}
19.}
Let's see how the bytecode handles the finally statement.
First look at the exception table, the exception is directly supported at the JVM level, the following exception table means, in the execution of the 0-8 statement, if there is an exception java.lang.Exception thrown, then enter the 11th statement,
In the execution of the 0-20 statement, any exception thrown, all into the 29 statement. In fact, this is how the JVM implements the finally statement:
* The byte code in the finally statement is incremented before any return statement
* Defines a statement that captures all exceptions, increases the bytecode in the finally statement, and if there is no return statement in Finally, throws the exception again (the processing method ends by throwing an exception)
Exceptions:
[0-8]: 11-java.lang.exception
[0-20]: 29
Let's see how the bytecode is made.
0 getstatic Java.lang.System.out
3 LDC "HI" (java.lang.String)
5 invokevirtual println
8 Goto 40
SYSTEM.OUT.PRINTLN ("HI"); execution returns after execution (goto 40)
Astore_1
Getstatic Java.lang.System.out
LDC "Exception" (java.lang.String)
Invokevirtual println
SYSTEM.OUT.PRINTLN ("exception");
Getstatic Java.lang.System.out
LDC "finally" (java.lang.String)
Invokevirtual println
The return statement is inserted before the finally partial byte code
System.out.println ("finally");
Return
Astore_2
Getstatic Java.lang.System.out
LDC "finally" (java.lang.String)
Invokevirtual println
Aload_2
Athrow
Executes this finally statement when an exception is thrown in the execution 0-29 statement
The two bytecode of the astore_2 (the stack top value-that is, the address of the exception-to the 2nd local variable) and aload_2 (the value of the 2nd local variable into the stack) is actually unnecessary,
It is important to note, however, that if these 2 pieces of code are removed, consider increasing the stack of operations (Max Stack) to accommodate this exception address
System.out.println ("finally");
Getstatic Java.lang.System.out
LDC "finally" (java.lang.String)
Invokevirtual println
The return statement is inserted before the finally partial byte code
System.out.println ("finally");
Return
In fact, what we need to do is
* Insert a method into the code when the method enters (note that for constructors that do not allow this kind of processing, the constructor must call the parent class's constructor in the first step.)
* Insert method exit code before each return operation (including return, Ireturn, Freturn, etc.)
* Define a handle to catch all exceptions, and in the process, insert the method exit code (that is, the method terminates execution in a way that throws the exception)
4. Realize
Let's see how we can use ObjectWeb ASM to implement the functionality described above
1) ObjectWeb ASM Bytecode Modification
1. Classreader cr = new Classreader (ByteArray); Using byte code to construct a reader
2. Classwriter cw = new Classwriter (CR, 0);//writer will be modified based on existing bytecode
3. Monitorclassvisitor CA = new Monitorclassvisitor (CW);//Modify Processing callback class
4. Cr.accept (CA, 0);
An explanation of the ASM framework in Java