0001: /***
0002: * ASM: a very small and fast Java bytecode manipulation framework
0003: * Copyright (c) 2000-2005 INRIA, France Telecom
0004: * All rights reserved.
0005: *
0006: * Redistribution and use in source and binary forms, with or without
0007: * modification, are permitted provided that the following conditions
0008: * are met:
0009: * 1. Redistributions of source code must retain the above copyright
0010: * notice, this list of conditions and the following disclaimer.
0011: * 2. Redistributions in binary form must reproduce the above copyright
0012: * notice, this list of conditions and the following disclaimer in the
0013: * documentation and/or other materials provided with the distribution.
0014: * 3. Neither the name of the copyright holders nor the names of its
0015: * contributors may be used to endorse or promote products derived from
0016: * this software without specific prior written permission.
0017: *
0018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0021: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0022: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0023: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0024: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0025: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0026: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0027: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
0028: * THE POSSIBILITY OF SUCH DAMAGE.
0029: */package org.drools.asm;
0030:
0031: /**
0032: * A {@link ClassVisitor} that generates classes in bytecode form. More
0033: * precisely this visitor generates a byte array conforming to the Java class
0034: * file format. It can be used alone, to generate a Java class "from scratch",
0035: * or with one or more {@link ClassReader ClassReader} and adapter class visitor
0036: * to generate a modified class from one or more existing Java classes.
0037: *
0038: * @author Eric Bruneton
0039: */
0040: public class ClassWriter implements ClassVisitor {
0041:
0042: /**
0043: * The type of instructions without any argument.
0044: */
0045: final static int NOARG_INSN = 0;
0046:
0047: /**
0048: * The type of instructions with an signed byte argument.
0049: */
0050: final static int SBYTE_INSN = 1;
0051:
0052: /**
0053: * The type of instructions with an signed short argument.
0054: */
0055: final static int SHORT_INSN = 2;
0056:
0057: /**
0058: * The type of instructions with a local variable index argument.
0059: */
0060: final static int VAR_INSN = 3;
0061:
0062: /**
0063: * The type of instructions with an implicit local variable index argument.
0064: */
0065: final static int IMPLVAR_INSN = 4;
0066:
0067: /**
0068: * The type of instructions with a type descriptor argument.
0069: */
0070: final static int TYPE_INSN = 5;
0071:
0072: /**
0073: * The type of field and method invocations instructions.
0074: */
0075: final static int FIELDORMETH_INSN = 6;
0076:
0077: /**
0078: * The type of the INVOKEINTERFACE instruction.
0079: */
0080: final static int ITFMETH_INSN = 7;
0081:
0082: /**
0083: * The type of instructions with a 2 bytes bytecode offset label.
0084: */
0085: final static int LABEL_INSN = 8;
0086:
0087: /**
0088: * The type of instructions with a 4 bytes bytecode offset label.
0089: */
0090: final static int LABELW_INSN = 9;
0091:
0092: /**
0093: * The type of the LDC instruction.
0094: */
0095: final static int LDC_INSN = 10;
0096:
0097: /**
0098: * The type of the LDC_W and LDC2_W instructions.
0099: */
0100: final static int LDCW_INSN = 11;
0101:
0102: /**
0103: * The type of the IINC instruction.
0104: */
0105: final static int IINC_INSN = 12;
0106:
0107: /**
0108: * The type of the TABLESWITCH instruction.
0109: */
0110: final static int TABL_INSN = 13;
0111:
0112: /**
0113: * The type of the LOOKUPSWITCH instruction.
0114: */
0115: final static int LOOK_INSN = 14;
0116:
0117: /**
0118: * The type of the MULTIANEWARRAY instruction.
0119: */
0120: final static int MANA_INSN = 15;
0121:
0122: /**
0123: * The type of the WIDE instruction.
0124: */
0125: final static int WIDE_INSN = 16;
0126:
0127: /**
0128: * The instruction types of all JVM opcodes.
0129: */
0130: static byte[] TYPE;
0131:
0132: /**
0133: * The type of CONSTANT_Class constant pool items.
0134: */
0135: final static int CLASS = 7;
0136:
0137: /**
0138: * The type of CONSTANT_Fieldref constant pool items.
0139: */
0140: final static int FIELD = 9;
0141:
0142: /**
0143: * The type of CONSTANT_Methodref constant pool items.
0144: */
0145: final static int METH = 10;
0146:
0147: /**
0148: * The type of CONSTANT_InterfaceMethodref constant pool items.
0149: */
0150: final static int IMETH = 11;
0151:
0152: /**
0153: * The type of CONSTANT_String constant pool items.
0154: */
0155: final static int STR = 8;
0156:
0157: /**
0158: * The type of CONSTANT_Integer constant pool items.
0159: */
0160: final static int INT = 3;
0161:
0162: /**
0163: * The type of CONSTANT_Float constant pool items.
0164: */
0165: final static int FLOAT = 4;
0166:
0167: /**
0168: * The type of CONSTANT_Long constant pool items.
0169: */
0170: final static int LONG = 5;
0171:
0172: /**
0173: * The type of CONSTANT_Double constant pool items.
0174: */
0175: final static int DOUBLE = 6;
0176:
0177: /**
0178: * The type of CONSTANT_NameAndType constant pool items.
0179: */
0180: final static int NAME_TYPE = 12;
0181:
0182: /**
0183: * The type of CONSTANT_Utf8 constant pool items.
0184: */
0185: final static int UTF8 = 1;
0186:
0187: /**
0188: * The class reader from which this class writer was constructed, if any.
0189: */
0190: ClassReader cr;
0191:
0192: /**
0193: * Minor and major version numbers of the class to be generated.
0194: */
0195: int version;
0196:
0197: /**
0198: * Index of the next item to be added in the constant pool.
0199: */
0200: int index;
0201:
0202: /**
0203: * The constant pool of this class.
0204: */
0205: ByteVector pool;
0206:
0207: /**
0208: * The constant pool's hash table data.
0209: */
0210: Item[] items;
0211:
0212: /**
0213: * The threshold of the constant pool's hash table.
0214: */
0215: int threshold;
0216:
0217: /**
0218: * A reusable key used to look for items in the hash {@link #items items}.
0219: */
0220: Item key;
0221:
0222: /**
0223: * A reusable key used to look for items in the hash {@link #items items}.
0224: */
0225: Item key2;
0226:
0227: /**
0228: * A reusable key used to look for items in the hash {@link #items items}.
0229: */
0230: Item key3;
0231:
0232: /**
0233: * The access flags of this class.
0234: */
0235: private int access;
0236:
0237: /**
0238: * The constant pool item that contains the internal name of this class.
0239: */
0240: private int name;
0241:
0242: /**
0243: * The constant pool item that contains the signature of this class.
0244: */
0245: private int signature;
0246:
0247: /**
0248: * The constant pool item that contains the internal name of the super class
0249: * of this class.
0250: */
0251: private int super Name;
0252:
0253: /**
0254: * Number of interfaces implemented or extended by this class or interface.
0255: */
0256: private int interfaceCount;
0257:
0258: /**
0259: * The interfaces implemented or extended by this class or interface. More
0260: * precisely, this array contains the indexes of the constant pool items
0261: * that contain the internal names of these interfaces.
0262: */
0263: private int[] interfaces;
0264:
0265: /**
0266: * The index of the constant pool item that contains the name of the source
0267: * file from which this class was compiled.
0268: */
0269: private int sourceFile;
0270:
0271: /**
0272: * The SourceDebug attribute of this class.
0273: */
0274: private ByteVector sourceDebug;
0275:
0276: /**
0277: * The constant pool item that contains the name of the enclosing class of
0278: * this class.
0279: */
0280: private int enclosingMethodOwner;
0281:
0282: /**
0283: * The constant pool item that contains the name and descriptor of the
0284: * enclosing method of this class.
0285: */
0286: private int enclosingMethod;
0287:
0288: /**
0289: * The runtime visible annotations of this class.
0290: */
0291: private AnnotationWriter anns;
0292:
0293: /**
0294: * The runtime invisible annotations of this class.
0295: */
0296: private AnnotationWriter ianns;
0297:
0298: /**
0299: * The non standard attributes of this class.
0300: */
0301: private Attribute attrs;
0302:
0303: /**
0304: * The number of entries in the InnerClasses attribute.
0305: */
0306: private int innerClassesCount;
0307:
0308: /**
0309: * The InnerClasses attribute.
0310: */
0311: private ByteVector innerClasses;
0312:
0313: /**
0314: * The fields of this class. These fields are stored in a linked list of
0315: * {@link FieldWriter} objects, linked to each other by their
0316: * {@link FieldWriter#next} field. This field stores the first element of
0317: * this list.
0318: */
0319: FieldWriter firstField;
0320:
0321: /**
0322: * The fields of this class. These fields are stored in a linked list of
0323: * {@link FieldWriter} objects, linked to each other by their
0324: * {@link FieldWriter#next} field. This field stores the last element of
0325: * this list.
0326: */
0327: FieldWriter lastField;
0328:
0329: /**
0330: * The methods of this class. These methods are stored in a linked list of
0331: * {@link MethodWriter} objects, linked to each other by their
0332: * {@link MethodWriter#next} field. This field stores the first element of
0333: * this list.
0334: */
0335: MethodWriter firstMethod;
0336:
0337: /**
0338: * The methods of this class. These methods are stored in a linked list of
0339: * {@link MethodWriter} objects, linked to each other by their
0340: * {@link MethodWriter#next} field. This field stores the last element of
0341: * this list.
0342: */
0343: MethodWriter lastMethod;
0344:
0345: /**
0346: * <tt>true</tt> if the maximum stack size and number of local variables
0347: * must be automatically computed.
0348: */
0349: private boolean computeMaxs;
0350:
0351: // ------------------------------------------------------------------------
0352: // Static initializer
0353: // ------------------------------------------------------------------------
0354:
0355: /**
0356: * Computes the instruction types of JVM opcodes.
0357: */
0358: static {
0359: int i;
0360: final byte[] b = new byte[220];
0361: final String s = "AAAAAAAAAAAAAAAABCKLLDDDDDEEEEEEEEEEEEEEEEEEEEAAAAAAAADD"
0362: + "DDDEEEEEEEEEEEEEEEEEEEEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
0363: + "AAAAAAAAAAAAAAAAAMAAAAAAAAAAAAAAAAAAAAIIIIIIIIIIIIIIIIDNOAA"
0364: + "AAAAGGGGGGGHAFBFAAFFAAQPIIJJIIIIIIIIIIIIIIIIII";
0365: for (i = 0; i < b.length; ++i) {
0366: b[i] = (byte) (s.charAt(i) - 'A');
0367: }
0368: ClassWriter.TYPE = b;
0369:
0370: // code to generate the above string
0371: //
0372: // // SBYTE_INSN instructions
0373: // b[Constants.NEWARRAY] = SBYTE_INSN;
0374: // b[Constants.BIPUSH] = SBYTE_INSN;
0375: //
0376: // // SHORT_INSN instructions
0377: // b[Constants.SIPUSH] = SHORT_INSN;
0378: //
0379: // // (IMPL)VAR_INSN instructions
0380: // b[Constants.RET] = VAR_INSN;
0381: // for (i = Constants.ILOAD; i <= Constants.ALOAD; ++i) {
0382: // b[i] = VAR_INSN;
0383: // }
0384: // for (i = Constants.ISTORE; i <= Constants.ASTORE; ++i) {
0385: // b[i] = VAR_INSN;
0386: // }
0387: // for (i = 26; i <= 45; ++i) { // ILOAD_0 to ALOAD_3
0388: // b[i] = IMPLVAR_INSN;
0389: // }
0390: // for (i = 59; i <= 78; ++i) { // ISTORE_0 to ASTORE_3
0391: // b[i] = IMPLVAR_INSN;
0392: // }
0393: //
0394: // // TYPE_INSN instructions
0395: // b[Constants.NEW] = TYPE_INSN;
0396: // b[Constants.ANEWARRAY] = TYPE_INSN;
0397: // b[Constants.CHECKCAST] = TYPE_INSN;
0398: // b[Constants.INSTANCEOF] = TYPE_INSN;
0399: //
0400: // // (Set)FIELDORMETH_INSN instructions
0401: // for (i = Constants.GETSTATIC; i <= Constants.INVOKESTATIC; ++i) {
0402: // b[i] = FIELDORMETH_INSN;
0403: // }
0404: // b[Constants.INVOKEINTERFACE] = ITFMETH_INSN;
0405: //
0406: // // LABEL(W)_INSN instructions
0407: // for (i = Constants.IFEQ; i <= Constants.JSR; ++i) {
0408: // b[i] = LABEL_INSN;
0409: // }
0410: // b[Constants.IFNULL] = LABEL_INSN;
0411: // b[Constants.IFNONNULL] = LABEL_INSN;
0412: // b[200] = LABELW_INSN; // GOTO_W
0413: // b[201] = LABELW_INSN; // JSR_W
0414: // // temporary opcodes used internally by ASM - see Label and
0415: // MethodWriter
0416: // for (i = 202; i < 220; ++i) {
0417: // b[i] = LABEL_INSN;
0418: // }
0419: //
0420: // // LDC(_W) instructions
0421: // b[Constants.LDC] = LDC_INSN;
0422: // b[19] = LDCW_INSN; // LDC_W
0423: // b[20] = LDCW_INSN; // LDC2_W
0424: //
0425: // // special instructions
0426: // b[Constants.IINC] = IINC_INSN;
0427: // b[Constants.TABLESWITCH] = TABL_INSN;
0428: // b[Constants.LOOKUPSWITCH] = LOOK_INSN;
0429: // b[Constants.MULTIANEWARRAY] = MANA_INSN;
0430: // b[196] = WIDE_INSN; // WIDE
0431: //
0432: // for (i = 0; i < b.length; ++i) {
0433: // System.err.print((char)('A' + b[i]));
0434: // }
0435: // System.err.println();
0436: }
0437:
0438: // ------------------------------------------------------------------------
0439: // Constructor
0440: // ------------------------------------------------------------------------
0441:
0442: /**
0443: * Constructs a new {@link ClassWriter ClassWriter} object.
0444: *
0445: * @param computeMaxs <tt>true</tt> if the maximum stack size and the
0446: * maximum number of local variables must be automatically computed.
0447: * If this flag is <tt>true</tt>, then the arguments of the
0448: * {@link MethodVisitor#visitMaxs visitMaxs} method of the
0449: * {@link MethodVisitor} returned by the
0450: * {@link #visitMethod visitMethod} method will be ignored, and
0451: * computed automatically from the signature and the bytecode of each
0452: * method.
0453: */
0454: public ClassWriter(final boolean computeMaxs) {
0455: this (computeMaxs, false);
0456: }
0457:
0458: /**
0459: * Constructs a new {@link ClassWriter} object.
0460: *
0461: * @param computeMaxs <tt>true</tt> if the maximum stack size and the
0462: * maximum number of local variables must be automatically computed.
0463: * If this flag is <tt>true</tt>, then the arguments of the
0464: * {@link MethodVisitor#visitMaxs visitMaxs} method of the
0465: * {@link MethodVisitor} returned by the
0466: * {@link #visitMethod visitMethod} method will be ignored, and
0467: * computed automatically from the signature and the bytecode of each
0468: * method.
0469: * @param skipUnknownAttributes <b>Deprecated</b>. The value of this
0470: * parameter is ignored.
0471: */
0472: public ClassWriter(final boolean computeMaxs,
0473: final boolean skipUnknownAttributes) {
0474: this .index = 1;
0475: this .pool = new ByteVector();
0476: this .items = new Item[256];
0477: this .threshold = (int) (0.75d * this .items.length);
0478: this .key = new Item();
0479: this .key2 = new Item();
0480: this .key3 = new Item();
0481: this .computeMaxs = computeMaxs;
0482: }
0483:
0484: /**
0485: * Constructs a new {@link ClassWriter} object and enables optimizations for
0486: * "mostly add" bytecode transformations. These optimizations are the
0487: * following:
0488: *
0489: * <ul> <li>The constant pool from the original class is copied as is in
0490: * the new class, which saves time. New constant pool entries will be added
0491: * at the end if necessary, but unused constant pool entries <i>won't be
0492: * removed</i>.</li> <li>Methods that are not transformed are copied as
0493: * is in the new class, directly from the original class bytecode (i.e.
0494: * without emitting visit events for all the method instructions), which
0495: * saves a <i>lot</i> of time. Untransformed methods are detected by the
0496: * fact that the {@link ClassReader} receives {@link MethodVisitor} objects
0497: * that come from a {@link ClassWriter} (and not from a custom
0498: * {@link ClassAdapter} or any other {@link ClassVisitor} instance).</li>
0499: * </ul>
0500: *
0501: * @param classReader the {@link ClassReader} used to read the original
0502: * class. It will be used to copy the entire constant pool from the
0503: * original class and also to copy other fragments of original
0504: * bytecode where applicable.
0505: * @param computeMaxs <tt>true</tt> if the maximum stack size and the
0506: * maximum number of local variables must be automatically computed.
0507: * If this flag is <tt>true</tt>, then the arguments of the
0508: * {@link MethodVisitor#visitMaxs visitMaxs} method of the
0509: * {@link MethodVisitor} returned by the
0510: * {@link #visitMethod visitMethod} method will be ignored, and
0511: * computed automatically from the signature and the bytecode of each
0512: * method.
0513: */
0514: public ClassWriter(final ClassReader classReader,
0515: final boolean computeMaxs) {
0516: this (computeMaxs, false);
0517: classReader.copyPool(this );
0518: this .cr = classReader;
0519: }
0520:
0521: // ------------------------------------------------------------------------
0522: // Implementation of the ClassVisitor interface
0523: // ------------------------------------------------------------------------
0524:
0525: public void visit(final int version, final int access,
0526: final String name, final String signature,
0527: final String super Name, final String[] interfaces) {
0528: this .version = version;
0529: this .access = access;
0530: this .name = newClass(name);
0531: if (signature != null) {
0532: this .signature = newUTF8(signature);
0533: }
0534: this .super Name = super Name == null ? 0 : newClass(super Name);
0535: if (interfaces != null && interfaces.length > 0) {
0536: this .interfaceCount = interfaces.length;
0537: this .interfaces = new int[this .interfaceCount];
0538: for (int i = 0; i < this .interfaceCount; ++i) {
0539: this .interfaces[i] = newClass(interfaces[i]);
0540: }
0541: }
0542: }
0543:
0544: public void visitSource(final String file, final String debug) {
0545: if (file != null) {
0546: this .sourceFile = newUTF8(file);
0547: }
0548: if (debug != null) {
0549: this .sourceDebug = new ByteVector().putUTF8(debug);
0550: }
0551: }
0552:
0553: public void visitOuterClass(final String owner, final String name,
0554: final String desc) {
0555: this .enclosingMethodOwner = newClass(owner);
0556: if (name != null && desc != null) {
0557: this .enclosingMethod = newNameType(name, desc);
0558: }
0559: }
0560:
0561: public AnnotationVisitor visitAnnotation(final String desc,
0562: final boolean visible) {
0563: final ByteVector bv = new ByteVector();
0564: // write type, and reserve space for values count
0565: bv.putShort(newUTF8(desc)).putShort(0);
0566: final AnnotationWriter aw = new AnnotationWriter(this , true,
0567: bv, bv, 2);
0568: if (visible) {
0569: aw.next = this .anns;
0570: this .anns = aw;
0571: } else {
0572: aw.next = this .ianns;
0573: this .ianns = aw;
0574: }
0575: return aw;
0576: }
0577:
0578: public void visitAttribute(final Attribute attr) {
0579: attr.next = this .attrs;
0580: this .attrs = attr;
0581: }
0582:
0583: public void visitInnerClass(final String name,
0584: final String outerName, final String innerName,
0585: final int access) {
0586: if (this .innerClasses == null) {
0587: this .innerClasses = new ByteVector();
0588: }
0589: ++this .innerClassesCount;
0590: this .innerClasses.putShort(name == null ? 0 : newClass(name));
0591: this .innerClasses.putShort(outerName == null ? 0
0592: : newClass(outerName));
0593: this .innerClasses.putShort(innerName == null ? 0
0594: : newUTF8(innerName));
0595: this .innerClasses.putShort(access);
0596: }
0597:
0598: public FieldVisitor visitField(final int access, final String name,
0599: final String desc, final String signature,
0600: final Object value) {
0601: return new FieldWriter(this , access, name, desc, signature,
0602: value);
0603: }
0604:
0605: public MethodVisitor visitMethod(final int access,
0606: final String name, final String desc,
0607: final String signature, final String[] exceptions) {
0608: return new MethodWriter(this , access, name, desc, signature,
0609: exceptions, this .computeMaxs);
0610: }
0611:
0612: public void visitEnd() {
0613: }
0614:
0615: // ------------------------------------------------------------------------
0616: // Other public methods
0617: // ------------------------------------------------------------------------
0618:
0619: /**
0620: * Returns the bytecode of the class that was build with this class writer.
0621: *
0622: * @return the bytecode of the class that was build with this class writer.
0623: */
0624: public byte[] toByteArray() {
0625: // computes the real size of the bytecode of this class
0626: int size = 24 + 2 * this .interfaceCount;
0627: int nbFields = 0;
0628: FieldWriter fb = this .firstField;
0629: while (fb != null) {
0630: ++nbFields;
0631: size += fb.getSize();
0632: fb = fb.next;
0633: }
0634: int nbMethods = 0;
0635: MethodWriter mb = this .firstMethod;
0636: while (mb != null) {
0637: ++nbMethods;
0638: size += mb.getSize();
0639: mb = mb.next;
0640: }
0641: int attributeCount = 0;
0642: if (this .signature != 0) {
0643: ++attributeCount;
0644: size += 8;
0645: newUTF8("Signature");
0646: }
0647: if (this .sourceFile != 0) {
0648: ++attributeCount;
0649: size += 8;
0650: newUTF8("SourceFile");
0651: }
0652: if (this .sourceDebug != null) {
0653: ++attributeCount;
0654: size += this .sourceDebug.length + 4;
0655: newUTF8("SourceDebugExtension");
0656: }
0657: if (this .enclosingMethodOwner != 0) {
0658: ++attributeCount;
0659: size += 10;
0660: newUTF8("EnclosingMethod");
0661: }
0662: if ((this .access & Opcodes.ACC_DEPRECATED) != 0) {
0663: ++attributeCount;
0664: size += 6;
0665: newUTF8("Deprecated");
0666: }
0667: if ((this .access & Opcodes.ACC_SYNTHETIC) != 0
0668: && (this .version & 0xffff) < Opcodes.V1_5) {
0669: ++attributeCount;
0670: size += 6;
0671: newUTF8("Synthetic");
0672: }
0673: if (this .version == Opcodes.V1_4) {
0674: if ((this .access & Opcodes.ACC_ANNOTATION) != 0) {
0675: ++attributeCount;
0676: size += 6;
0677: newUTF8("Annotation");
0678: }
0679: if ((this .access & Opcodes.ACC_ENUM) != 0) {
0680: ++attributeCount;
0681: size += 6;
0682: newUTF8("Enum");
0683: }
0684: }
0685: if (this .innerClasses != null) {
0686: ++attributeCount;
0687: size += 8 + this .innerClasses.length;
0688: newUTF8("InnerClasses");
0689: }
0690: if (this .anns != null) {
0691: ++attributeCount;
0692: size += 8 + this .anns.getSize();
0693: newUTF8("RuntimeVisibleAnnotations");
0694: }
0695: if (this .ianns != null) {
0696: ++attributeCount;
0697: size += 8 + this .ianns.getSize();
0698: newUTF8("RuntimeInvisibleAnnotations");
0699: }
0700: if (this .attrs != null) {
0701: attributeCount += this .attrs.getCount();
0702: size += this .attrs.getSize(this , null, 0, -1, -1);
0703: }
0704: size += this .pool.length;
0705: // allocates a byte vector of this size, in order to avoid unnecessary
0706: // arraycopy operations in the ByteVector.enlarge() method
0707: final ByteVector out = new ByteVector(size);
0708: out.putInt(0xCAFEBABE).putInt(this .version);
0709: out.putShort(this .index).putByteArray(this .pool.data, 0,
0710: this .pool.length);
0711: out.putShort(this .access).putShort(this .name).putShort(
0712: this .super Name);
0713: out.putShort(this .interfaceCount);
0714: for (int i = 0; i < this .interfaceCount; ++i) {
0715: out.putShort(this .interfaces[i]);
0716: }
0717: out.putShort(nbFields);
0718: fb = this .firstField;
0719: while (fb != null) {
0720: fb.put(out);
0721: fb = fb.next;
0722: }
0723: out.putShort(nbMethods);
0724: mb = this .firstMethod;
0725: while (mb != null) {
0726: mb.put(out);
0727: mb = mb.next;
0728: }
0729: out.putShort(attributeCount);
0730: if (this .signature != 0) {
0731: out.putShort(newUTF8("Signature")).putInt(2).putShort(
0732: this .signature);
0733: }
0734: if (this .sourceFile != 0) {
0735: out.putShort(newUTF8("SourceFile")).putInt(2).putShort(
0736: this .sourceFile);
0737: }
0738: if (this .sourceDebug != null) {
0739: final int len = this .sourceDebug.length - 2;
0740: out.putShort(newUTF8("SourceDebugExtension")).putInt(len);
0741: out.putByteArray(this .sourceDebug.data, 2, len);
0742: }
0743: if (this .enclosingMethodOwner != 0) {
0744: out.putShort(newUTF8("EnclosingMethod")).putInt(4);
0745: out.putShort(this .enclosingMethodOwner).putShort(
0746: this .enclosingMethod);
0747: }
0748: if ((this .access & Opcodes.ACC_DEPRECATED) != 0) {
0749: out.putShort(newUTF8("Deprecated")).putInt(0);
0750: }
0751: if ((this .access & Opcodes.ACC_SYNTHETIC) != 0
0752: && (this .version & 0xffff) < Opcodes.V1_5) {
0753: out.putShort(newUTF8("Synthetic")).putInt(0);
0754: }
0755: if (this .version == Opcodes.V1_4) {
0756: if ((this .access & Opcodes.ACC_ANNOTATION) != 0) {
0757: out.putShort(newUTF8("Annotation")).putInt(0);
0758: }
0759: if ((this .access & Opcodes.ACC_ENUM) != 0) {
0760: out.putShort(newUTF8("Enum")).putInt(0);
0761: }
0762: }
0763: if (this .innerClasses != null) {
0764: out.putShort(newUTF8("InnerClasses"));
0765: out.putInt(this .innerClasses.length + 2).putShort(
0766: this .innerClassesCount);
0767: out.putByteArray(this .innerClasses.data, 0,
0768: this .innerClasses.length);
0769: }
0770: if (this .anns != null) {
0771: out.putShort(newUTF8("RuntimeVisibleAnnotations"));
0772: this .anns.put(out);
0773: }
0774: if (this .ianns != null) {
0775: out.putShort(newUTF8("RuntimeInvisibleAnnotations"));
0776: this .ianns.put(out);
0777: }
0778: if (this .attrs != null) {
0779: this .attrs.put(this , null, 0, -1, -1, out);
0780: }
0781: return out.data;
0782: }
0783:
0784: // ------------------------------------------------------------------------
0785: // Utility methods: constant pool management
0786: // ------------------------------------------------------------------------
0787:
0788: /**
0789: * Adds a number or string constant to the constant pool of the class being
0790: * build. Does nothing if the constant pool already contains a similar item.
0791: *
0792: * @param cst the value of the constant to be added to the constant pool.
0793: * This parameter must be an {@link Integer}, a {@link Float}, a
0794: * {@link Long}, a {@link Double}, a {@link String} or a
0795: * {@link Type}.
0796: * @return a new or already existing constant item with the given value.
0797: */
0798: Item newConstItem(final Object cst) {
0799: if (cst instanceof Integer) {
0800: final int val = ((Integer) cst).intValue();
0801: return newInteger(val);
0802: } else if (cst instanceof Byte) {
0803: final int val = ((Byte) cst).intValue();
0804: return newInteger(val);
0805: } else if (cst instanceof Character) {
0806: final int val = ((Character) cst).charValue();
0807: return newInteger(val);
0808: } else if (cst instanceof Short) {
0809: final int val = ((Short) cst).intValue();
0810: return newInteger(val);
0811: } else if (cst instanceof Boolean) {
0812: final int val = ((Boolean) cst).booleanValue() ? 1 : 0;
0813: return newInteger(val);
0814: } else if (cst instanceof Float) {
0815: final float val = ((Float) cst).floatValue();
0816: return newFloat(val);
0817: } else if (cst instanceof Long) {
0818: final long val = ((Long) cst).longValue();
0819: return newLong(val);
0820: } else if (cst instanceof Double) {
0821: final double val = ((Double) cst).doubleValue();
0822: return newDouble(val);
0823: } else if (cst instanceof String) {
0824: return newString((String) cst);
0825: } else if (cst instanceof Type) {
0826: final Type t = (Type) cst;
0827: return newClassItem(t.getSort() == Type.OBJECT ? t
0828: .getInternalName() : t.getDescriptor());
0829: } else {
0830: throw new IllegalArgumentException("value " + cst);
0831: }
0832: }
0833:
0834: /**
0835: * Adds a number or string constant to the constant pool of the class being
0836: * build. Does nothing if the constant pool already contains a similar item.
0837: * <i>This method is intended for {@link Attribute} sub classes, and is
0838: * normally not needed by class generators or adapters.</i>
0839: *
0840: * @param cst the value of the constant to be added to the constant pool.
0841: * This parameter must be an {@link Integer}, a {@link Float}, a
0842: * {@link Long}, a {@link Double} or a {@link String}.
0843: * @return the index of a new or already existing constant item with the
0844: * given value.
0845: */
0846: public int newConst(final Object cst) {
0847: return newConstItem(cst).index;
0848: }
0849:
0850: /**
0851: * Adds an UTF8 string to the constant pool of the class being build. Does
0852: * nothing if the constant pool already contains a similar item. <i>This
0853: * method is intended for {@link Attribute} sub classes, and is normally not
0854: * needed by class generators or adapters.</i>
0855: *
0856: * @param value the String value.
0857: * @return the index of a new or already existing UTF8 item.
0858: */
0859: public int newUTF8(final String value) {
0860: this .key.set(ClassWriter.UTF8, value, null, null);
0861: Item result = get(this .key);
0862: if (result == null) {
0863: this .pool.putByte(ClassWriter.UTF8).putUTF8(value);
0864: result = new Item(this .index++, this .key);
0865: put(result);
0866: }
0867: return result.index;
0868: }
0869:
0870: /**
0871: * Adds a class reference to the constant pool of the class being build.
0872: * Does nothing if the constant pool already contains a similar item.
0873: * <i>This method is intended for {@link Attribute} sub classes, and is
0874: * normally not needed by class generators or adapters.</i>
0875: *
0876: * @param value the internal name of the class.
0877: * @return the index of a new or already existing class reference item.
0878: */
0879: public int newClass(final String value) {
0880: return newClassItem(value).index;
0881: }
0882:
0883: /**
0884: * Adds a class reference to the constant pool of the class being build.
0885: * Does nothing if the constant pool already contains a similar item.
0886: * <i>This method is intended for {@link Attribute} sub classes, and is
0887: * normally not needed by class generators or adapters.</i>
0888: *
0889: * @param value the internal name of the class.
0890: * @return a new or already existing class reference item.
0891: */
0892: private Item newClassItem(final String value) {
0893: this .key2.set(ClassWriter.CLASS, value, null, null);
0894: Item result = get(this .key2);
0895: if (result == null) {
0896: this .pool.put12(ClassWriter.CLASS, newUTF8(value));
0897: result = new Item(this .index++, this .key2);
0898: put(result);
0899: }
0900: return result;
0901: }
0902:
0903: /**
0904: * Adds a field reference to the constant pool of the class being build.
0905: * Does nothing if the constant pool already contains a similar item.
0906: * <i>This method is intended for {@link Attribute} sub classes, and is
0907: * normally not needed by class generators or adapters.</i>
0908: *
0909: * @param owner the internal name of the field's owner class.
0910: * @param name the field's name.
0911: * @param desc the field's descriptor.
0912: * @return the index of a new or already existing field reference item.
0913: */
0914: public int newField(final String owner, final String name,
0915: final String desc) {
0916: this .key3.set(ClassWriter.FIELD, owner, name, desc);
0917: Item result = get(this .key3);
0918: if (result == null) {
0919: put122(ClassWriter.FIELD, newClass(owner), newNameType(
0920: name, desc));
0921: result = new Item(this .index++, this .key3);
0922: put(result);
0923: }
0924: return result.index;
0925: }
0926:
0927: /**
0928: * Adds a method reference to the constant pool of the class being build.
0929: * Does nothing if the constant pool already contains a similar item.
0930: *
0931: * @param owner the internal name of the method's owner class.
0932: * @param name the method's name.
0933: * @param desc the method's descriptor.
0934: * @param itf <tt>true</tt> if <tt>owner</tt> is an interface.
0935: * @return a new or already existing method reference item.
0936: */
0937: Item newMethodItem(final String owner, final String name,
0938: final String desc, final boolean itf) {
0939: final int type = itf ? ClassWriter.IMETH : ClassWriter.METH;
0940: this .key3.set(type, owner, name, desc);
0941: Item result = get(this .key3);
0942: if (result == null) {
0943: put122(type, newClass(owner), newNameType(name, desc));
0944: result = new Item(this .index++, this .key3);
0945: put(result);
0946: }
0947: return result;
0948: }
0949:
0950: /**
0951: * Adds a method reference to the constant pool of the class being build.
0952: * Does nothing if the constant pool already contains a similar item.
0953: * <i>This method is intended for {@link Attribute} sub classes, and is
0954: * normally not needed by class generators or adapters.</i>
0955: *
0956: * @param owner the internal name of the method's owner class.
0957: * @param name the method's name.
0958: * @param desc the method's descriptor.
0959: * @param itf <tt>true</tt> if <tt>owner</tt> is an interface.
0960: * @return the index of a new or already existing method reference item.
0961: */
0962: public int newMethod(final String owner, final String name,
0963: final String desc, final boolean itf) {
0964: return newMethodItem(owner, name, desc, itf).index;
0965: }
0966:
0967: /**
0968: * Adds an integer to the constant pool of the class being build. Does
0969: * nothing if the constant pool already contains a similar item.
0970: *
0971: * @param value the int value.
0972: * @return a new or already existing int item.
0973: */
0974: Item newInteger(final int value) {
0975: this .key.set(value);
0976: Item result = get(this .key);
0977: if (result == null) {
0978: this .pool.putByte(ClassWriter.INT).putInt(value);
0979: result = new Item(this .index++, this .key);
0980: put(result);
0981: }
0982: return result;
0983: }
0984:
0985: /**
0986: * Adds a float to the constant pool of the class being build. Does nothing
0987: * if the constant pool already contains a similar item.
0988: *
0989: * @param value the float value.
0990: * @return a new or already existing float item.
0991: */
0992: Item newFloat(final float value) {
0993: this .key.set(value);
0994: Item result = get(this .key);
0995: if (result == null) {
0996: this .pool.putByte(ClassWriter.FLOAT).putInt(
0997: Float.floatToIntBits(value));
0998: result = new Item(this .index++, this .key);
0999: put(result);
1000: }
1001: return result;
1002: }
1003:
1004: /**
1005: * Adds a long to the constant pool of the class being build. Does nothing
1006: * if the constant pool already contains a similar item.
1007: *
1008: * @param value the long value.
1009: * @return a new or already existing long item.
1010: */
1011: Item newLong(final long value) {
1012: this .key.set(value);
1013: Item result = get(this .key);
1014: if (result == null) {
1015: this .pool.putByte(ClassWriter.LONG).putLong(value);
1016: result = new Item(this .index, this .key);
1017: put(result);
1018: this .index += 2;
1019: }
1020: return result;
1021: }
1022:
1023: /**
1024: * Adds a double to the constant pool of the class being build. Does nothing
1025: * if the constant pool already contains a similar item.
1026: *
1027: * @param value the double value.
1028: * @return a new or already existing double item.
1029: */
1030: Item newDouble(final double value) {
1031: this .key.set(value);
1032: Item result = get(this .key);
1033: if (result == null) {
1034: this .pool.putByte(ClassWriter.DOUBLE).putLong(
1035: Double.doubleToLongBits(value));
1036: result = new Item(this .index, this .key);
1037: put(result);
1038: this .index += 2;
1039: }
1040: return result;
1041: }
1042:
1043: /**
1044: * Adds a string to the constant pool of the class being build. Does nothing
1045: * if the constant pool already contains a similar item.
1046: *
1047: * @param value the String value.
1048: * @return a new or already existing string item.
1049: */
1050: private Item newString(final String value) {
1051: this .key2.set(ClassWriter.STR, value, null, null);
1052: Item result = get(this .key2);
1053: if (result == null) {
1054: this .pool.put12(ClassWriter.STR, newUTF8(value));
1055: result = new Item(this .index++, this .key2);
1056: put(result);
1057: }
1058: return result;
1059: }
1060:
1061: /**
1062: * Adds a name and type to the constant pool of the class being build. Does
1063: * nothing if the constant pool already contains a similar item. <i>This
1064: * method is intended for {@link Attribute} sub classes, and is normally not
1065: * needed by class generators or adapters.</i>
1066: *
1067: * @param name a name.
1068: * @param desc a type descriptor.
1069: * @return the index of a new or already existing name and type item.
1070: */
1071: public int newNameType(final String name, final String desc) {
1072: this .key2.set(ClassWriter.NAME_TYPE, name, desc, null);
1073: Item result = get(this .key2);
1074: if (result == null) {
1075: put122(ClassWriter.NAME_TYPE, newUTF8(name), newUTF8(desc));
1076: result = new Item(this .index++, this .key2);
1077: put(result);
1078: }
1079: return result.index;
1080: }
1081:
1082: /**
1083: * Returns the constant pool's hash table item which is equal to the given
1084: * item.
1085: *
1086: * @param key a constant pool item.
1087: * @return the constant pool's hash table item which is equal to the given
1088: * item, or <tt>null</tt> if there is no such item.
1089: */
1090: private Item get(final Item key) {
1091: Item i = this .items[key.hashCode % this .items.length];
1092: while (i != null && !key.isEqualTo(i)) {
1093: i = i.next;
1094: }
1095: return i;
1096: }
1097:
1098: /**
1099: * Puts the given item in the constant pool's hash table. The hash table
1100: * <i>must</i> not already contains this item.
1101: *
1102: * @param i the item to be added to the constant pool's hash table.
1103: */
1104: private void put(final Item i) {
1105: if (this .index > this .threshold) {
1106: final int ll = this .items.length;
1107: final int nl = ll * 2 + 1;
1108: final Item[] newItems = new Item[nl];
1109: for (int l = ll - 1; l >= 0; --l) {
1110: Item j = this .items[l];
1111: while (j != null) {
1112: final int index = j.hashCode % newItems.length;
1113: final Item k = j.next;
1114: j.next = newItems[index];
1115: newItems[index] = j;
1116: j = k;
1117: }
1118: }
1119: this .items = newItems;
1120: this .threshold = (int) (nl * 0.75);
1121: }
1122: final int index = i.hashCode % this .items.length;
1123: i.next = this .items[index];
1124: this .items[index] = i;
1125: }
1126:
1127: /**
1128: * Puts one byte and two shorts into the constant pool.
1129: *
1130: * @param b a byte.
1131: * @param s1 a short.
1132: * @param s2 another short.
1133: */
1134: private void put122(final int b, final int s1, final int s2) {
1135: this.pool.put12(b, s1).putShort(s2);
1136: }
1137: }
|