Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
ASM: a very small and fast Java bytecode manipulation framework Copyright (c) 2000-2011 INRIA, France Telecom All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. Neither the name of the copyright holders nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  
  package com.sleepycat.asm;

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.

Author(s):
Eric Bruneton
  
  public class ClassWriter extends ClassVisitor {

    
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_MAXS = 1;

    
Flag to automatically compute the stack map frames of methods from scratch. If this flag is set, then the calls to the MethodVisitor.visitFrame(int,int,java.lang.Object[],int,java.lang.Object[]) 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.

  
      public static final int COMPUTE_FRAMES = 2;

    
Pseudo access flag to distinguish between the synthetic attribute and the synthetic access flag.
  
      static final int ACC_SYNTHETIC_ATTRIBUTE = 0x40000;

    
The type of instructions without any argument.
  
      static final int NOARG_INSN = 0;

    
The type of instructions with an signed byte argument.
  
      static final int SBYTE_INSN = 1;

    
The type of instructions with an signed short argument.
  
      static final int SHORT_INSN = 2;

    
The type of instructions with a local variable index argument.
  
      static final int VAR_INSN = 3;

    
The type of instructions with an implicit local variable index argument.
  
      static final int IMPLVAR_INSN = 4;

    
The type of instructions with a type descriptor argument.
 
     static final int TYPE_INSN = 5;

    
The type of field and method invocations instructions.
 
     static final int FIELDORMETH_INSN = 6;

    
The type of the INVOKEINTERFACE/INVOKEDYNAMIC instruction.
 
     static final int ITFMETH_INSN = 7;

    
The type of the INVOKEDYNAMIC instruction.
 
     static final int INDYMETH_INSN = 8;

    
The type of instructions with a 2 bytes bytecode offset label.
 
     static final int LABEL_INSN = 9;

    
The type of instructions with a 4 bytes bytecode offset label.
 
     static final int LABELW_INSN = 10;

    
The type of the LDC instruction.
 
     static final int LDC_INSN = 11;

    
The type of the LDC_W and LDC2_W instructions.
 
     static final int LDCW_INSN = 12;

    
The type of the IINC instruction.
 
     static final int IINC_INSN = 13;

    
The type of the TABLESWITCH instruction.
 
     static final int TABL_INSN = 14;

    
The type of the LOOKUPSWITCH instruction.
 
     static final int LOOK_INSN = 15;

    
The type of the MULTIANEWARRAY instruction.
 
     static final int MANA_INSN = 16;

    
The type of the WIDE instruction.
 
     static final int WIDE_INSN = 17;

    
The instruction types of all JVM opcodes.
 
     static final byte[] TYPE;

    
The type of CONSTANT_Class constant pool items.
 
     static final int CLASS = 7;

    
The type of CONSTANT_Fieldref constant pool items.
 
     static final int FIELD = 9;

    
The type of CONSTANT_Methodref constant pool items.
 
     static final int METH = 10;

    
The type of CONSTANT_InterfaceMethodref constant pool items.
 
     static final int IMETH = 11;

    
The type of CONSTANT_String constant pool items.
 
     static final int STR = 8;

    
The type of CONSTANT_Integer constant pool items.
 
     static final int INT = 3;

    
The type of CONSTANT_Float constant pool items.
 
     static final int FLOAT = 4;

    
The type of CONSTANT_Long constant pool items.
 
     static final int LONG = 5;

    
The type of CONSTANT_Double constant pool items.
 
     static final int DOUBLE = 6;

    
The type of CONSTANT_NameAndType constant pool items.
 
     static final int NAME_TYPE = 12;

    
The type of CONSTANT_Utf8 constant pool items.
 
     static final int UTF8 = 1;

    
The type of CONSTANT_MethodType constant pool items.
 
     static final int MTYPE = 16;

    
The type of CONSTANT_MethodHandle constant pool items.
 
     static final int HANDLE = 15;

    
The type of CONSTANT_InvokeDynamic constant pool items.
 
     static final int INDY = 18;

    
The base value for all CONSTANT_MethodHandle constant pool items. Internally, ASM store the 9 variations of CONSTANT_MethodHandle into 9 different items.
 
     static final int HANDLE_BASE = 20;

    
Normal type Item stored in the 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_NORMAL = 30;

    
Uninitialized type Item stored in the 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 = 31;

    
Merged type Item stored in the 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 = 32;

    
The type of BootstrapMethods items. These items are stored in a special class attribute named BootstrapMethods and not in the constant pool.
 
     static final int BSM = 33;

    
The class reader from which this class writer was constructed, if any.
 
     ClassReader cr;

    
Minor and major version numbers of the class to be generated.
 
     int version;

    
Index of the next item to be added in the constant pool.
 
     int index;

    
The constant pool of this class.
 
     final ByteVector pool;

    
The constant pool's hash table data.
 
     Item[] items;

    
The threshold of the constant pool's hash table.
 
     int threshold;

    
A reusable key used to look for items in the items 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.
 
     final Item key4;

    
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.
 
     Item[] typeTable;

    
Number of elements in the typeTable array.
 
     private short typeCount;

    
The access flags of this class.
 
     private int access;

    
The constant pool item that contains the internal name of this class.
 
     private int name;

    
The internal name of this class.
 
     String thisName;

    
The constant pool item that contains the signature of this class.
 
     private int signature;

    
The constant pool item that contains the internal name of the super class of this class.
 
     private int superName;

    
Number of interfaces implemented or extended by this class or interface.
 
     private int interfaceCount;

    
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[] interfaces;

    
The index of the constant pool item that contains the name of the source file from which this class was compiled.
 
     private int sourceFile;

    
The SourceDebug attribute of this class.
 
     private ByteVector sourceDebug;

    
The constant pool item that contains the name of the enclosing class of this class.
 
     private int enclosingMethodOwner;

    
The constant pool item that contains the name and descriptor of the enclosing method of this class.
 
     private int enclosingMethod;

    
The runtime visible annotations of this class.
 
     private AnnotationWriter anns;

    
The runtime invisible annotations of this class.
 
     private AnnotationWriter ianns;

    
The non standard attributes of this class.
 
     private Attribute attrs;

    
The number of entries in the InnerClasses attribute.
 
     private int innerClassesCount;

    
The InnerClasses attribute.
 
     private ByteVector innerClasses;

    
The number of entries in the BootstrapMethods attribute.
 
     int bootstrapMethodsCount;

    
The BootstrapMethods attribute.
 
     ByteVector bootstrapMethods;

    
The fields of this class. These fields are stored in a linked list of FieldWriter objects, linked to each other by their FieldVisitor.fv field. This field stores the first element of this list.
 
     FieldWriter firstField;

    
The fields of this class. These fields are stored in a linked list of FieldWriter objects, linked to each other by their FieldVisitor.fv field. This field stores the last element of this list.
 
     FieldWriter lastField;

    
The methods of this class. These methods are stored in a linked list of MethodWriter objects, linked to each other by their MethodVisitor.mv field. This field stores the first 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 MethodVisitor.mv field. This field stores the last element of this list.
 
     MethodWriter lastMethod;

    
true if the maximum stack size and number of local variables must be automatically computed.
 
     private final boolean computeMaxs;

    
true if the stack map frames must be recomputed from scratch.
 
     private final boolean computeFrames;

    
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).
 
     boolean invalidFrames;
 
     // ------------------------------------------------------------------------
     // Static initializer
     // ------------------------------------------------------------------------
 
    
