About Java byte code ASM framework of learning

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

  One, what is ASM

  ASM is a Java bytecode manipulation framework, it can be used to dynamically generate the class or enhance both the function of class. ASM can directly generate the binary class  file, also can be in class; before being loaded into the Java virtual machine dynamic change behavior. Java class is stored in the strict definition format.Class files, these files have enough metadata to all elements of the class: class name, method, attribute and the Java byte code (instructions). ASM read from the information file, can change the behavior, analysis of information, and even generate new classes according to user requirements.

  Using the ASM framework need to import the ASM jar package, the download link: asm-3.2.jar.

  Two, how to use the ASM

  The core classes in the ASM framework has the following:

  ①  ClassReader: this class is used to parse the compiled class byte code file.

  ②  ClassWriter: this class is used to reconstruct the compiled class, for example to change names, attributes and methods, can even generate new class bytecode file.

  ③  ClassAdapter: the class also implements the ClassVisitor interface, it will approach to its calling entrusted to another ClassVisitor object.

  Example 1 the ASM generated bytecode

 1 package com.asm3;
 2 
 3 import java.io.File;
 4 import java.io.FileNotFoundException;
 5 import java.io.FileOutputStream;
 6 import java.io.IOException;
 7 
 8 import org.objectweb.asm.ClassWriter;
 9 import org.objectweb.asm.Opcodes;
10 
11 /**
12  * The ASM generated bytecode
13  * @author Administrator
14  *
15  */
16 public class GeneratorClass {
17 
18     public static void main(String[] args) throws IOException {
19         //Generates a class ClassWriter components you need
20         ClassWriter cw = new ClassWriter(0);
21         //Determine the head information by visit method
22         cw.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC+Opcodes.ACC_ABSTRACT+Opcodes.ACC_INTERFACE,
23                 "com/asm3/Comparable", null, "java/lang/Object", new String[]{"com/asm3/Mesurable"});
24         //Define class attributes
25         cw.visitField(Opcodes.ACC_PUBLIC+Opcodes.ACC_FINAL+Opcodes.ACC_STATIC,
26                 "LESS", "I", null, new Integer(-1)).visitEnd();
27         cw.visitField(Opcodes.ACC_PUBLIC+Opcodes.ACC_FINAL+Opcodes.ACC_STATIC,
28                 "EQUAL", "I", null, new Integer(0)).visitEnd();
29         cw.visitField(Opcodes.ACC_PUBLIC+Opcodes.ACC_FINAL+Opcodes.ACC_STATIC,
30                 "GREATER", "I", null, new Integer(1)).visitEnd();
31         //Methods class definition
32         cw.visitMethod(Opcodes.ACC_PUBLIC+Opcodes.ACC_ABSTRACT, "compareTo",
33                 "(Ljava/lang/Object;)I", null, null).visitEnd();
34         cw.visitEnd(); //The CW class is complete
35         //CW will be converted into an array of bytes written to a file.
36         byte[] data = cw.toByteArray();
37         File file = new File("D://Comparable.class");
38         FileOutputStream fout = new FileOutputStream(file);
39         fout.write(data);
40         fout.close();
41     }
42 }

  Generates a class bytecode file only needs to use the ClassWriter class, the compiler for javap instructions to generate Comparable.class: javap -c Comparable.class > test.txt   , compiled the results as follows:

1 public interface com.asm3.Comparable extends com.asm3.Mesurable {
2   public static final int LESS;
3 
4   public static final int EQUAL;
5 
6   public static final int GREATER;
7 
8   public abstract int compareTo(java.lang.Object);
9 }

  Note: a compiled Java class does not contain the package and import segments, so all types used in the class document is the full path.

  Byte 2 example modify a class file

C.java

1 package com.asm5;
2 
3 public class C {
4     public void m() throws InterruptedException{
5         Thread.sleep(100); 
6     }
7 }

The contents of the C.java class is replaced by the following:

 1 package com.asm5;
 2 
 3 public class C {
 4     public static long timer;
 5     public void m() throws InterruptedException{
 6         timer -= System.currentTimeMillis();
 7         Thread.sleep(100); 
 8         timer += System.currentTimeMillis();
 9     }
10 }

  In order to understand how ASM is implemented, we first compile the two classes, and then output them on TraceClassVisitor, we can find the following different (bold)

GETSTATIC C.timer : J
INVOKESTATIC java/lang/System.currentTimilis()J
LSUB
PUTSTATIC C.timer : J
LDC 100
INVOKESTATIC java/lang/Thread.sleep(J)V
GETSTATIC C.timer : J
INVOKESTATIC java/lang/System.currentTimilis()J
LADD
PUTSTATIC C.timer : J
RETURN
MAXSTACK=4
MAXLOCALS=1

  By comparing the above instructions, we can find that must be in the M () method to increase the front four instructions, also increased four instructions in front of RETURN, at the same time, the four must be located in the xRETURN and ATHROW before, because these instructions will end method execution.

