Save This Page
Home » asm-3.1 » org.objectweb » asm » [javadoc | source]
org.objectweb.asm
public class: ClassWriter [javadoc | source]
java.lang.Object
   org.objectweb.asm.ClassWriter

All Implemented Interfaces:
    ClassVisitor

A ClassVisitor that generates classes in bytecode form. More precisely this visitor generates a byte array conforming to the Java class file format. It can be used alone, to generate a Java class "from scratch", or with one or more ClassReader and adapter class visitor to generate a modified class from one or more existing Java classes.
Field Summary
public static final  int COMPUTE_MAXS    Flag to automatically compute the maximum stack size and the maximum number of local variables of methods. If this flag is set, then the arguments of the visitMaxs method of the MethodVisitor returned by the visitMethod method will be ignored, and computed automatically from the signature and the bytecode of each method. 
public static final  int COMPUTE_FRAMES    Flag to automatically compute the stack map frames of methods from scratch. If this flag is set, then the calls to the MethodVisitor#visitFrame method are ignored, and the stack map frames are recomputed from the methods bytecode. The arguments of the visitMaxs method are also ignored and recomputed from the bytecode. In other words, computeFrames implies computeMaxs. 
static final  int NOARG_INSN    The type of instructions without any argument. 
static final  int SBYTE_INSN    The type of instructions with an signed byte argument. 
static final  int SHORT_INSN    The type of instructions with an signed short argument. 
static final  int VAR_INSN    The type of instructions with a local variable index argument. 
static final  int IMPLVAR_INSN    The type of instructions with an implicit local variable index argument. 
static final  int TYPE_INSN    The type of instructions with a type descriptor argument. 
static final  int FIELDORMETH_INSN    The type of field and method invocations instructions. 
static final  int ITFMETH_INSN    The type of the INVOKEINTERFACE instruction. 
static final  int LABEL_INSN    The type of instructions with a 2 bytes bytecode offset label. 
static final  int LABELW_INSN    The type of instructions with a 4 bytes bytecode offset label. 
static final  int LDC_INSN    The type of the LDC instruction. 
static final  int LDCW_INSN    The type of the LDC_W and LDC2_W instructions. 
static final  int IINC_INSN    The type of the IINC instruction. 
static final  int TABL_INSN    The type of the TABLESWITCH instruction. 
static final  int LOOK_INSN    The type of the LOOKUPSWITCH instruction. 
static final  int MANA_INSN    The type of the MULTIANEWARRAY instruction. 
static final  int WIDE_INSN    The type of the WIDE instruction. 
static final  byte[] TYPE    The instruction types of all JVM opcodes. 
static final  int CLASS    The type of CONSTANT_Class constant pool items. 
static final  int FIELD    The type of CONSTANT_Fieldref constant pool items. 
static final  int METH    The type of CONSTANT_Methodref constant pool items. 
static final  int IMETH    The type of CONSTANT_InterfaceMethodref constant pool items. 
static final  int STR    The type of CONSTANT_String constant pool items. 
static final  int INT    The type of CONSTANT_Integer constant pool items. 
static final  int FLOAT    The type of CONSTANT_Float constant pool items. 
static final  int LONG    The type of CONSTANT_Long constant pool items. 
static final  int DOUBLE    The type of CONSTANT_Double constant pool items. 
static final  int NAME_TYPE    The type of CONSTANT_NameAndType constant pool items. 
static final  int UTF8    The type of CONSTANT_Utf8 constant pool items. 
static final  int TYPE_NORMAL    Normal type Item stored in the ClassWriter ClassWriter#typeTable , instead of the constant pool, in order to avoid clashes with normal constant pool items in the ClassWriter constant pool's hash table. 
static final  int TYPE_UNINIT    Uninitialized type Item stored in the ClassWriter ClassWriter#typeTable , instead of the constant pool, in order to avoid clashes with normal constant pool items in the ClassWriter constant pool's hash table. 
static final  int TYPE_MERGED    Merged type Item stored in the ClassWriter ClassWriter#typeTable , instead of the constant pool, in order to avoid clashes with normal constant pool items in the ClassWriter constant pool's hash table. 
 ClassReader cr    The class reader from which this class writer was constructed, if any. 
 int version    Minor and major version numbers of the class to be generated. 
 int index    Index of the next item to be added in the constant pool. 
