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 com.uwyn.rife.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: byte[] b = new byte[220];
0361: 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: 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: index = 1;
0475: pool = new ByteVector();
0476: items = new Item[256];
0477: threshold = (int) (0.75d * items.length);
0478: key = new Item();
0479: key2 = new Item();
0480: 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: interfaceCount = interfaces.length;
0537: this .interfaces = new int[interfaceCount];
0538: for (int i = 0; i < 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: sourceFile = newUTF8(file);
0547: }
0548: if (debug != null) {
0549: sourceDebug = new ByteVector().putUTF8(debug);
0550: }
0551: }
0552:
0553: public void visitOuterClass(final String owner, final String name,
0554: final String desc) {
0555: enclosingMethodOwner = newClass(owner);
0556: if (name != null && desc != null) {
0557: enclosingMethod = newNameType(name, desc);
0558: }
0559: }
0560:
0561: public AnnotationVisitor visitAnnotation(String desc,
0562: boolean visible) {
0563: ByteVector bv = new ByteVector();
0564: // write type, and reserve space for values count
0565: bv.putShort(newUTF8(desc)).putShort(0);
0566: AnnotationWriter aw = new AnnotationWriter(this , true, bv, bv,
0567: 2);
0568: if (visible) {
0569: aw.next = anns;
0570: anns = aw;
0571: } else {
0572: aw.next = ianns;
0573: ianns = aw;
0574: }
0575: return aw;
0576: }
0577:
0578: public void visitAttribute(final Attribute attr) {
0579: attr.next = attrs;
0580: attrs = attr;
0581: }
0582:
0583: public void visitInnerClass(final String name,
0584: final String outerName, final String innerName,
0585: final int access) {
0586: if (innerClasses == null) {
0587: innerClasses = new ByteVector();
0588: }
0589: ++innerClassesCount;
0590: innerClasses.putShort(name == null ? 0 : newClass(name));
0591: innerClasses.putShort(outerName == null ? 0
0592: : newClass(outerName));
0593: innerClasses.putShort(innerName == null ? 0
0594: : newUTF8(innerName));
0595: 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, 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 * interfaceCount;
0627: int nbFields = 0;
0628: FieldWriter fb = firstField;
0629: while (fb != null) {
0630: ++nbFields;
0631: size += fb.getSize();
0632: fb = fb.next;
0633: }
0634: int nbMethods = 0;
0635: MethodWriter mb = firstMethod;
0636: while (mb != null) {
0637: ++nbMethods;
0638: size += mb.getSize();
0639: mb = mb.next;
0640: }
0641: int attributeCount = 0;
0642: if (signature != 0) {
0643: ++attributeCount;
0644: size += 8;
0645: newUTF8("Signature");
0646: }
0647: if (sourceFile != 0) {
0648: ++attributeCount;
0649: size += 8;
0650: newUTF8("SourceFile");
0651: }
0652: if (sourceDebug != null) {
0653: ++attributeCount;
0654: size += sourceDebug.length + 4;
0655: newUTF8("SourceDebugExtension");
0656: }
0657: if (enclosingMethodOwner != 0) {
0658: ++attributeCount;
0659: size += 10;
0660: newUTF8("EnclosingMethod");
0661: }
0662: if ((access & Opcodes.ACC_DEPRECATED) != 0) {
0663: ++attributeCount;
0664: size += 6;
0665: newUTF8("Deprecated");
0666: }
0667: if ((access & Opcodes.ACC_SYNTHETIC) != 0
0668: && (version & 0xffff) < Opcodes.V1_5) {
0669: ++attributeCount;
0670: size += 6;
0671: newUTF8("Synthetic");
0672: }
0673: if (version == Opcodes.V1_4) {
0674: if ((access & Opcodes.ACC_ANNOTATION) != 0) {
0675: ++attributeCount;
0676: size += 6;
0677: newUTF8("Annotation");
0678: }
0679: if ((access & Opcodes.ACC_ENUM) != 0) {
0680: ++attributeCount;
0681: size += 6;
0682: newUTF8("Enum");
0683: }
0684: }
0685: if (innerClasses != null) {
0686: ++attributeCount;
0687: size += 8 + innerClasses.length;
0688: newUTF8("InnerClasses");
0689: }
0690: if (anns != null) {
0691: ++attributeCount;
0692: size += 8 + anns.getSize();
0693: newUTF8("RuntimeVisibleAnnotations");
0694: }
0695: if (ianns != null) {
0696: ++attributeCount;
0697: size += 8 + ianns.getSize();
0698: newUTF8("RuntimeInvisibleAnnotations");
0699: }
0700: if (attrs != null) {
0701: attributeCount += attrs.getCount();
0702: size += attrs.getSize(this , null, 0, -1, -1);
0703: }
0704: size += pool.length;
0705: // allocates a byte vector of this size, in order to avoid unnecessary
0706: // arraycopy operations in the ByteVector.enlarge() method
0707: ByteVector out = new ByteVector(size);
0708: out.putInt(0xCAFEBABE).putInt(version);
0709: out.putShort(index).putByteArray(pool.data, 0, pool.length);
0710: out.putShort(access).putShort(name).putShort(super Name);
0711: out.putShort(interfaceCount);
0712: for (int i = 0; i < interfaceCount; ++i) {
0713: out.putShort(interfaces[i]);
0714: }
0715: out.putShort(nbFields);
0716: fb = firstField;
0717: while (fb != null) {
0718: fb.put(out);
0719: fb = fb.next;
0720: }
0721: out.putShort(nbMethods);
0722: mb = firstMethod;
0723: while (mb != null) {
0724: mb.put(out);
0725: mb = mb.next;
0726: }
0727: out.putShort(attributeCount);
0728: if (signature != 0) {
0729: out.putShort(newUTF8("Signature")).putInt(2).putShort(
0730: signature);
0731: }
0732: if (sourceFile != 0) {
0733: out.putShort(newUTF8("SourceFile")).putInt(2).putShort(
0734: sourceFile);
0735: }
0736: if (sourceDebug != null) {
0737: int len = sourceDebug.length - 2;
0738: out.putShort(newUTF8("SourceDebugExtension")).putInt(len);
0739: out.putByteArray(sourceDebug.data, 2, len);
0740: }
0741: if (enclosingMethodOwner != 0) {
0742: out.putShort(newUTF8("EnclosingMethod")).putInt(4);
0743: out.putShort(enclosingMethodOwner)
0744: .putShort(enclosingMethod);
0745: }
0746: if ((access & Opcodes.ACC_DEPRECATED) != 0) {
0747: out.putShort(newUTF8("Deprecated")).putInt(0);
0748: }
0749: if ((access & Opcodes.ACC_SYNTHETIC) != 0
0750: && (version & 0xffff) < Opcodes.V1_5) {
0751: out.putShort(newUTF8("Synthetic")).putInt(0);
0752: }
0753: if (version == Opcodes.V1_4) {
0754: if ((access & Opcodes.ACC_ANNOTATION) != 0) {
0755: out.putShort(newUTF8("Annotation")).putInt(0);
0756: }
0757: if ((access & Opcodes.ACC_ENUM) != 0) {
0758: out.putShort(newUTF8("Enum")).putInt(0);
0759: }
0760: }
0761: if (innerClasses != null) {
0762: out.putShort(newUTF8("InnerClasses"));
0763: out.putInt(innerClasses.length + 2).putShort(
0764: innerClassesCount);
0765: out.putByteArray(innerClasses.data, 0, innerClasses.length);
0766: }
0767: if (anns != null) {
0768: out.putShort(newUTF8("RuntimeVisibleAnnotations"));
0769: anns.put(out);
0770: }
0771: if (ianns != null) {
0772: out.putShort(newUTF8("RuntimeInvisibleAnnotations"));
0773: ianns.put(out);
0774: }
0775: if (attrs != null) {
0776: attrs.put(this , null, 0, -1, -1, out);
0777: }
0778: return out.data;
0779: }
0780:
0781: // ------------------------------------------------------------------------
0782: // Utility methods: constant pool management
0783: // ------------------------------------------------------------------------
0784:
0785: /**
0786: * Adds a number or string constant to the constant pool of the class being
0787: * build. Does nothing if the constant pool already contains a similar item.
0788: *
0789: * @param cst the value of the constant to be added to the constant pool.
0790: * This parameter must be an {@link Integer}, a {@link Float}, a
0791: * {@link Long}, a {@link Double}, a {@link String} or a
0792: * {@link Type}.
0793: * @return a new or already existing constant item with the given value.
0794: */
0795: Item newConstItem(final Object cst) {
0796: if (cst instanceof Integer) {
0797: int val = ((Integer) cst).intValue();
0798: return newInteger(val);
0799: } else if (cst instanceof Byte) {
0800: int val = ((Byte) cst).intValue();
0801: return newInteger(val);
0802: } else if (cst instanceof Character) {
0803: int val = ((Character) cst).charValue();
0804: return newInteger(val);
0805: } else if (cst instanceof Short) {
0806: int val = ((Short) cst).intValue();
0807: return newInteger(val);
0808: } else if (cst instanceof Boolean) {
0809: int val = ((Boolean) cst).booleanValue() ? 1 : 0;
0810: return newInteger(val);
0811: } else if (cst instanceof Float) {
0812: float val = ((Float) cst).floatValue();
0813: return newFloat(val);
0814: } else if (cst instanceof Long) {
0815: long val = ((Long) cst).longValue();
0816: return newLong(val);
0817: } else if (cst instanceof Double) {
0818: double val = ((Double) cst).doubleValue();
0819: return newDouble(val);
0820: } else if (cst instanceof String) {
0821: return newString((String) cst);
0822: } else if (cst instanceof Type) {
0823: Type t = (Type) cst;
0824: return newClassItem(t.getSort() == Type.OBJECT ? t
0825: .getInternalName() : t.getDescriptor());
0826: } else {
0827: throw new IllegalArgumentException("value " + cst);
0828: }
0829: }
0830:
0831: /**
0832: * Adds a number or string constant to the constant pool of the class being
0833: * build. Does nothing if the constant pool already contains a similar item.
0834: * <i>This method is intended for {@link Attribute} sub classes, and is
0835: * normally not needed by class generators or adapters.</i>
0836: *
0837: * @param cst the value of the constant to be added to the constant pool.
0838: * This parameter must be an {@link Integer}, a {@link Float}, a
0839: * {@link Long}, a {@link Double} or a {@link String}.
0840: * @return the index of a new or already existing constant item with the
0841: * given value.
0842: */
0843: public int newConst(final Object cst) {
0844: return newConstItem(cst).index;
0845: }
0846:
0847: /**
0848: * Adds an UTF8 string to the constant pool of the class being build. Does
0849: * nothing if the constant pool already contains a similar item. <i>This
0850: * method is intended for {@link Attribute} sub classes, and is normally not
0851: * needed by class generators or adapters.</i>
0852: *
0853: * @param value the String value.
0854: * @return the index of a new or already existing UTF8 item.
0855: */
0856: public int newUTF8(final String value) {
0857: key.set(UTF8, value, null, null);
0858: Item result = get(key);
0859: if (result == null) {
0860: pool.putByte(UTF8).putUTF8(value);
0861: result = new Item(index++, key);
0862: put(result);
0863: }
0864: return result.index;
0865: }
0866:
0867: /**
0868: * Adds a class reference to the constant pool of the class being build.
0869: * Does nothing if the constant pool already contains a similar item.
0870: * <i>This method is intended for {@link Attribute} sub classes, and is
0871: * normally not needed by class generators or adapters.</i>
0872: *
0873: * @param value the internal name of the class.
0874: * @return the index of a new or already existing class reference item.
0875: */
0876: public int newClass(final String value) {
0877: return newClassItem(value).index;
0878: }
0879:
0880: /**
0881: * Adds a class reference to the constant pool of the class being build.
0882: * Does nothing if the constant pool already contains a similar item.
0883: * <i>This method is intended for {@link Attribute} sub classes, and is
0884: * normally not needed by class generators or adapters.</i>
0885: *
0886: * @param value the internal name of the class.
0887: * @return a new or already existing class reference item.
0888: */
0889: private Item newClassItem(final String value) {
0890: key2.set(CLASS, value, null, null);
0891: Item result = get(key2);
0892: if (result == null) {
0893: pool.put12(CLASS, newUTF8(value));
0894: result = new Item(index++, key2);
0895: put(result);
0896: }
0897: return result;
0898: }
0899:
0900: /**
0901: * Adds a field reference to the constant pool of the class being build.
0902: * Does nothing if the constant pool already contains a similar item.
0903: * <i>This method is intended for {@link Attribute} sub classes, and is
0904: * normally not needed by class generators or adapters.</i>
0905: *
0906: * @param owner the internal name of the field's owner class.
0907: * @param name the field's name.
0908: * @param desc the field's descriptor.
0909: * @return the index of a new or already existing field reference item.
0910: */
0911: public int newField(final String owner, final String name,
0912: final String desc) {
0913: key3.set(FIELD, owner, name, desc);
0914: Item result = get(key3);
0915: if (result == null) {
0916: put122(FIELD, newClass(owner), newNameType(name, desc));
0917: result = new Item(index++, key3);
0918: put(result);
0919: }
0920: return result.index;
0921: }
0922:
0923: /**
0924: * Adds a method reference to the constant pool of the class being build.
0925: * Does nothing if the constant pool already contains a similar item.
0926: *
0927: * @param owner the internal name of the method's owner class.
0928: * @param name the method's name.
0929: * @param desc the method's descriptor.
0930: * @param itf <tt>true</tt> if <tt>owner</tt> is an interface.
0931: * @return a new or already existing method reference item.
0932: */
0933: Item newMethodItem(final String owner, final String name,
0934: final String desc, final boolean itf) {
0935: int type = itf ? IMETH : METH;
0936: key3.set(type, owner, name, desc);
0937: Item result = get(key3);
0938: if (result == null) {
0939: put122(type, newClass(owner), newNameType(name, desc));
0940: result = new Item(index++, key3);
0941: put(result);
0942: }
0943: return result;
0944: }
0945:
0946: /**
0947: * Adds a method reference to the constant pool of the class being build.
0948: * Does nothing if the constant pool already contains a similar item.
0949: * <i>This method is intended for {@link Attribute} sub classes, and is
0950: * normally not needed by class generators or adapters.</i>
0951: *
0952: * @param owner the internal name of the method's owner class.
0953: * @param name the method's name.
0954: * @param desc the method's descriptor.
0955: * @param itf <tt>true</tt> if <tt>owner</tt> is an interface.
0956: * @return the index of a new or already existing method reference item.
0957: */
0958: public int newMethod(final String owner, final String name,
0959: final String desc, final boolean itf) {
0960: return newMethodItem(owner, name, desc, itf).index;
0961: }
0962:
0963: /**
0964: * Adds an integer to the constant pool of the class being build. Does
0965: * nothing if the constant pool already contains a similar item.
0966: *
0967: * @param value the int value.
0968: * @return a new or already existing int item.
0969: */
0970: Item newInteger(final int value) {
0971: key.set(value);
0972: Item result = get(key);
0973: if (result == null) {
0974: pool.putByte(INT).putInt(value);
0975: result = new Item(index++, key);
0976: put(result);
0977: }
0978: return result;
0979: }
0980:
0981: /**
0982: * Adds a float to the constant pool of the class being build. Does nothing
0983: * if the constant pool already contains a similar item.
0984: *
0985: * @param value the float value.
0986: * @return a new or already existing float item.
0987: */
0988: Item newFloat(final float value) {
0989: key.set(value);
0990: Item result = get(key);
0991: if (result == null) {
0992: pool.putByte(FLOAT).putInt(Float.floatToIntBits(value));
0993: result = new Item(index++, key);
0994: put(result);
0995: }
0996: return result;
0997: }
0998:
0999: /**
1000: * Adds a long to the constant pool of the class being build. Does nothing
1001: * if the constant pool already contains a similar item.
1002: *
1003: * @param value the long value.
1004: * @return a new or already existing long item.
1005: */
1006: Item newLong(final long value) {
1007: key.set(value);
1008: Item result = get(key);
1009: if (result == null) {
1010: pool.putByte(LONG).putLong(value);
1011: result = new Item(index, key);
1012: put(result);
1013: index += 2;
1014: }
1015: return result;
1016: }
1017:
1018: /**
1019: * Adds a double to the constant pool of the class being build. Does nothing
1020: * if the constant pool already contains a similar item.
1021: *
1022: * @param value the double value.
1023: * @return a new or already existing double item.
1024: */
1025: Item newDouble(final double value) {
1026: key.set(value);
1027: Item result = get(key);
1028: if (result == null) {
1029: pool.putByte(DOUBLE)
1030: .putLong(Double.doubleToLongBits(value));
1031: result = new Item(index, key);
1032: put(result);
1033: index += 2;
1034: }
1035: return result;
1036: }
1037:
1038: /**
1039: * Adds a string to the constant pool of the class being build. Does nothing
1040: * if the constant pool already contains a similar item.
1041: *
1042: * @param value the String value.
1043: * @return a new or already existing string item.
1044: */
1045: private Item newString(final String value) {
1046: key2.set(STR, value, null, null);
1047: Item result = get(key2);
1048: if (result == null) {
1049: pool.put12(STR, newUTF8(value));
1050: result = new Item(index++, key2);
1051: put(result);
1052: }
1053: return result;
1054: }
1055:
1056: /**
1057: * Adds a name and type to the constant pool of the class being build. Does
1058: * nothing if the constant pool already contains a similar item. <i>This
1059: * method is intended for {@link Attribute} sub classes, and is normally not
1060: * needed by class generators or adapters.</i>
1061: *
1062: * @param name a name.
1063: * @param desc a type descriptor.
1064: * @return the index of a new or already existing name and type item.
1065: */
1066: public int newNameType(final String name, final String desc) {
1067: key2.set(NAME_TYPE, name, desc, null);
1068: Item result = get(key2);
1069: if (result == null) {
1070: put122(NAME_TYPE, newUTF8(name), newUTF8(desc));
1071: result = new Item(index++, key2);
1072: put(result);
1073: }
1074: return result.index;
1075: }
1076:
1077: /**
1078: * Returns the constant pool's hash table item which is equal to the given
1079: * item.
1080: *
1081: * @param key a constant pool item.
1082: * @return the constant pool's hash table item which is equal to the given
1083: * item, or <tt>null</tt> if there is no such item.
1084: */
1085: private Item get(final Item key) {
1086: Item i = items[key.hashCode % items.length];
1087: while (i != null && !key.isEqualTo(i)) {
1088: i = i.next;
1089: }
1090: return i;
1091: }
1092:
1093: /**
1094: * Puts the given item in the constant pool's hash table. The hash table
1095: * <i>must</i> not already contains this item.
1096: *
1097: * @param i the item to be added to the constant pool's hash table.
1098: */
1099: private void put(final Item i) {
1100: if (index > threshold) {
1101: int ll = items.length;
1102: int nl = ll * 2 + 1;
1103: Item[] newItems = new Item[nl];
1104: for (int l = ll - 1; l >= 0; --l) {
1105: Item j = items[l];
1106: while (j != null) {
1107: int index = j.hashCode % newItems.length;
1108: Item k = j.next;
1109: j.next = newItems[index];
1110: newItems[index] = j;
1111: j = k;
1112: }
1113: }
1114: items = newItems;
1115: threshold = (int) (nl * 0.75);
1116: }
1117: int index = i.hashCode % items.length;
1118: i.next = items[index];
1119: items[index] = i;
1120: }
1121:
1122: /**
1123: * Puts one byte and two shorts into the constant pool.
1124: *
1125: * @param b a byte.
1126: * @param s1 a short.
1127: * @param s2 another short.
1128: */
1129: private void put122(final int b, final int s1, final int s2) {
1130: pool.put12(b, s1).putShort(s2);
1131: }
1132: }
|