| java.lang.Object org.drools.asm.util.AbstractVisitor org.drools.asm.util.ASMifierAbstractVisitor org.drools.asm.util.ASMifierClassVisitor
ASMifierClassVisitor | public class ASMifierClassVisitor extends ASMifierAbstractVisitor implements ClassVisitor(Code) | | A
ClassVisitor that prints the ASM code that generates the classes it
visits. This class visitor can be used to quickly write ASM code to generate
some given bytecode: - write the Java source code equivalent to the
bytecode you want to generate;
- compile it with javac;
- make a
ASMifierClassVisitor visit this compiled class (see the
ASMifierClassVisitor.main main method);
- edit the generated source code, if
necessary.
The source code printed when visiting the
Hello class is the following:
import org.objectweb.asm.*;
public class HelloDump implements Opcodes {
public static byte[] dump() throws Exception {
ClassWriter cw = new ClassWriter(false);
FieldVisitor fv;
MethodVisitor mv;
AnnotationVisitor av0;
cw.visit(49,
ACC_PUBLIC + ACC_SUPER,
"Hello",
null,
"java/lang/Object",
null);
cw.visitSource("Hello.java", null);
{
mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
mv.visitVarInsn(ALOAD, 0);
mv.visitMethodInsn(INVOKESPECIAL,
"java/lang/Object",
"<init>",
"()V");
mv.visitInsn(RETURN);
mv.visitMaxs(1, 1);
mv.visitEnd();
}
{
mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC,
"main",
"([Ljava/lang/String;)V",
null,
null);
mv.visitFieldInsn(GETSTATIC,
"java/lang/System",
"out",
"Ljava/io/PrintStream;");
mv.visitLdcInsn("hello");
mv.visitMethodInsn(INVOKEVIRTUAL,
"java/io/PrintStream",
"println",
"(Ljava/lang/String;)V");
mv.visitInsn(RETURN);
mv.visitMaxs(2, 1);
mv.visitEnd();
}
cw.visitEnd();
return cw.toByteArray();
}
}
where Hello is defined by:
public class Hello {
public static void main(String[] args) {
System.out.println("hello");
}
}
author: Eric Bruneton author: Eugene Kuleshov |
Field Summary | |
final protected PrintWriter | pw The print writer to be used to print the class. |
Method Summary | |
void | appendAccess(int access) Appends a string representation of the given access modifiers to
ASMifierClassVisitor.buf buf . | static void | appendConstant(StringBuffer buf, Object cst) Appends a string representation of the given constant to the given
buffer. | public static void | main(String[] args) Prints the ASM source code to generate the given class to the standard
output. | public void | visit(int version, int access, String name, String signature, String superName, String[] interfaces) | public AnnotationVisitor | visitAnnotation(String desc, boolean visible) | public void | visitEnd() | public FieldVisitor | visitField(int access, String name, String desc, String signature, Object value) | public void | visitInnerClass(String name, String outerName, String innerName, int access) | public MethodVisitor | visitMethod(int access, String name, String desc, String signature, String[] exceptions) | public void | visitOuterClass(String owner, String name, String desc) | public void | visitSource(String file, String debug) |
pw | final protected PrintWriter pw(Code) | | The print writer to be used to print the class.
|
appendAccess | void appendAccess(int access)(Code) | | Appends a string representation of the given access modifiers to
ASMifierClassVisitor.buf buf .
Parameters: access - some access modifiers. |
main | public static void main(String[] args) throws Exception(Code) | | Prints the ASM source code to generate the given class to the standard
output. Usage: ASMifierClassVisitor [-debug] <fully qualified
class name or class file name>
Parameters: args - the command line arguments. throws: Exception - if the class cannot be found, or if an IO exceptionoccurs. |
visitEnd | public void visitEnd()(Code) | | |
|
|