深入了解Java ClassLoader、Bytecode 、ASM、cglib
- import java.io.ByteArrayOutputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.IOException;
- public class FileClassLoader extends ClassLoader {
- public Class findClass(String name) {
- byte[] data = loadClassData(name);
- return defineClass(name, data, 0, data.length);
- }
- private byte[] loadClassData(String name) {
- FileInputStream fis = null;
- byte[] data = null;
- try {
- fis = new FileInputStream(new File("D:\\project\\test\\" + name + ".class"));
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- int ch = 0;
- while ((ch = fis.read()) != -1) {
- baos.write(ch);
- }
- data = baos.toByteArray();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return data;
- }
- }
MyApp.java:
- public class MyApp {
- public static void main(String[] args) throws Exception {
- FileClassLoader loader = new FileClassLoader();
- Class objClass = loader.findClass("MyApp");
- Object obj = objClass.newInstance();
- System.out.println(objClass.getName());
- System.out.println(objClass.getClassLoader());
- System.out.println(obj);
- }
- }
編譯并運行MyApp類,結果為:
- MyApp
- FileClassLoader@757aef
- MyApp@9cab16
二、Bytecode
1,什么是Bytecode
C/C++編譯器把源代碼編譯成匯編代碼,Java編譯器把Java源代碼編譯成字節碼bytecode。
Java跨平臺其實就是基于相同的bytecode規范做不同平臺的虛擬機,我們的Java程序編譯成bytecode后就可以在不同平臺跑了。
.net框架有IL(intermediate language),匯編是C/C++程序的中間表達方式,而bytecode可以說是Java平臺的中間語言。
了解Java字節碼知識對debugging、performance tuning以及做一些高級語言擴展或框架很有幫助。
2,使用javap生成Bytecode
JDK自帶的javap.exe文件可以反匯編Bytecode,讓我們看個例子:
Test.java:
- public class Test {
- public static void main(String[] args) {
- int i = 10000;
- System.out.println("Hello Bytecode! Number = " + i);
- }
- }
編譯后的Test.class:
- 漱壕 1 +
- <init> ()V Code LineNumberTable main ([Ljava/lang/String;)V
- SourceFile Test.java
- ! " java/lang/StringBuilder Hello Bytecode! Number = # $ # % & ' ( ) * Test java/lang/Object java/lang/System out Ljava/io/PrintStream; append -(Ljava/lang/String;)Ljava/lang/StringBuilder; (I)Ljava/lang/StringBuilder; toString ()Ljava/lang/String; java/io/PrintStream println (Ljava/lang/String;)V !
- * > '< Y
使用javap -c Test > Test.bytecode生成的Test.bytecode:
- Compiled from "Test.java"
- public class Test extends java.lang.Object{
- public Test();
- Code:
- 0: aload_0
- 1: invokespecial #1; //Method java/lang/Object."<init>":()V
- 4: return
- public static void main(java.lang.String[]);
- Code:
- 0: sipush 10000
- 3: istore_1
- 4: getstatic #2; //Field java/lang/System.out:Ljava/io/PrintStream;
- 7: new #3; //class java/lang/StringBuilder
- 10: dup
- 11: invokespecial #4; //Method java/lang/StringBuilder."<init>":()V
- 14: ldc #5; //String Hello Bytecode! Number =
- 16: invokevirtual #6; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
- 19: iload_1
- 20: invokevirtual #7; //Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
- 23: invokevirtual #8; //Method java/lang/StringBuilder.toString:()Ljava/lang/String;
- 26: invokevirtual #9; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
- 29: return
- }
JVM就是一個基于stack的機器,每個thread擁有一個存儲著一些frames的JVM stack,每次調用一個方法時生成一個frame。
一個frame包括一個local variables數組(本地變量表),一個Operand LIFO stack和運行時常量池的一個引用。
我們來簡單分析一下生成的字節碼指令:
aload和iload指令的“a”前綴和“i”分別表示對象引用和int類型,其他還有“b”表示byte,“c”表示char,“d”表示double等等
我們這里的aload_0表示將把local variable table中index 0的值push到Operand stack,iload_1類似
invokespecial表示初始化對象,return表示返回
sipush表示把10000這個int值push到Operand stack
getstatic表示取靜態域
invokevirtual表示調用一些實例方法
這些指令又稱為opcode,Java一直以來只有約202個Opcode,具體請參考Java Bytecode規范。
我們看到Test.class文件不全是二進制的指令,有些是我們可以識別的字符,這是因為有些包名、類名和常量字符串沒有編譯成二進制Bytecode指令。
3,體驗字節碼增強的魔力
我們J2EE常用的Hibernate、Spring都用到了動態字節碼修改來改變類的行為。
讓我們通過看看ASM的org.objectweb.asm.MethodWriter類的部分方法來理解ASM是如何修改字節碼的:
- class MethodWriter implements MethodVisitor {
- private ByteVector code = new ByteVector();
- public void visitIntInsn(final int opcode, final int operand) {
- // Label currentBlock = this.currentBlock;
- if (currentBlock != null) {
- if (compute == FRAMES) {
- currentBlock.frame.execute(opcode, operand, null, null);
- } else if (opcode != Opcodes.NEWARRAY) {
- // updates current and max stack sizes only for NEWARRAY
- // (stack size variation = 0 for BIPUSH or SIPUSH)
- int size = stackSize + 1;
- if (size > maxStackSize) {
- maxStackSize = size;
- }
- stackSize = size;
- }
- }
- // adds the instruction to the bytecode of the method
- if (opcode == Opcodes.SIPUSH) {
- code.put12(opcode, operand);
- } else { // BIPUSH or NEWARRAY
- code.put11(opcode, operand);
- }
- }
- public void visitMethodInsn(
- final int opcode,
- final String owner,
- final String name,
- final String desc)
- {
- boolean itf = opcode == Opcodes.INVOKEINTERFACE;
- Item i = cw.newMethodItem(owner, name, desc, itf);
- int argSize = i.intVal;
- // Label currentBlock = this.currentBlock;
- if (currentBlock != null) {
- if (compute == FRAMES) {
- currentBlock.frame.execute(opcode, 0, cw, i);
- } else {
- /*
- * computes the stack size variation. In order not to recompute
- * several times this variation for the same Item, we use the
- * intVal field of this item to store this variation, once it
- * has been computed. More precisely this intVal field stores
- * the sizes of the arguments and of the return value
- * corresponding to desc.
- */
- if (argSize == 0) {
- // the above sizes have not been computed yet,
- // so we compute them...
- argSize = getArgumentsAndReturnSizes(desc);
- // ... and we save them in order
- // not to recompute them in the future
- i.intVal = argSize;
- }
- int size;
- if (opcode == Opcodes.INVOKESTATIC) {
- size = stackSize - (argSize >> 2) + (argSize & 0x03) + 1;
- } else {
- size = stackSize - (argSize >> 2) + (argSize & 0x03);
- }
- // updates current and max stack sizes
- if (size > maxStackSize) {
- maxStackSize = size;
- }
- stackSize = size;
- }
- }
- // adds the instruction to the bytecode of the method
- if (itf) {
- if (argSize == 0) {
- argSize = getArgumentsAndReturnSizes(desc);
- i.intVal = argSize;
- }
- code.put12(Opcodes.INVOKEINTERFACE, i.index).put11(argSize >> 2, 0);
- } else {
- code.put12(opcode, i.index);
- }
- }
- }
通過注釋我們可以大概理解visitIntInsn和visitMethodInsn方法的意思。
比如visitIntInsn先計算stack的size,然后根據opcode來判斷是SIPUSH指令還是BIPUSH or NEWARRAY指令,并相應的調用字節碼修改相關的方法。
三、ASM
我們知道Java是靜態語言,而python、ruby是動態語言,Java程序一旦寫好很難在運行時更改類的行為,而python、ruby可以。
不過基于bytecode層面上我們可以做一些手腳,來使Java程序多一些靈活性和Magic,ASM就是這樣一個應用廣泛的開源庫。
ASM is a Java bytecode manipulation framework. It can be used to dynamically generate stub classes or other proxy classes,
directly in binary form, or to dynamically modify classes at load time, i.e., just before they are loaded into the Java
Virtual Machine.
ASM完成了BCEL和SERP同樣的功能,但ASM
只有30多k,而后兩者分別是350k和150k。apache真是越來越過氣了。
讓我們來看一個ASM的簡單例子Helloworld.java,它生成一個Example類和一個main方法,main方法打印"Hello world!"語句:
- import java.io.FileOutputStream;
- import java.io.PrintStream;
- import org.objectweb.asm.ClassWriter;
- import org.objectweb.asm.MethodVisitor;
- import org.objectweb.asm.Opcodes;
- import org.objectweb.asm.Type;
- import org.objectweb.asm.commons.GeneratorAdapter;
- import org.objectweb.asm.commons.Method;
- public class Helloworld extends ClassLoader implements Opcodes {
- public static void main(final String args[]) throws Exception {
- // creates a ClassWriter for the Example public class,
- // which inherits from Object
- ClassWriter cw = new ClassWriter(0);
- cw.visit(V1_1, ACC_PUBLIC, "Example", null, "java/lang/Object", null);
- MethodVisitor mw = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null,
- null);
- mw.visitVarInsn(ALOAD, 0);
- mw.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
- mw.visitInsn(RETURN);
- mw.visitMaxs(1, 1);
- mw.visitEnd();
- mw = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "main",
- "([Ljava/lang/String;)V", null, null);
- mw.visitFieldInsn(GETSTATIC, "java/lang/System", "out",
- "Ljava/io/PrintStream;");
- mw.visitLdcInsn("Hello world!");
- mw.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println",
- "(Ljava/lang/String;)V");
- mw.visitInsn(RETURN);
- mw.visitMaxs(2, 2);
- mw.visitEnd();
- byte[] code = cw.toByteArray();
- FileOutputStream fos = new FileOutputStream("Example.class");
- fos.write(code);
- fos.close();
- Helloworld loader = new Helloworld();
- Class exampleClass = loader
- .defineClass("Example", code, 0, code.length);
- exampleClass.getMethods()[0].invoke(null, new Object[] { null });
- // ------------------------------------------------------------------------
- // Same example with a GeneratorAdapter (more convenient but slower)
- // ------------------------------------------------------------------------
- cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
- cw.visit(V1_1, ACC_PUBLIC, "Example", null, "java/lang/Object", null);
- Method m = Method.getMethod("void <init> ()");
- GeneratorAdapter mg = new GeneratorAdapter(ACC_PUBLIC, m, null, null,
- cw);
- mg.loadThis();
- mg.invokeConstructor(Type.getType(Object.class), m);
- mg.returnValue();
- mg.endMethod();
- m = Method.getMethod("void main (String[])");
- mg = new GeneratorAdapter(ACC_PUBLIC + ACC_STATIC, m, null, null, cw);
- mg.getStatic(Type.getType(System.class), "out", Type
- .getType(PrintStream.class));
- mg.push("Hello world!");
- mg.invokeVirtual(Type.getType(PrintStream.class), Method
- .getMethod("void println (String)"));
- mg.returnValue();
- mg.endMethod();
- cw.visitEnd();
- code = cw.toByteArray();
- loader = new Helloworld();
- exampleClass = loader.defineClass("Example", code, 0, code.length);
- exampleClass.getMethods()[0].invoke(null, new Object[] { null });
- }
- }
我們看到上面的例子分別使用ASM的MethodVisitor和GeneratorAdapter兩種方式來動態生成Example類并調用打印語句。
四、cglib
cglib is a powerful, high performance and quality Code Generation Library, It is used to extend JAVA classes and implements interfaces at runtime.
cglib是Code Generation Library的縮寫。
cglib依賴于ASM庫。
Hibernate主要是利用cglib生成pojo的子類并override get方法來實現lazy loading機制,Spring則是利用cglib來實現動態代理。
而JDK的動態代理機制要求有接口才行,這樣就強制我們的pojo實現某個接口。
這里還是提供一個cglib的入門級的示例:
MyClass.java:
- public class MyClass {
- public void print() {
- System.out.println("I'm in MyClass.print!");
- }
- }
Main.java:
- import java.lang.reflect.Method;
- import net.sf.cglib.proxy.Enhancer;
- import net.sf.cglib.proxy.MethodInterceptor;
- import net.sf.cglib.proxy.MethodProxy;
- public class Main {
- public static void main(String[] args) {
- Enhancer enhancer = new Enhancer();
- enhancer.setSuperclass(MyClass.class);
- enhancer.setCallback(new MethodInterceptorImpl());
- MyClass my = (MyClass) enhancer.create();
- my.print();
- }
- private static class MethodInterceptorImpl implements MethodInterceptor {
- public Object intercept(Object obj, Method method, Object[] args,
- MethodProxy proxy) throws Throwable {
- // log something
- System.out.println(method + " intercepted!");
- proxy.invokeSuper(obj, args);
- return null;
- }
- }
- }
打印結果為:
- public void MyClass.print() intercepted!
- I'm in MyClass.print!
這個示例就基本上實現了日志AOP的功能,很簡單吧。
參考資料
CLR和JRE的運行機制的初步總結
Java虛擬機
了解Java ClassLoader
Java Virtual Machine Specification
Java bytecode
解讀字節碼文件
Java Bytecode Specification and Verification
ASM User Guide
Hello, ASM
cglig指南
Java下的框架編程--cglib的應用
AOP = Proxy Pattern + Method Reflection + Aspect DSL + 自動代碼生成
深入淺出Spring AOP
Traceback:http://www.javaeye.com/topic/98178