Computes the instruction types of JVM opcodes.
 
     static {
         int i;
         byte[] b = new byte[220];
         String s = "AAAAAAAAAAAAAAAABCLMMDDDDDEEEEEEEEEEEEEEEEEEEEAAAAAAAADD"
                 + "DDDEEEEEEEEEEEEEEEEEEEEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
                 + "AAAAAAAAAAAAAAAAANAAAAAAAAAAAAAAAAAAAAJJJJJJJJJJJJJJJJDOPAA"
                 + "AAAAGGGGGGGHIFBFAAFFAARQJJKKJJJJJJJJJJJJJJJJJJ";
         for (i = 0; i < b.length; ++i) {
             b[i] = (byte) (s.charAt(i) - 'A');
         }
          = 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;
         // b[Constants.INVOKEDYNAMIC] = INDYMETH_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
         // MethodWriter
         // 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();
     }
 
     // ------------------------------------------------------------------------
     // Constructor
     // ------------------------------------------------------------------------
 
    
Constructs a new ClassWriter object.

Parameters:
flags option flags that can be used to modify the default behavior of this class. See COMPUTE_MAXS, COMPUTE_FRAMES.
 
     public ClassWriter(final int flags) {
         super(.);
          = 1;
          = new ByteVector();
          = new Item[256];
          = (int) (0.75d * .);
          = new Item();
          = new Item();
          = new Item();
          = new Item();
         this. = (flags & ) != 0;
         this. = (flags & ) != 0;
     }

    
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 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. These option flags do not affect methods that are copied as is in the new class. This means that the maximum stack size nor the stack frames will be computed for these methods. See COMPUTE_MAXS, COMPUTE_FRAMES.
 
     public ClassWriter(final ClassReader classReaderfinal int flags) {
         this(flags);
         classReader.copyPool(this);
         this. = classReader;
     }
 
     // ------------------------------------------------------------------------
     // Implementation of the ClassVisitor abstract class
     // ------------------------------------------------------------------------
 
     @Override
     public final void visit(
         final int version,
         final int access,
         final String name,
         final String signature,
         final String superName,
         final String[] interfaces)
     {
         this. = version;
         this. = access;
         this. = newClass(name);
          = name;
         if (. && signature != null) {
             this. = newUTF8(signature);
         }
         this. = superName == null ? 0 : newClass(superName);
         if (interfaces != null && interfaces.length > 0) {
              = interfaces.length;
             this. = new int[];
             for (int i = 0; i < ; ++i) {
                 this.[i] = newClass(interfaces[i]);
             }
         }
     }
 
     @Override
     public final void visitSource(final String filefinal String debug) {
         if (file != null) {
              = newUTF8(file);
         }
         if (debug != null) {
              = new ByteVector().putUTF8(debug);
         }
     }
 
     @Override
     public final void visitOuterClass(
         final String owner,
         final String name,
         final String desc)
     {
          = newClass(owner);
         if (name != null && desc != null) {
              = newNameType(namedesc);
         }
     }
 
     @Override
     public final AnnotationVisitor visitAnnotation(
         final String desc,
         final boolean visible)
     {
         if (!.) {
             return null;
         }
         ByteVector bv = new ByteVector();
         // write type, and reserve space for values count
         bv.putShort(newUTF8(desc)).putShort(0);
         AnnotationWriter aw = new AnnotationWriter(thistruebvbv, 2);
         if (visible) {
             aw.next = ;
              = aw;
         } else {
             aw.next = ;
              = aw;
         }
         return aw;
     }
 
     @Override
     public final void visitAttribute(final Attribute attr) {
         attr.next = ;
          = attr;
     }
 
     @Override
     public final void visitInnerClass(
         final String name,
         final String outerName,
         final String innerName,
         final int access)
     {
         if ( == null) {
              = new ByteVector();
         }
         ++;
         .putShort(name == null ? 0 : newClass(name));
         .putShort(outerName == null ? 0 : newClass(outerName));
         .putShort(innerName == null ? 0 : newUTF8(innerName));
         .putShort(access);
     }
 
     @Override
     public final FieldVisitor visitField(
         final int access,
         final String name,
         final String desc,
         final String signature,
         final Object value)
     {
         return new FieldWriter(thisaccessnamedescsignaturevalue);
     }
 
     @Override
     public final MethodVisitor visitMethod(
         final int access,
         final String name,
         final String desc,
         final String signature,
         final String[] exceptions)
     {
         return new MethodWriter(this,
                 access,
                 name,
                 desc,
                 signature,
                 exceptions,
                 ,
                 );
     }
 
     @Override
     public final void visitEnd() {
     }
 
     // ------------------------------------------------------------------------
     // Other public methods
     // ------------------------------------------------------------------------
 
    
