Java字节码操纵框架。它能够直接以二进制形式动态地生成stub类或其余代理类,或者在装载时动态地修改类。ASM提供相似于BCEL和SERP之类的工具包的功能,可是被设计得更小巧、更快速,这使它适用于实时代码插装。
.NET/liyangbing315/article/details/5472862
你能够利用ASM动态操做class
java
咱们知道Java是静态语言,而Python、ruby是动态语言,Java程序一旦写好很难在运行时更改类的行为,而Python、ruby能够。
不过基于bytecode层面上咱们能够作一些手脚,来使Java程序多一些灵活性和Magic,ASM就是这样一个应用普遍的开源库。
ASM is a Java bytecode manipulation framework. It can be used to dynamically generatestub classes or other proxy classes,
directly in binary form, or to dynamically modify classes at load time, i.e., justbefore 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!"语句:python
Java代码web
- 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类并调用打印语句。apache