org.objectweb.asm.util
public class ASMifierClassVisitor extends PrintClassVisitor
PrintClassVisitor 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:
main method);
import org.objectweb.asm.*;
import java.io.FileOutputStream;
public class Dump implements Constants {
public static void main (String[] args) throws Exception {
ClassWriter cw = new ClassWriter(false);
CodeVisitor cv;
cw.visit(ACC_PUBLIC + ACC_SUPER, "Hello", "java/lang/Object", null, "Hello.java");
{
cv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "main", "([Ljava/lang/String;)V", null, null);
cv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
cv.visitLdcInsn("hello");
cv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V");
cv.visitInsn(RETURN);
cv.visitMaxs(2, 1);
}
{
cv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
cv.visitVarInsn(ALOAD, 0);
cv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
cv.visitInsn(RETURN);
cv.visitMaxs(1, 1);
}
cw.visitEnd();
FileOutputStream os = new FileOutputStream("Dumped.class");
os.write(cw.toByteArray());
os.close();
}
}
where Hello is defined by:
public class Hello {
public static void main (String[] args) {
System.out.println("hello");
}
}
| Constructor Summary | |
|---|---|
| ASMifierClassVisitor(PrintWriter pw)
Constructs a new ASMifierClassVisitor object.
| |
| Method Summary | |
|---|---|
| static void | main(String[] args)
Prints the ASM source code to generate the given class to the standard
output.
|
| void | visit(int version, int access, String name, String superName, String[] interfaces, String sourceFile) |
| void | visitAttribute(Attribute attr) |
| void | visitEnd() |
| void | visitField(int access, String name, String desc, Object value, Attribute attrs) |
| void | visitInnerClass(String name, String outerName, String innerName, int access) |
| CodeVisitor | visitMethod(int access, String name, String desc, String[] exceptions, Attribute attrs) |
Parameters: pw the print writer to be used to print the class.
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 exception occurs.