final  ByteVector pool    The constant pool of this class. 
 Item[] items    The constant pool's hash table data. 
 int threshold    The threshold of the constant pool's hash table. 
final  Item key    A reusable key used to look for items in the #items hash table. 
final  Item key2    A reusable key used to look for items in the #items hash table. 
final  Item key3    A reusable key used to look for items in the #items hash table. 
 Item[] typeTable    A type table used to temporarily store internal names that will not necessarily be stored in the constant pool. This type table is used by the control flow and data flow analysis algorithm used to compute stack map frames from scratch. This array associates to each index i the Item whose index is i. All Item objects stored in this array are also stored in the #items hash table. These two arrays allow to retrieve an Item from its index or, conversely, to get the index of an Item from its value. Each Item stores an internal name in its Item#strVal1 field. 
 String thisName    The internal name of this class. 
 FieldWriter firstField    The fields of this class. These fields are stored in a linked list of FieldWriter objects, linked to each other by their FieldWriter#next field. This field stores the first element of this list. 
 FieldWriter lastField    The fields of this class. These fields are stored in a linked list of FieldWriter objects, linked to each other by their FieldWriter#next field. This field stores the last element of this list. 
 MethodWriter firstMethod    The methods of this class. These methods are stored in a linked list of MethodWriter objects, linked to each other by their MethodWriter#next field. This field stores the first element of this list. 
 MethodWriter lastMethod    The methods of this class. These methods are stored in a linked list of MethodWriter objects, linked to each other by their MethodWriter#next field. This field stores the last element of this list. 
 boolean invalidFrames    true if the stack map tables of this class are invalid. The MethodWriter#resizeInstructions method cannot transform existing stack map tables, and so produces potentially invalid classes when it is executed. In this case the class is reread and rewritten with the #COMPUTE_FRAMES option (the resizeInstructions method can resize stack map tables when this option is used). 
Constructor:
 public ClassWriter(int flags) 
 public ClassWriter(ClassReader classReader,
    int flags) 
    Constructs a new ClassWriter object and enables optimizations for "mostly add" bytecode transformations. These optimizations are the following:
    • The constant pool from the original class is copied as is in the new class, which saves time. New constant pool entries will be added at the end if necessary, but unused constant pool entries won't be removed.
    • Methods that are not transformed are copied as is in the new class, directly from the original class bytecode (i.e. without emitting visit events for all the method instructions), which saves a lot of time. Untransformed methods are detected by the fact that the ClassReader receives MethodVisitor objects that come from a ClassWriter (and not from a custom ClassAdapter or any other ClassVisitor instance).
    Parameters:
    classReader - the ClassReader used to read the original class. It will be used to copy the entire constant pool from the original class and also to copy other fragments of original bytecode where applicable.
    flags - option flags that can be used to modify the default behavior of this class. See #COMPUTE_MAXS , #COMPUTE_FRAMES .