Returns the bytecode of the class that was build with this class writer.

Returns:
the bytecode of the class that was build with this class writer.
 
     public byte[] toByteArray() {
         if ( > .) {
             throw new RuntimeException("Class file too large!");
         }
         // computes the real size of the bytecode of this class
         int size = 24 + 2 * ;
         int nbFields = 0;
         FieldWriter fb = ;
         while (fb != null) {
             ++nbFields;
             size += fb.getSize();
             fb = (FieldWriterfb.fv;
         }
         int nbMethods = 0;
         MethodWriter mb = ;
         while (mb != null) {
             ++nbMethods;
             size += mb.getSize();
             mb = (MethodWritermb.mv;
         }
         int attributeCount = 0;
         if ( != null) {  // we put it as first argument in order
                                          // to improve a bit ClassReader.copyBootstrapMethods
             ++attributeCount;
             size += 8 + .;
             newUTF8("BootstrapMethods");
         }
         if (. &&  != 0) {
             ++attributeCount;
             size += 8;
             newUTF8("Signature");
         }
         if ( != 0) {
             ++attributeCount;
             size += 8;
             newUTF8("SourceFile");
         }
         if ( != null) {
             ++attributeCount;
             size += . + 4;
             newUTF8("SourceDebugExtension");
         }
         if ( != 0) {
             ++attributeCount;
             size += 10;
             newUTF8("EnclosingMethod");
         }
         if (( & .) != 0) {
             ++attributeCount;
             size += 6;
             newUTF8("Deprecated");
         }
         if (( & .) != 0
                 && (( & 0xFFFF) < . || ( & ) != 0))
         {
             ++attributeCount;
             size += 6;
             newUTF8("Synthetic");
         }
         if ( != null) {
             ++attributeCount;
             size += 8 + .;
             newUTF8("InnerClasses");
         }
         if (. &&  != null) {
             ++attributeCount;
             size += 8 + .getSize();
             newUTF8("RuntimeVisibleAnnotations");
         }
         if (. &&  != null) {
             ++attributeCount;
             size += 8 + .getSize();
             newUTF8("RuntimeInvisibleAnnotations");
         }
         if ( != null) {
             attributeCount += .getCount();
             size += .getSize(thisnull, 0, -1, -1);
         }
         size += .;
         // 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();
         out.putShort().putByteArray(., 0, .);
         int mask = .
                 | .
         out.putShort( & ~mask).putShort().putShort();
         out.putShort();
         for (int i = 0; i < ; ++i) {
             out.putShort([i]);
         }
         out.putShort(nbFields);
         fb = ;
         while (fb != null) {
             fb.put(out);
             fb = (FieldWriterfb.fv;
         }
         out.putShort(nbMethods);
         mb = ;
         while (mb != null) {
             mb.put(out);
             mb = (MethodWritermb.mv;
         }
         out.putShort(attributeCount);
         if ( != null) {   // should be the first class attribute ?
             out.putShort(newUTF8("BootstrapMethods"));
             out.putInt(. + 2).putShort();
             out.putByteArray(., 0, .);
         }
         if (. &&  != 0) {
             out.putShort(newUTF8("Signature")).putInt(2).putShort();
         }
         if ( != 0) {
             out.putShort(newUTF8("SourceFile")).putInt(2).putShort();
         }
         if ( != null) {
             int len = . - 2;
             out.putShort(newUTF8("SourceDebugExtension")).putInt(len);
             out.putByteArray(., 2, len);
         }
         if ( != 0) {
             out.putShort(newUTF8("EnclosingMethod")).putInt(4);
             out.putShort().putShort();
         }
         if (( & .) != 0) {
             out.putShort(newUTF8("Deprecated")).putInt(0);
         }
         if (( & .) != 0
                 && (( & 0xFFFF) < . || ( & ) != 0))
         {
             out.putShort(newUTF8("Synthetic")).putInt(0);
         }
         if ( != null) {
             out.putShort(newUTF8("InnerClasses"));
             out.putInt(. + 2).putShort();
             out.putByteArray(., 0, .);
         }
         if (. &&  != null) {
             out.putShort(newUTF8("RuntimeVisibleAnnotations"));
             .put(out);
         }
         if (. &&  != null) {
             out.putShort(newUTF8("RuntimeInvisibleAnnotations"));
             .put(out);
         }
         if ( != null) {
             .put(thisnull, 0, -1, -1, out);
         }
         if () {
             ClassWriter cw = new ClassWriter();
             new ClassReader(out.data).accept(cw.);
             return cw.toByteArray();
         }
         return out.data;
     }
 
     // ------------------------------------------------------------------------
     // Utility methods: constant pool management
     // ------------------------------------------------------------------------
 
    
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.

Parameters:
cst the value of the constant to be added to the constant pool. This parameter must be an java.lang.Integer, a java.lang.Float, a java.lang.Long, a java.lang.Double, a java.lang.String or a Type.
Returns:
a new or already existing constant item with the given value.
 
     Item newConstItem(final Object cst) {
         if (cst instanceof Integer) {
             int val = ((Integercst).intValue();
             return newInteger(val);
         } else if (cst instanceof Byte) {
             int val = ((Bytecst).intValue();
             return newInteger(val);
         } else if (cst instanceof Character) {
             int val = ((Charactercst).charValue();
             return newInteger(val);
         } else if (cst instanceof Short) {
             int val = ((Shortcst).intValue();
             return newInteger(val);
         } else if (cst instanceof Boolean) {
             int val = ((Booleancst).booleanValue() ? 1 : 0;
             return newInteger(val);
         } else if (cst instanceof Float) {
             float val = ((Floatcst).floatValue();
             return newFloat(val);
         } else if (cst instanceof Long) {
             long val = ((Longcst).longValue();
             return newLong(val);
         } else if (cst instanceof Double) {
             double val = ((Doublecst).doubleValue();
             return newDouble(val);
         } else if (cst instanceof String) {
             return newString((Stringcst);
         } else if (cst instanceof Type) {
             Type t = (Typecst;
             int s = t.getSort();
             if (s == .) {
                 return newClassItem(t.getDescriptor());
             } else if (s == .) {
                 return newClassItem(t.getInternalName());
             } else { // s == Type.METHOD
                 return newMethodTypeItem(t.getDescriptor());
             }
         } else if (cst instanceof Handle) {
             Handle h = (Handlecst;
             return newHandleItem(h.tagh.ownerh.nameh.desc);
         } else {
             throw new IllegalArgumentException("value " + 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.

Parameters:
cst the value of the constant to be added to the constant pool. This parameter must be an java.lang.Integer, a java.lang.Float, a java.lang.Long, a java.lang.Double or a java.lang.String.
Returns:
the index of a new or already existing constant item with the given value.
    public int newConst(final Object cst) {
        return newConstItem(cst).;
    }

    
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.

Parameters:
value the String value.
Returns:
the index of a new or already existing UTF8 item.
    public int newUTF8(final String value) {
        .set(valuenullnull);
        Item result = get();
        if (result == null) {
            .putByte().putUTF8(value);
            result = new Item(++, );
            put(result);
        }
        return result.index;
    }

    
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.

Parameters:
value the internal name of the class.
Returns:
a new or already existing class reference item.
    Item newClassItem(final String value) {
        .set(valuenullnull);
        Item result = get();
        if (result == null) {
            .put12(newUTF8(value));
            result = new Item(++, );
            put(result);
        }
        return result;
    }

    
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.

Parameters:
value the internal name of the class.
Returns:
the index of a new or already existing class reference item.
    public int newClass(final String value) {
        return newClassItem(value).;
    }

    
Adds a method type 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.

Parameters:
methodDesc method descriptor of the method type.
Returns:
a new or already existing method type reference item.
    Item newMethodTypeItem(final String methodDesc) {
        .set(methodDescnullnull);
        Item result = get();
        if (result == null) {
            .put12(newUTF8(methodDesc));
            result = new Item(++, );
            put(result);
        }
        return result;
    }

    
Adds a method type 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.

Parameters:
methodDesc method descriptor of the method type.
Returns:
the index of a new or already existing method type reference item.
    public int newMethodType(final String methodDesc) {
        return newMethodTypeItem(methodDesc).;
    }

    
Adds a handle 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.

Parameters:
tag the kind of this handle. Must be Opcodes.H_GETFIELD, Opcodes.H_GETSTATIC, Opcodes.H_PUTFIELD, Opcodes.H_PUTSTATIC, Opcodes.H_INVOKEVIRTUAL, Opcodes.H_INVOKESTATIC, Opcodes.H_INVOKESPECIAL, Opcodes.H_NEWINVOKESPECIAL or Opcodes.H_INVOKEINTERFACE.
owner the internal name of the field or method owner class.
name the name of the field or method.
desc the descriptor of the field or method.
Returns:
a new or an already existing method type reference item.
        final int tag,
        final String owner,
        final String name,
        final String desc)
    {
        .set( + tagownernamedesc);
        Item result = get();
        if (result == null) {
            if (tag <= .) {
                put112(tagnewField(ownernamedesc));
            } else {
                put112(tagnewMethod(owner,
                        name,
                        desc,
                        tag == .));
            }
            result = new Item(++, );
            put(result);
        }
        return result;
    }

    
Adds a handle 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.

Parameters:
tag the kind of this handle. Must be Opcodes.H_GETFIELD, Opcodes.H_GETSTATIC, Opcodes.H_PUTFIELD, Opcodes.H_PUTSTATIC, Opcodes.H_INVOKEVIRTUAL, Opcodes.H_INVOKESTATIC, Opcodes.H_INVOKESPECIAL, Opcodes.H_NEWINVOKESPECIAL or Opcodes.H_INVOKEINTERFACE.
owner the internal name of the field or method owner class.
name the name of the field or method.
desc the descriptor of the field or method.
Returns:
the index of a new or already existing method type reference item.
    public int newHandle(
        final int tag,
        final String owner,
        final String name,
        final String desc)
    {
        return newHandleItem(tagownernamedesc).;
    }

    
Adds an invokedynamic 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.

Parameters:
name name of the invoked method.
desc descriptor of the invoke method.
bsm the bootstrap method.
bsmArgs the bootstrap method constant arguments.
Returns:
a new or an already existing invokedynamic type reference item.
        final String name,
        final String desc,
        final Handle bsm,
        final Object... bsmArgs)
    {
        // cache for performance
        ByteVector bootstrapMethods = this.;
        if (bootstrapMethods == null) {
            bootstrapMethods = this. = new ByteVector();
        }
        int position = bootstrapMethods.length// record current position
        int hashCode = bsm.hashCode();
        bootstrapMethods.putShort(newHandle(bsm.tag,
                bsm.owner,
                bsm.name,
                bsm.desc));
        int argsLength = bsmArgs.length;
        bootstrapMethods.putShort(argsLength);
        for (int i = 0; i < argsLengthi++) {
            Object bsmArg = bsmArgs[i];
            hashCode ^= bsmArg.hashCode();
            bootstrapMethods.putShort(newConst(bsmArg));
        }
        byte[] data = bootstrapMethods.data;
        int length = (1 + 1 + argsLength) << 1; // (bsm + argCount + arguments)
        hashCode &= 0x7FFFFFFF;
        Item result = [hashCode % .];
        loop: while (result != null) {
            if (result.type !=  || result.hashCode != hashCode) {
                result = result.next;
                continue;
            }
            // because the data encode the size of the argument
            // we don't need to test if these size are equals
            int resultPosition = result.intVal;
            for (int p = 0; p < lengthp++) {
                if (data[position + p] != data[resultPosition + p]) {
                    result = result.next;
                    continue loop;
                }
            }
            break;
        }
        int bootstrapMethodIndex;
        if (result != null) {
            bootstrapMethodIndex = result.index;
            bootstrapMethods.length = position// revert to old position
        } else {
            bootstrapMethodIndex = ++;
            result = new Item(bootstrapMethodIndex);
            result.set(positionhashCode);
            put(result);
        }
        // now, create the InvokeDynamic constant
        .set(namedescbootstrapMethodIndex);
        result = get();
        if (result == null) {
            put122(bootstrapMethodIndexnewNameType(namedesc));
            result = new Item(++, );
            put(result);
        }
        return result;
    }

    
Adds an invokedynamic 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.

Parameters:
name name of the invoked method.
desc descriptor of the invoke method.
bsm the bootstrap method.
bsmArgs the bootstrap method constant arguments.
Returns:
the index of a new or already existing invokedynamic reference item.
    public int newInvokeDynamic(
        final String name,
        final String desc,
        final Handle bsm,
        final Object... bsmArgs)
    {
        return newInvokeDynamicItem(namedescbsmbsmArgs).;
    }

    
Adds a field reference to the constant pool of the class being build. Does nothing if the constant pool already contains a similar item.

Parameters:
owner the internal name of the field's owner class.
name the field's name.
desc the field's descriptor.
Returns:
a new or already existing field reference item.
    Item newFieldItem(final String ownerfinal String namefinal String desc)
    {
        .set(ownernamedesc);
        Item result = get();
        if (result == null) {
            put122(newClass(owner), newNameType(namedesc));
            result = new Item(++, );
            put(result);
        }
        return result;
    }

    
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.

Parameters:
owner the internal name of the field's owner class.
name the field's name.
desc the field's descriptor.
Returns:
the index of a new or already existing field reference item.
    public int newField(final String ownerfinal String namefinal String desc)
    {
        return newFieldItem(ownernamedesc).;
    }

    
Adds a method reference to the constant pool of the class being build. Does nothing if the constant pool already contains a similar item.

Parameters:
owner the internal name of the method's owner class.
name the method's name.
desc the method's descriptor.
itf true if owner is an interface.
Returns:
a new or already existing method reference item.
        final String owner,
        final String name,
        final String desc,
        final boolean itf)
    {
        int type = itf ?  : ;
        .set(typeownernamedesc);
        Item result = get();
        if (result == null) {
            put122(typenewClass(owner), newNameType(namedesc));
            result = new Item(++, );
            put(result);
        }
        return result;
    }

    
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.

Parameters:
owner the internal name of the method's owner class.
name the method's name.
desc the method's descriptor.
itf true if owner is an interface.
Returns:
the index of a new or already existing method reference item.
    public int newMethod(
        final String owner,
        final String name,
        final String desc,
        final boolean itf)
    {
        return newMethodItem(ownernamedescitf).;
    }

    
Adds an integer to the constant pool of the class being build. Does nothing if the constant pool already contains a similar item.

Parameters:
value the int value.
Returns:
a new or already existing int item.
    Item newInteger(final int value) {
        .set(value);
        Item result = get();
        if (result == null) {
            .putByte().putInt(value);
            result = new Item(++, );
            put(result);
        }
        return result;
    }

    
Adds a float to the constant pool of the class being build. Does nothing if the constant pool already contains a similar item.

Parameters:
value the float value.
Returns:
a new or already existing float item.
    Item newFloat(final float value) {
        .set(value);
        Item result = get();
        if (result == null) {
            .putByte().putInt(.);
            result = new Item(++, );
            put(result);
        }
        return result;
    }

    
Adds a long to the constant pool of the class being build. Does nothing if the constant pool already contains a similar item.

Parameters:
value the long value.
Returns:
a new or already existing long item.
    Item newLong(final long value) {
        .set(value);
        Item result = get();
        if (result == null) {
            .putByte().putLong(value);
            result = new Item();
             += 2;
            put(result);
        }
        return result;
    }

    
Adds a double to the constant pool of the class being build. Does nothing if the constant pool already contains a similar item.

Parameters:
value the double value.
Returns:
a new or already existing double item.
    Item newDouble(final double value) {
        .set(value);
        Item result = get();
        if (result == null) {
            .putByte().putLong(.);
            result = new Item();
             += 2;
            put(result);
        }
        return result;
    }

    
Adds a string to the constant pool of the class being build. Does nothing if the constant pool already contains a similar item.

Parameters:
value the String value.
Returns:
a new or already existing string item.
    private Item newString(final String value) {
        .set(valuenullnull);
        Item result = get();
        if (result == null) {
            .put12(newUTF8(value));
            result = new Item(++, );
            put(result);
        }
        return result;
    }

    
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.

Parameters:
name a name.
desc a type descriptor.
Returns:
the index of a new or already existing name and type item.
    public int newNameType(final String namefinal String desc) {
        return newNameTypeItem(namedesc).;
    }

    
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.

Parameters:
name a name.
desc a type descriptor.
Returns:
a new or already existing name and type item.
    Item newNameTypeItem(final String namefinal String desc) {
        .set(namedescnull);
        Item result = get();
        if (result == null) {
            put122(newUTF8(name), newUTF8(desc));
            result = new Item(++, );
            put(result);
        }
        return result;
    }

    
Adds the given internal name to typeTable and returns its index. Does nothing if the type table already contains this internal name.

Parameters:
type the internal name to be added to the type table.
Returns:
the index of this internal name in the type table.
    int addType(final String type) {
        .set(typenullnull);
        Item result = get();
        if (result == null) {
            result = addType();
        }
        return result.index;
    }

    
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.

Parameters:
type the internal name to be added to the type table.
offset the bytecode offset of the NEW instruction that created this UNINITIALIZED type value.
Returns:
the index of this internal name in the type table.
    int addUninitializedType(final String typefinal int offset) {
        . = ;
        . = offset;
        . = type;
        . = 0x7FFFFFFF & ( + type.hashCode() + offset);
        Item result = get();
        if (result == null) {
            result = addType();
        }
        return result.index;
    }

    
Adds the given Item to typeTable.

Parameters:
item the value to be added to the type table.
Returns:
the added Item, which a new Item instance with the same value as the given Item.
    private Item addType(final Item item) {
        ++;
        Item result = new Item();
        put(result);
        if ( == null) {
             = new Item[16];
        }
        if ( == .) {
            Item[] newTable = new Item[2 * .];
            System.arraycopy(, 0, newTable, 0, .);
             = newTable;
        }
        [] = result;
        return result;
    }

    
Returns the index of the common super type of the two given types. This method calls getCommonSuperClass(java.lang.String,java.lang.String) and caches the result in the items hash table to speedup future calls with the same parameters.

Parameters:
type1 index of an internal name in typeTable.
type2 index of an internal name in typeTable.
Returns:
the index of the common super type of the two given types.
    int getMergedType(final int type1final int type2) {
        . = ;
        . = type1 | (((longtype2) << 32);
        . = 0x7FFFFFFF & ( + type1 + type2);
        Item result = get();
        if (result == null) {
            String t = [type1].;
            String u = [type2].;
            . = addType(getCommonSuperClass(tu));
            result = new Item((short) 0, );
            put(result);
        }
        return result.intVal;
    }

    
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.

Parameters:
type1 the internal name of a class.
type2 the internal name of another class.
Returns:
the internal name of the common super class of the two given classes.
    protected String getCommonSuperClass(final String type1final String type2)
    {
        Class<?> cd;
        ClassLoader classLoader = getClass().getClassLoader();
        try {
            c = Class.forName(type1.replace('/''.'), falseclassLoader);
            d = Class.forName(type2.replace('/''.'), falseclassLoader);
        } catch (Exception e) {
            throw new RuntimeException(e.toString());
        }
        if (c.isAssignableFrom(d)) {
            return type1;
        }
        if (d.isAssignableFrom(c)) {
            return type2;
        }
        if (c.isInterface() || d.isInterface()) {
            return "java/lang/Object";
        } else {
            do {
                c = c.getSuperclass();
            } while (!c.isAssignableFrom(d));