The following code:

AddTimeClassAdapter.java

 1 package com.asm5;
 2 
 3 import org.objectweb.asm.ClassAdapter;
 4 import org.objectweb.asm.ClassVisitor;
 5 import org.objectweb.asm.FieldVisitor;
 6 import org.objectweb.asm.MethodAdapter;
 7 import org.objectweb.asm.MethodVisitor;
 8 import org.objectweb.asm.Opcodes;
 9 
10 public class AddTimeClassAdapter extends ClassAdapter {
11     private String owner;
12     private boolean isInterface;
13     public AddTimeClassAdapter(ClassVisitor cv) {
14         super(cv);
15     }
16     @Override
17     public void visit(int version, int access, String name, String signature,
18             String superName, String[] interfaces) {
19         cv.visit(version, access, name, signature, superName, interfaces);
20         owner = name;
21         isInterface = (access & Opcodes.ACC_INTERFACE) != 0;
22     }
23     @Override
24     public MethodVisitor visitMethod(int access, String name, String desc,
25             String signature, String[] exceptions) {
26         MethodVisitor mv = cv.visitMethod(access, name, desc, signature, exceptions);
27         if(!name.equals("<init>") && !isInterface && mv!=null){
28             //Add the timing function method
29             mv = new AddTimeMethodAdapter(mv);
30         }
31         return mv;
32     }
33     @Override
34     public void visitEnd() {
35         //Add the field
36         if(!isInterface){
37             FieldVisitor fv = cv.visitField(Opcodes.ACC_PUBLIC+Opcodes.ACC_STATIC, "timer", "J", null, null);
38             if(fv!=null){
39                 fv.visitEnd();
40             }
41         }
42         cv.visitEnd();
43     }
44     
45     class AddTimeMethodAdapter extends MethodAdapter{
46         public AddTimeMethodAdapter(MethodVisitor mv) {
47             super(mv);
48         }
49         @Override
50         public void visitCode() {
51             mv.visitCode();
52             mv.visitFieldInsn(Opcodes.GETSTATIC, owner, "timer", "J");
53             mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/System", "currentTimeMillis", "()J");
54             mv.visitInsn(Opcodes.LSUB);
55             mv.visitFieldInsn(Opcodes.PUTSTATIC, owner, "timer", "J");
56         }
57         @Override
58         public void visitInsn(int opcode) {
59             if((opcode>=Opcodes.IRETURN && opcode<=Opcodes.RETURN) || opcode==Opcodes.ATHROW){
60                 mv.visitFieldInsn(Opcodes.GETSTATIC, owner, "timer", "J");
61                 mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/System", "currentTimeMillis", "()J");
62                 mv.visitInsn(Opcodes.LADD);
63                 mv.visitFieldInsn(Opcodes.PUTSTATIC, owner, "timer", "J");
64             }
65             mv.visitInsn(opcode);
66         }
67         @Override
68         public void visitMaxs(int maxStack, int maxLocal) {
69             mv.visitMaxs(maxStack+4, maxLocal);
70         }
71     }
72     
73 }

Generator.java

 1 package com.asm5;
 2 
 3 import java.io.File;
 4 import java.io.FileNotFoundException;
 5 import java.io.FileOutputStream;
 6 import java.io.IOException;
 7 
 8 import org.objectweb.asm.ClassAdapter;
 9 import org.objectweb.asm.ClassReader;
10 import org.objectweb.asm.ClassWriter;
11 
12 
13 
14 public class Generator {
15 
16     public static void main(String[] args){
17         try {
18             ClassReader cr = new ClassReader("com/asm5/C");
19             ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
20             ClassAdapter classAdapter = new AddTimeClassAdapter(cw);
21             //The given visitors to access Java class ClassReader
22             cr.accept(classAdapter, ClassReader.SKIP_DEBUG);
23             byte[] data = cw.toByteArray();
24             File file = new File(System.getProperty("user.dir") + "\\WebRoot\\WEB-INF\\classes\\com\\asm5\\C.class");
25             FileOutputStream fout = new FileOutputStream(file);
26             fout.write(data);
27             fout.close();
28             System.out.println("success!");
29         } catch (FileNotFoundException e) {
30             e.printStackTrace();
31         } catch (IOException e) {
32             e.printStackTrace();
33         }
34     }
35 
36 }

The following is a test class:

 1 package com.asm5;
 2 
 3 public class Test {
 4     public static void main(String[] args) throws InterruptedException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
 5         C c = new C();
 6         c.m();
 7         Class cc = c.getClass();
 8         System.out.println(cc.getField("timer").get(c));
 9     }
10 }

Output results: 100

To be continued....

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Laurent at November 15, 2013 - 12:55 PM