Method from org.objectweb.asm.ClassWriter Summary:
addType,   addUninitializedType,   getCommonSuperClass,   getMergedType,   newClass,   newClassItem,   newConst,   newConstItem,   newDouble,   newField,   newFieldItem,   newFloat,   newInteger,   newLong,   newMethod,   newMethodItem,   newNameType,   newUTF8,   toByteArray,   visit,   visitAnnotation,   visitAttribute,   visitEnd,   visitField,   visitInnerClass,   visitMethod,   visitOuterClass,   visitSource
Methods from java.lang.Object:
equals,   getClass,   hashCode,   notify,   notifyAll,   toString,   wait,   wait,   wait
Method from org.objectweb.asm.ClassWriter Detail:
 int addType(String type) 
    Adds the given internal name to #typeTable and returns its index. Does nothing if the type table already contains this internal name.
 int addUninitializedType(String type,
    int offset) 
    Adds the given "uninitialized" type to #typeTable and returns its index. This method is used for UNINITIALIZED types, made of an internal name and a bytecode offset.
 protected String getCommonSuperClass(String type1,
    String type2) 
    Returns the common super type of the two given types. The default implementation of this method loads the two given classes and uses the java.lang.Class methods to find the common super class. It can be overridden to compute this common super type in other ways, in particular without actually loading any class, or to take into account the class that is currently being generated by this ClassWriter, which can of course not be loaded since it is under construction.
 int getMergedType(int type1,
    int type2) 
    Returns the index of the common super type of the two given types. This method calls #getCommonSuperClass and caches the result in the #items hash table to speedup future calls with the same parameters.
 public int newClass(String value) 
    Adds a class reference to the constant pool of the class being build. Does nothing if the constant pool already contains a similar item. This method is intended for Attribute sub classes, and is normally not needed by class generators or adapters.
 Item newClassItem(String value) 
    Adds a class reference to the constant pool of the class being build. Does nothing if the constant pool already contains a similar item. This method is intended for Attribute sub classes, and is normally not needed by class generators or adapters.
 public int newConst(Object cst) 
    Adds a number or string constant to the constant pool of the class being build. Does nothing if the constant pool already contains a similar item. This method is intended for Attribute sub classes, and is normally not needed by class generators or adapters.
 Item newConstItem(Object cst) 
    Adds a number or string constant to the constant pool of the class being build. Does nothing if the constant pool already contains a similar item.
 Item newDouble(double value) 
    Adds a double to the constant pool of the class being build. Does nothing if the constant pool already contains a similar item.
 public int newField(String owner,
    String name,
    String desc) 
    Adds a field reference to the constant pool of the class being build. Does nothing if the constant pool already contains a similar item. This method is intended for Attribute sub classes, and is normally not needed by class generators or adapters.
 Item newFieldItem(String owner,
    String name,
    String desc) 
    Adds a field reference to the constant pool of the class being build. Does nothing if the constant pool already contains a similar item.
 Item newFloat(float value) 
    Adds a float to the constant pool of the class being build. Does nothing if the constant pool already contains a similar item.
 Item newInteger(int value) 
    Adds an integer to the constant pool of the class being build. Does nothing if the constant pool already contains a similar item.
 Item newLong(long value) 
    Adds a long to the constant pool of the class being build. Does nothing if the constant pool already contains a similar item.
 public int newMethod(String owner,
    String name,
    String desc,
    boolean itf) 
    Adds a method reference to the constant pool of the class being build. Does nothing if the constant pool already contains a similar item. This method is intended for Attribute sub classes, and is normally not needed by class generators or adapters.
 Item newMethodItem(String owner,
    String name,
    String desc,
    boolean itf) 
    Adds a method reference to the constant pool of the class being build. Does nothing if the constant pool already contains a similar item.
 public int newNameType(String name,
    String desc) 
    Adds a name and type to the constant pool of the class being build. Does nothing if the constant pool already contains a similar item. This method is intended for Attribute sub classes, and is normally not needed by class generators or adapters.
 public int newUTF8(String value) 
    Adds an UTF8 string to the constant pool of the class being build. Does nothing if the constant pool already contains a similar item. This method is intended for Attribute sub classes, and is normally not needed by class generators or adapters.
 public byte[] toByteArray() 
    Returns the bytecode of the class that was build with this class writer.
 public  void visit(int version,
    int access,
    String name,
    String signature,
    String superName,
    String[] interfaces) 
 public AnnotationVisitor visitAnnotation(String desc,
    boolean visible) 
 public  void visitAttribute(Attribute attr) 
 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)