FileDocCategorySizeDatePackage
ClassWriter.javaAPI DocGlassfish v2 API30715Thu Mar 02 11:51:12 GMT 2006oracle.toplink.libraries.asm

ClassWriter

public class ClassWriter extends Object implements ClassVisitor
A {@link ClassVisitor ClassVisitor} that generates Java class files. 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 {@link ClassReader ClassReader} and adapter class visitor to generate a modified class from one or more existing Java classes.
author
Eric Bruneton

Fields Summary
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.
private int
version
Minor and major version numbers of the class to be generated.
private short
index
Index of the next item to be added in the constant pool.
private ByteVector
pool
The constant pool of this class.
private Item[]
items
The constant pool's hash table data.
private int
threshold
The threshold of the constant pool's hash table.
private int
access
The access flags of this class.
private int
name
The constant pool item that contains the internal name of this class.
private int
superName
The constant pool item that contains the internal name of the super class of this class.
private int
interfaceCount
Number of interfaces implemented or extended by this class or interface.
private int[]
interfaces
The interfaces implemented or extended by this class or interface. More precisely, this array contains the indexes of the constant pool items that contain the internal names of these interfaces.
private int
sourceFile
The index of the constant pool item that contains the name of the source file from which this class was compiled.
private int
fieldCount
Number of fields of this class.
private ByteVector
fields
The fields of this class.
private boolean
computeMaxs
true if the maximum stack size and number of local variables must be automatically computed.
boolean
checkAttributes
true to test that all attributes are known.
CodeWriter
firstMethod
The methods of this class. These methods are stored in a linked list of {@link CodeWriter CodeWriter} objects, linked to each other by their {@link CodeWriter#next} field. This field stores the first element of this list.
CodeWriter
lastMethod
The methods of this class. These methods are stored in a linked list of {@link CodeWriter CodeWriter} objects, linked to each other by their {@link CodeWriter#next} field. This field stores the last element of this list.
private int
innerClassesCount
The number of entries in the InnerClasses attribute.
private ByteVector
innerClasses
The InnerClasses attribute.
private Attribute
attrs
The non standard attributes of the class.
Item
key
A reusable key used to look for items in the hash {@link #items items}.
Item
key2
A reusable key used to look for items in the hash {@link #items items}.
Item
key3
A reusable key used to look for items in the hash {@link #items items}.
static final int
NOARG_INSN
The type of instructions without any label.
static final int
SBYTE_INSN
The type of instructions with an signed byte label.
static final int
SHORT_INSN
The type of instructions with an signed short label.
static final int
VAR_INSN
The type of instructions with a local variable index label.
static final int
IMPLVAR_INSN
The type of instructions with an implicit local variable index label.
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 byte[]
TYPE
The instruction types of all JVM opcodes.
Constructors Summary
public ClassWriter(boolean computeMaxs)
Constructs a new {@link ClassWriter ClassWriter} object.

param
computeMaxs true if the maximum stack size and the maximum number of local variables must be automatically computed. If this flag is true, then the arguments of the {@link CodeVisitor#visitMaxs visitMaxs} method of the {@link CodeVisitor CodeVisitor} returned by the {@link #visitMethod visitMethod} method will be ignored, and computed automatically from the signature and the bytecode of each method.


  // --------------------------------------------------------------------------
  // Static initializer
  // --------------------------------------------------------------------------

            

   
    int i;
    byte[] b = new byte[220];
    String s =
      "AAAAAAAAAAAAAAAABCKLLDDDDDEEEEEEEEEEEEEEEEEEEEAAAAAAAADDDDDEEEEEEEEE" +
      "EEEEEEEEEEEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMAAA" +
      "AAAAAAAAAAAAAAAAAIIIIIIIIIIIIIIIIDNOAAAAAAGGGGGGGHAFBFAAFFAAQPIIJJII" +
      "IIIIIIIIIIIIIIII";
    for (i = 0; i < b.length; ++i) {
      b[i] = (byte)(s.charAt(i) - 'A");
    }
    TYPE = b;

    /* code to generate the above string

    // SBYTE_INSN instructions
    b[Constants.NEWARRAY] = SBYTE_INSN;
    b[Constants.BIPUSH] = SBYTE_INSN;

    // SHORT_INSN instructions
    b[Constants.SIPUSH] = SHORT_INSN;

    // (IMPL)VAR_INSN instructions
    b[Constants.RET] = VAR_INSN;
    for (i = Constants.ILOAD; i <= Constants.ALOAD; ++i) {
      b[i] = VAR_INSN;
    }
    for (i = Constants.ISTORE; i <= Constants.ASTORE; ++i) {
      b[i] = VAR_INSN;
    }
    for (i = 26; i <= 45; ++i) { // ILOAD_0 to ALOAD_3
      b[i] = IMPLVAR_INSN;
    }
    for (i = 59; i <= 78; ++i) { // ISTORE_0 to ASTORE_3
      b[i] = IMPLVAR_INSN;
    }

    // TYPE_INSN instructions
    b[Constants.NEW] = TYPE_INSN;
    b[Constants.ANEWARRAY] = TYPE_INSN;
    b[Constants.CHECKCAST] = TYPE_INSN;
    b[Constants.INSTANCEOF] = TYPE_INSN;

    // (Set)FIELDORMETH_INSN instructions
    for (i = Constants.GETSTATIC; i <= Constants.INVOKESTATIC; ++i) {
      b[i] = FIELDORMETH_INSN;
    }
    b[Constants.INVOKEINTERFACE] = ITFMETH_INSN;

    // LABEL(W)_INSN instructions
    for (i = Constants.IFEQ; i <= Constants.JSR; ++i) {
      b[i] = LABEL_INSN;
    }
    b[Constants.IFNULL] = LABEL_INSN;
    b[Constants.IFNONNULL] = LABEL_INSN;
    b[200] = LABELW_INSN; // GOTO_W
    b[201] = LABELW_INSN; // JSR_W
    // temporary opcodes used internally by ASM - see Label and CodeWriter
    for (i = 202; i < 220; ++i) {
      b[i] = LABEL_INSN;
    }

    // LDC(_W) instructions
    b[Constants.LDC] = LDC_INSN;
    b[19] = LDCW_INSN; // LDC_W
    b[20] = LDCW_INSN; // LDC2_W

    // special instructions
    b[Constants.IINC] = IINC_INSN;
    b[Constants.TABLESWITCH] = TABL_INSN;
    b[Constants.LOOKUPSWITCH] = LOOK_INSN;
    b[Constants.MULTIANEWARRAY] = MANA_INSN;
    b[196] = WIDE_INSN; // WIDE

    for (i = 0; i < b.length; ++i) {
      System.err.print((char)('A' + b[i]));
    }
    System.err.println();
    */
  
    this(computeMaxs, false);
  
public ClassWriter(boolean computeMaxs, boolean skipUnknownAttributes)
Constructs a new {@link ClassWriter ClassWriter} object.

param
computeMaxs true if the maximum stack size and the maximum number of local variables must be automatically computed. If this flag is true, then the arguments of the {@link CodeVisitor#visitMaxs visitMaxs} method of the {@link CodeVisitor CodeVisitor} returned by the {@link #visitMethod visitMethod} method will be ignored, and computed automatically from the signature and the bytecode of each method.
param
skipUnknownAttributes true to silently ignore unknown attributes, or false to throw an exception if an unknown attribute is found.

    index = 1;
    pool = new ByteVector();
    items = new Item[64];
    threshold = (int)(0.75d*items.length);
    key = new Item();
    key2 = new Item();
    key3 = new Item();
    this.computeMaxs = computeMaxs;
    this.checkAttributes = !skipUnknownAttributes;
  
Methods Summary
private Itemget(Item key)
Returns the constant pool's hash table item which is equal to the given item.

param
key a constant pool item.
return
the constant pool's hash table item which is equal to the given item, or null if there is no such item.

    int h = key.hashCode;
    Item i = items[h % items.length];
    while (i != null) {
      if (i.hashCode == h && key.isEqualTo(i)) {
        return i;
      }
      i = i.next;
    }
    return null;
  
public intnewClass(java.lang.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 {@link Attribute} sub classes, and is normally not needed by class generators or adapters.

param
value the internal name of the class.
return
the index of a new or already existing class reference item.

    return newClassItem(value).index;
  
private ItemnewClassItem(java.lang.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 {@link Attribute} sub classes, and is normally not needed by class generators or adapters.

param
value the internal name of the class.
return
a new or already existing class reference item.

    key2.set(CLASS, value, null, null);
    Item result = get(key2);
    if (result == null) {
      pool.put12(CLASS, newUTF8(value));
      result = new Item(index++, key2);
      put(result);
    }
    return result;
  
public intnewConst(java.lang.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 {@link Attribute} sub classes, and is normally not needed by class generators or adapters.

param
cst the value of the constant to be added to the constant pool. This parameter must be an {@link java.lang.Integer Integer}, a {@link java.lang.Float Float}, a {@link java.lang.Long Long}, a {@link java.lang.Double Double} or a {@link String String}.
return
the index of a new or already existing constant item with the given value.

    return newConstItem(cst).index;
  
public intnewConstDouble(double d)

    return newDouble(d).index;
  
public intnewConstFloat(float f)

    return newFloat(f).index;
  
public intnewConstInt(int i)

    return newInteger(i).index;
  
ItemnewConstItem(java.lang.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.

param
cst the value of the constant to be added to the constant pool. This parameter must be an {@link java.lang.Integer Integer}, a {@link java.lang.Float Float}, a {@link java.lang.Long Long}, a {@link java.lang.Double Double}, a {@link String String} or a {@link Type}.
return
a new or already existing constant item with the given value.

    if (cst instanceof Integer) {
      int val = ((Integer)cst).intValue();
      return newInteger(val);
    } else if (cst instanceof Byte) {
      int val = ((Byte)cst).intValue();
      return newInteger(val);
    } else if (cst instanceof Character) {
      int val = ((Character)cst).charValue();
      return newInteger(val);
    } else if (cst instanceof Short) {
      int val = ((Short)cst).intValue();
      return newInteger(val);
    } else if (cst instanceof Boolean) {
      int val = ((Boolean)cst).booleanValue() ? 1 : 0;
      return newInteger(val);
    } else if (cst instanceof Float) {
      float val = ((Float)cst).floatValue();
      return newFloat(val);
    } else if (cst instanceof Long) {
      long val = ((Long)cst).longValue();
      return newLong(val);
    } else if (cst instanceof Double) {
      double val = ((Double)cst).doubleValue();
      return newDouble(val);
    } else if (cst instanceof String) {
      return newString((String)cst);
    } else if (cst instanceof Type) {
      Type t = (Type)cst;
      return newClassItem(
        t.getSort() == Type.OBJECT ? t.getInternalName() : t.getDescriptor());
    } else {
      throw new IllegalArgumentException("value " + cst);
    }
  
public intnewConstLong(long l)

    return newLong(l).index;
  
private ItemnewDouble(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.

param
value the double value.
return
a new or already existing double item.

    key.set(value);
    Item result = get(key);
    if (result == null) {
      pool.putByte(DOUBLE).putLong(Double.doubleToLongBits(value));
      result = new Item(index, key);
      put(result);
      index += 2;
    }
    return result;
  
public intnewField(java.lang.String owner, java.lang.String name, java.lang.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 {@link Attribute} sub classes, and is normally not needed by class generators or adapters.

param
owner the internal name of the field's owner class.
param
name the field's name.
param
desc the field's descriptor.
return
the index of a new or already existing field reference item.

    key3.set(FIELD, owner, name, desc);
    Item result = get(key3);
    if (result == null) {
      put122(FIELD, newClass(owner), newNameType(name, desc));
      result = new Item(index++, key3);
      put(result);
    }
    return result.index;
  
private ItemnewFloat(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.

param
value the float value.
return
a new or already existing float item.

    key.set(value);
    Item result = get(key);
    if (result == null) {
      pool.putByte(FLOAT).putInt(Float.floatToIntBits(value));
      result = new Item(index++, key);
      put(result);
    }
    return result;
  
private ItemnewInteger(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.

param
value the int value.
return
a new or already existing int item.

    key.set(value);
    Item result = get(key);
    if (result == null) {
      pool.putByte(INT).putInt(value);
      result = new Item(index++, key);
      put(result);
    }
    return result;
  
private ItemnewLong(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.

param
value the long value.
return
a new or already existing long item.

    key.set(value);
    Item result = get(key);
    if (result == null) {
      pool.putByte(LONG).putLong(value);
      result = new Item(index, key);
      put(result);
      index += 2;
    }
    return result;
  
public intnewMethod(java.lang.String owner, java.lang.String name, java.lang.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 {@link Attribute} sub classes, and is normally not needed by class generators or adapters.

param
owner the internal name of the method's owner class.
param
name the method's name.
param
desc the method's descriptor.
param
itf true if owner is an interface.
return
the index of a new or already existing method reference item.

    return newMethodItem(owner, name, desc, itf).index;
  
ItemnewMethodItem(java.lang.String owner, java.lang.String name, java.lang.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.

param
owner the internal name of the method's owner class.
param
name the method's name.
param
desc the method's descriptor.
param
itf true if owner is an interface.
return
a new or already existing method reference item.

    key3.set(itf ? IMETH : METH, owner, name, desc);
    Item result = get(key3);
    if (result == null) {
      put122(itf ? IMETH : METH, newClass(owner), newNameType(name, desc));
      result = new Item(index++, key3);
      put(result);
    }
    return result;
  
public intnewNameType(java.lang.String name, java.lang.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 {@link Attribute} sub classes, and is normally not needed by class generators or adapters.

param
name a name.
param
desc a type descriptor.
return
the index of a new or already existing name and type item.

    key2.set(NAME_TYPE, name, desc, null);
    Item result = get(key2);
    if (result == null) {
      put122(NAME_TYPE, newUTF8(name), newUTF8(desc));
      result = new Item(index++, key2);
      put(result);
    }
    return result.index;
  
private ItemnewString(java.lang.String value)
Adds a string to the constant pool of the class being build. Does nothing if the constant pool already contains a similar item.

param
value the String value.
return
a new or already existing string item.

    key2.set(STR, value, null, null);
    Item result = get(key2);
    if (result == null) {
      pool.put12(STR, newUTF8(value));
      result = new Item(index++, key2);
      put(result);
    }
    return result;
  
public intnewUTF8(java.lang.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 {@link Attribute} sub classes, and is normally not needed by class generators or adapters.

param
value the String value.
return
the index of a new or already existing UTF8 item.

    key.set(UTF8, value, null, null);
    Item result = get(key);
    if (result == null) {
      pool.putByte(UTF8).putUTF8(value);
      result = new Item(index++, key);
      put(result);
    }
    return result.index;
  
private voidput(Item i)
Puts the given item in the constant pool's hash table. The hash table must not already contains this item.

param
i the item to be added to the constant pool's hash table.

    if (index > threshold) {
      Item[] newItems = new Item[items.length * 2 + 1];
      for (int l = items.length - 1; l >= 0; --l) {
        Item j = items[l];
        while (j != null) {
          int index = j.hashCode % newItems.length;
          Item k = j.next;
          j.next = newItems[index];
          newItems[index] = j;
          j = k;
        }
      }
      items = newItems;
      threshold = (int)(items.length * 0.75);
    }
    int index = i.hashCode % items.length;
    i.next = items[index];
    items[index] = i;
  
private voidput122(int b, int s1, int s2)
Puts one byte and two shorts into the constant pool.

param
b a byte.
param
s1 a short.
param
s2 another short.

    pool.put12(b, s1).putShort(s2);
  
public byte[]toByteArray()
Returns the bytecode of the class that was build with this class writer.

return
the bytecode of the class that was build with this class writer.

    // computes the real size of the bytecode of this class
    int size = 24 + 2*interfaceCount;
    if (fields != null) {
      size += fields.length;
    }
    int nbMethods = 0;
    CodeWriter cb = firstMethod;
    while (cb != null) {
      ++nbMethods;
      size += cb.getSize();
      cb = cb.next;
    }
    int attributeCount = 0;
    if (sourceFile != 0) {
      ++attributeCount;
      size += 8;
    }
    if ((access & Constants.ACC_DEPRECATED) != 0) {
      ++attributeCount;
      size += 6;
    }
    if ((access & Constants.ACC_SYNTHETIC) != 0) {
      ++attributeCount;
      size += 6;
    }
    if (innerClasses != null) {
      ++attributeCount;
      size += 8 + innerClasses.length;
    }
    if (attrs != null) {
      attributeCount += attrs.getCount();
      size += attrs.getSize(this, null, 0, -1, -1);
    }
    size += pool.length;
    // allocates a byte vector of this size, in order to avoid unnecessary
    // arraycopy operations in the ByteVector.enlarge() method
    ByteVector out = new ByteVector(size);
    out.putInt(0xCAFEBABE).putInt(version);
    out.putShort(index).putByteArray(pool.data, 0, pool.length);
    out.putShort(access).putShort(name).putShort(superName);
    out.putShort(interfaceCount);
    for (int i = 0; i < interfaceCount; ++i) {
      out.putShort(interfaces[i]);
    }
    out.putShort(fieldCount);
    if (fields != null) {
      out.putByteArray(fields.data, 0, fields.length);
    }
    out.putShort(nbMethods);
    cb = firstMethod;
    while (cb != null) {
      cb.put(out);
      cb = cb.next;
    }
    out.putShort(attributeCount);
    if (sourceFile != 0) {
      out.putShort(newUTF8("SourceFile")).putInt(2).putShort(sourceFile);
    }
    if ((access & Constants.ACC_DEPRECATED) != 0) {
      out.putShort(newUTF8("Deprecated")).putInt(0);
    }
    if ((access & Constants.ACC_SYNTHETIC) != 0) {
      out.putShort(newUTF8("Synthetic")).putInt(0);
    }
    if (innerClasses != null) {
      out.putShort(newUTF8("InnerClasses"));
      out.putInt(innerClasses.length + 2).putShort(innerClassesCount);
      out.putByteArray(innerClasses.data, 0, innerClasses.length);
    }
    if (attrs != null) {
      attrs.put(this, null, 0, -1, -1, out);
    }
    return out.data;
  
public voidvisit(int version, int access, java.lang.String name, java.lang.String superName, java.lang.String[] interfaces, java.lang.String sourceFile)

    this.version = version;
    this.access = access;
    this.name = newClass(name);
    this.superName = superName == null ? 0 : newClass(superName);
    if (interfaces != null && interfaces.length > 0) {
      interfaceCount = interfaces.length;
      this.interfaces = new int[interfaceCount];
      for (int i = 0; i < interfaceCount; ++i) {
        this.interfaces[i] = newClass(interfaces[i]);
      }
    }
    if (sourceFile != null) {
      newUTF8("SourceFile");
      this.sourceFile = newUTF8(sourceFile);
    }
    if ((access & Constants.ACC_DEPRECATED) != 0) {
      newUTF8("Deprecated");
    }
    if ((access & Constants.ACC_SYNTHETIC) != 0) {
      newUTF8("Synthetic");
    }
  
public voidvisitAttribute(Attribute attr)

    attr.next = attrs;
    attrs = attr;
  
public voidvisitEnd()

  
public voidvisitField(int access, java.lang.String name, java.lang.String desc, java.lang.Object value, Attribute attrs)

    ++fieldCount;
    if (fields == null) {
      fields = new ByteVector();
    }
    fields.putShort(access).putShort(newUTF8(name)).putShort(newUTF8(desc));
    int attributeCount = 0;
    if (value != null) {
      ++attributeCount;
    }
    if ((access & Constants.ACC_SYNTHETIC) != 0) {
      ++attributeCount;
    }
    if ((access & Constants.ACC_DEPRECATED) != 0) {
      ++attributeCount;
    }
    if (attrs != null) {
      attributeCount += attrs.getCount();
    }
    fields.putShort(attributeCount);
    if (value != null) {
      fields.putShort(newUTF8("ConstantValue"));
      fields.putInt(2).putShort(newConstItem(value).index);
    }
    if ((access & Constants.ACC_SYNTHETIC) != 0) {
      fields.putShort(newUTF8("Synthetic")).putInt(0);
    }
    if ((access & Constants.ACC_DEPRECATED) != 0) {
      fields.putShort(newUTF8("Deprecated")).putInt(0);
    }
    if (attrs != null) {
      attrs.put(this, null, 0, -1, -1, fields);
    }
  
public voidvisitInnerClass(java.lang.String name, java.lang.String outerName, java.lang.String innerName, int access)

    if (innerClasses == null) {
      newUTF8("InnerClasses");
      innerClasses = new ByteVector();
    }
    ++innerClassesCount;
    innerClasses.putShort(name == null ? 0 : newClass(name));
    innerClasses.putShort(outerName == null ? 0 : newClass(outerName));
    innerClasses.putShort(innerName == null ? 0 : newUTF8(innerName));
    innerClasses.putShort(access);
  
public CodeVisitorvisitMethod(int access, java.lang.String name, java.lang.String desc, java.lang.String[] exceptions, Attribute attrs)

    CodeWriter cw = new CodeWriter(this, computeMaxs);
    cw.init(access, name, desc, exceptions, attrs);
    return cw;