org.objectweb.asm.util

Class ASMifierClassVisitor

Implemented Interfaces:
ClassVisitor

public class ASMifierClassVisitor
extends ASMifierAbstractVisitor
implements ClassVisitor

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: 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");
     }
 }
 
Authors:
Eric Bruneton
Eugene Kuleshov

Field Summary

protected PrintWriter
pw
The print writer to be used to print the class.

Fields inherited from class org.objectweb.asm.util.ASMifierAbstractVisitor

name

Fields inherited from class org.objectweb.asm.util.AbstractVisitor

OPCODES, TYPES, buf, text

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 signature, String superName, String[] interfaces)
AnnotationVisitor
visitAnnotation(String desc, boolean visible)
void
visitEnd()
FieldVisitor
visitField(int access, String name, String desc, String signature, Object value)
void
visitInnerClass(String name, String outerName, String innerName, int access)
MethodVisitor
visitMethod(int access, String name, String desc, String signature, String[] exceptions)
void
visitOuterClass(String owner, String name, String desc)
void
visitSource(String file, String debug)

Methods inherited from class org.objectweb.asm.util.ASMifierAbstractVisitor

visitAnnotation, visitAttribute, visitEnd

Methods inherited from class org.objectweb.asm.util.AbstractVisitor

appendString, getDefaultAttributes, getText

Field Details

pw

protected final PrintWriter pw
The print writer to be used to print the class.

Constructor Details

ASMifierClassVisitor

public ASMifierClassVisitor(PrintWriter pw)
Parameters:
pw - the print writer to be used to print the class.

Method Details

main

public static void main(String[] args)
            throws Exception
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.

visit

public void visit(int version,
                  int access,
                  String name,
                  String signature,
                  String superName,
                  String[] interfaces)
Specified by:
visit in interface ClassVisitor

visitAnnotation

public AnnotationVisitor visitAnnotation(String desc,
                                         boolean visible)
Specified by:
visitAnnotation in interface ClassVisitor
Overrides:
visitAnnotation in interface ASMifierAbstractVisitor

visitEnd

public void visitEnd()
Specified by:
visitEnd in interface ClassVisitor
Overrides:
visitEnd in interface ASMifierAbstractVisitor

visitField

public FieldVisitor visitField(int access,
                               String name,
                               String desc,
                               String signature,
                               Object value)
Specified by:
visitField in interface ClassVisitor

visitInnerClass

public void visitInnerClass(String name,
                            String outerName,
                            String innerName,
                            int access)
Specified by:
visitInnerClass in interface ClassVisitor

visitMethod

public MethodVisitor visitMethod(int access,
                                 String name,
                                 String desc,
                                 String signature,
                                 String[] exceptions)
Specified by:
visitMethod in interface ClassVisitor

visitOuterClass

public void visitOuterClass(String owner,
                            String name,
                            String desc)
Specified by:
visitOuterClass in interface ClassVisitor

visitSource

public void visitSource(String file,
                        String debug)
Specified by:
visitSource in interface ClassVisitor