0001: /***
0002: * ASM: a very small and fast Java bytecode manipulation framework
0003: * Copyright (c) 2000-2007 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.objectweb.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: * Flag to automatically compute the maximum stack size and the maximum
0044: * number of local variables of methods. If this flag is set, then the
0045: * arguments of the {@link MethodVisitor#visitMaxs visitMaxs} method of the
0046: * {@link MethodVisitor} returned by the {@link #visitMethod visitMethod}
0047: * method will be ignored, and computed automatically from the signature and
0048: * the bytecode of each method.
0049: *
0050: * @see #ClassWriter(int)
0051: */
0052: public static final int COMPUTE_MAXS = 1;
0053:
0054: /**
0055: * Flag to automatically compute the stack map frames of methods from
0056: * scratch. If this flag is set, then the calls to the
0057: * {@link MethodVisitor#visitFrame} method are ignored, and the stack map
0058: * frames are recomputed from the methods bytecode. The arguments of the
0059: * {@link MethodVisitor#visitMaxs visitMaxs} method are also ignored and
0060: * recomputed from the bytecode. In other words, computeFrames implies
0061: * computeMaxs.
0062: *
0063: * @see #ClassWriter(int)
0064: */
0065: public static final int COMPUTE_FRAMES = 2;
0066:
0067: /**
0068: * The type of instructions without any argument.
0069: */
0070: static final int NOARG_INSN = 0;
0071:
0072: /**
0073: * The type of instructions with an signed byte argument.
0074: */
0075: static final int SBYTE_INSN = 1;
0076:
0077: /**
0078: * The type of instructions with an signed short argument.
0079: */
0080: static final int SHORT_INSN = 2;
0081:
0082: /**
0083: * The type of instructions with a local variable index argument.
0084: */
0085: static final int VAR_INSN = 3;
0086:
0087: /**
0088: * The type of instructions with an implicit local variable index argument.
0089: */
0090: static final int IMPLVAR_INSN = 4;
0091:
0092: /**
0093: * The type of instructions with a type descriptor argument.
0094: */
0095: static final int TYPE_INSN = 5;
0096:
0097: /**
0098: * The type of field and method invocations instructions.
0099: */
0100: static final int FIELDORMETH_INSN = 6;
0101:
0102: /**
0103: * The type of the INVOKEINTERFACE instruction.
0104: */
0105: static final int ITFMETH_INSN = 7;
0106:
0107: /**
0108: * The type of instructions with a 2 bytes bytecode offset label.
0109: */
0110: static final int LABEL_INSN = 8;
0111:
0112: /**
0113: * The type of instructions with a 4 bytes bytecode offset label.
0114: */
0115: static final int LABELW_INSN = 9;
0116:
0117: /**
0118: * The type of the LDC instruction.
0119: */
0120: static final int LDC_INSN = 10;
0121:
0122: /**
0123: * The type of the LDC_W and LDC2_W instructions.
0124: */
0125: static final int LDCW_INSN = 11;
0126:
0127: /**
0128: * The type of the IINC instruction.
0129: */
0130: static final int IINC_INSN = 12;
0131:
0132: /**
0133: * The type of the TABLESWITCH instruction.
0134: */
0135: static final int TABL_INSN = 13;
0136:
0137: /**
0138: * The type of the LOOKUPSWITCH instruction.
0139: */
0140: static final int LOOK_INSN = 14;
0141:
0142: /**
0143: * The type of the MULTIANEWARRAY instruction.
0144: */
0145: static final int MANA_INSN = 15;
0146:
0147: /**
0148: * The type of the WIDE instruction.
0149: */
0150: static final int WIDE_INSN = 16;
0151:
0152: /**
0153: * The instruction types of all JVM opcodes.
0154: */
0155: static final byte[] TYPE;
0156:
0157: /**
0158: * The type of CONSTANT_Class constant pool items.
0159: */
0160: static final int CLASS = 7;
0161:
0162: /**
0163: * The type of CONSTANT_Fieldref constant pool items.
0164: */
0165: static final int FIELD = 9;
0166:
0167: /**
0168: * The type of CONSTANT_Methodref constant pool items.
0169: */
0170: static final int METH = 10;
0171:
0172: /**
0173: * The type of CONSTANT_InterfaceMethodref constant pool items.
0174: */
0175: static final int IMETH = 11;
0176:
0177: /**
0178: * The type of CONSTANT_String constant pool items.
0179: */
0180: static final int STR = 8;
0181:
0182: /**
0183: * The type of CONSTANT_Integer constant pool items.
0184: */
0185: static final int INT = 3;
0186:
0187: /**
0188: * The type of CONSTANT_Float constant pool items.
0189: */
0190: static final int FLOAT = 4;
0191:
0192: /**
0193: * The type of CONSTANT_Long constant pool items.
0194: */
0195: static final int LONG = 5;
0196:
0197: /**
0198: * The type of CONSTANT_Double constant pool items.
0199: */
0200: static final int DOUBLE = 6;
0201:
0202: /**
0203: * The type of CONSTANT_NameAndType constant pool items.
0204: */
0205: static final int NAME_TYPE = 12;
0206:
0207: /**
0208: * The type of CONSTANT_Utf8 constant pool items.
0209: */
0210: static final int UTF8 = 1;
0211:
0212: /**
0213: * Normal type Item stored in the ClassWriter {@link ClassWriter#typeTable},
0214: * instead of the constant pool, in order to avoid clashes with normal
0215: * constant pool items in the ClassWriter constant pool's hash table.
0216: */
0217: static final int TYPE_NORMAL = 13;
0218:
0219: /**
0220: * Uninitialized type Item stored in the ClassWriter
0221: * {@link ClassWriter#typeTable}, instead of the constant pool, in order to
0222: * avoid clashes with normal constant pool items in the ClassWriter constant
0223: * pool's hash table.
0224: */
0225: static final int TYPE_UNINIT = 14;
0226:
0227: /**
0228: * Merged type Item stored in the ClassWriter {@link ClassWriter#typeTable},
0229: * instead of the constant pool, in order to avoid clashes with normal
0230: * constant pool items in the ClassWriter constant pool's hash table.
0231: */
0232: static final int TYPE_MERGED = 15;
0233:
0234: /**
0235: * The class reader from which this class writer was constructed, if any.
0236: */
0237: ClassReader cr;
0238:
0239: /**
0240: * Minor and major version numbers of the class to be generated.
0241: */
0242: int version;
0243:
0244: /**
0245: * Index of the next item to be added in the constant pool.
0246: */
0247: int index;
0248:
0249: /**
0250: * The constant pool of this class.
0251: */
0252: final ByteVector pool;
0253:
0254: /**
0255: * The constant pool's hash table data.
0256: */
0257: Item[] items;
0258:
0259: /**
0260: * The threshold of the constant pool's hash table.
0261: */
0262: int threshold;
0263:
0264: /**
0265: * A reusable key used to look for items in the {@link #items} hash table.
0266: */
0267: final Item key;
0268:
0269: /**
0270: * A reusable key used to look for items in the {@link #items} hash table.
0271: */
0272: final Item key2;
0273:
0274: /**
0275: * A reusable key used to look for items in the {@link #items} hash table.
0276: */
0277: final Item key3;
0278:
0279: /**
0280: * A type table used to temporarily store internal names that will not
0281: * necessarily be stored in the constant pool. This type table is used by
0282: * the control flow and data flow analysis algorithm used to compute stack
0283: * map frames from scratch. This array associates to each index <tt>i</tt>
0284: * the Item whose index is <tt>i</tt>. All Item objects stored in this
0285: * array are also stored in the {@link #items} hash table. These two arrays
0286: * allow to retrieve an Item from its index or, conversely, to get the index
0287: * of an Item from its value. Each Item stores an internal name in its
0288: * {@link Item#strVal1} field.
0289: */
0290: Item[] typeTable;
0291:
0292: /**
0293: * Number of elements in the {@link #typeTable} array.
0294: */
0295: private short typeCount;
0296:
0297: /**
0298: * The access flags of this class.
0299: */
0300: private int access;
0301:
0302: /**
0303: * The constant pool item that contains the internal name of this class.
0304: */
0305: private int name;
0306:
0307: /**
0308: * The internal name of this class.
0309: */
0310: String this Name;
0311:
0312: /**
0313: * The constant pool item that contains the signature of this class.
0314: */
0315: private int signature;
0316:
0317: /**
0318: * The constant pool item that contains the internal name of the super class
0319: * of this class.
0320: */
0321: private int super Name;
0322:
0323: /**
0324: * Number of interfaces implemented or extended by this class or interface.
0325: */
0326: private int interfaceCount;
0327:
0328: /**
0329: * The interfaces implemented or extended by this class or interface. More
0330: * precisely, this array contains the indexes of the constant pool items
0331: * that contain the internal names of these interfaces.
0332: */
0333: private int[] interfaces;
0334:
0335: /**
0336: * The index of the constant pool item that contains the name of the source
0337: * file from which this class was compiled.
0338: */
0339: private int sourceFile;
0340:
0341: /**
0342: * The SourceDebug attribute of this class.
0343: */
0344: private ByteVector sourceDebug;
0345:
0346: /**
0347: * The constant pool item that contains the name of the enclosing class of
0348: * this class.
0349: */
0350: private int enclosingMethodOwner;
0351:
0352: /**
0353: * The constant pool item that contains the name and descriptor of the
0354: * enclosing method of this class.
0355: */
0356: private int enclosingMethod;
0357:
0358: /**
0359: * The runtime visible annotations of this class.
0360: */
0361: private AnnotationWriter anns;
0362:
0363: /**
0364: * The runtime invisible annotations of this class.
0365: */
0366: private AnnotationWriter ianns;
0367:
0368: /**
0369: * The non standard attributes of this class.
0370: */
0371: private Attribute attrs;
0372:
0373: /**
0374: * The number of entries in the InnerClasses attribute.
0375: */
0376: private int innerClassesCount;
0377:
0378: /**
0379: * The InnerClasses attribute.
0380: */
0381: private ByteVector innerClasses;
0382:
0383: /**
0384: * The fields of this class. These fields are stored in a linked list of
0385: * {@link FieldWriter} objects, linked to each other by their
0386: * {@link FieldWriter#next} field. This field stores the first element of
0387: * this list.
0388: */
0389: FieldWriter firstField;
0390:
0391: /**
0392: * The fields of this class. These fields are stored in a linked list of
0393: * {@link FieldWriter} objects, linked to each other by their
0394: * {@link FieldWriter#next} field. This field stores the last element of
0395: * this list.
0396: */
0397: FieldWriter lastField;
0398:
0399: /**
0400: * The methods of this class. These methods are stored in a linked list of
0401: * {@link MethodWriter} objects, linked to each other by their
0402: * {@link MethodWriter#next} field. This field stores the first element of
0403: * this list.
0404: */
0405: MethodWriter firstMethod;
0406:
0407: /**
0408: * The methods of this class. These methods are stored in a linked list of
0409: * {@link MethodWriter} objects, linked to each other by their
0410: * {@link MethodWriter#next} field. This field stores the last element of
0411: * this list.
0412: */
0413: MethodWriter lastMethod;
0414:
0415: /**
0416: * <tt>true</tt> if the maximum stack size and number of local variables
0417: * must be automatically computed.
0418: */
0419: private final boolean computeMaxs;
0420:
0421: /**
0422: * <tt>true</tt> if the stack map frames must be recomputed from scratch.
0423: */
0424: private final boolean computeFrames;
0425:
0426: /**
0427: * <tt>true</tt> if the stack map tables of this class are invalid. The
0428: * {@link MethodWriter#resizeInstructions} method cannot transform existing
0429: * stack map tables, and so produces potentially invalid classes when it is
0430: * executed. In this case the class is reread and rewritten with the
0431: * {@link #COMPUTE_FRAMES} option (the resizeInstructions method can resize
0432: * stack map tables when this option is used).
0433: */
0434: boolean invalidFrames;
0435:
0436: // ------------------------------------------------------------------------
0437: // Static initializer
0438: // ------------------------------------------------------------------------
0439:
0440: /**
0441: * Computes the instruction types of JVM opcodes.
0442: */
0443: static {
0444: int i;
0445: byte[] b = new byte[220];
0446: String s = "AAAAAAAAAAAAAAAABCKLLDDDDDEEEEEEEEEEEEEEEEEEEEAAAAAAAADD"
0447: + "DDDEEEEEEEEEEEEEEEEEEEEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
0448: + "AAAAAAAAAAAAAAAAAMAAAAAAAAAAAAAAAAAAAAIIIIIIIIIIIIIIIIDNOAA"
0449: + "AAAAGGGGGGGHAFBFAAFFAAQPIIJJIIIIIIIIIIIIIIIIII";
0450: for (i = 0; i < b.length; ++i) {
0451: b[i] = (byte) (s.charAt(i) - 'A');
0452: }
0453: TYPE = b;
0454:
0455: // code to generate the above string
0456: //
0457: // // SBYTE_INSN instructions
0458: // b[Constants.NEWARRAY] = SBYTE_INSN;
0459: // b[Constants.BIPUSH] = SBYTE_INSN;
0460: //
0461: // // SHORT_INSN instructions
0462: // b[Constants.SIPUSH] = SHORT_INSN;
0463: //
0464: // // (IMPL)VAR_INSN instructions
0465: // b[Constants.RET] = VAR_INSN;
0466: // for (i = Constants.ILOAD; i <= Constants.ALOAD; ++i) {
0467: // b[i] = VAR_INSN;
0468: // }
0469: // for (i = Constants.ISTORE; i <= Constants.ASTORE; ++i) {
0470: // b[i] = VAR_INSN;
0471: // }
0472: // for (i = 26; i <= 45; ++i) { // ILOAD_0 to ALOAD_3
0473: // b[i] = IMPLVAR_INSN;
0474: // }
0475: // for (i = 59; i <= 78; ++i) { // ISTORE_0 to ASTORE_3
0476: // b[i] = IMPLVAR_INSN;
0477: // }
0478: //
0479: // // TYPE_INSN instructions
0480: // b[Constants.NEW] = TYPE_INSN;
0481: // b[Constants.ANEWARRAY] = TYPE_INSN;
0482: // b[Constants.CHECKCAST] = TYPE_INSN;
0483: // b[Constants.INSTANCEOF] = TYPE_INSN;
0484: //
0485: // // (Set)FIELDORMETH_INSN instructions
0486: // for (i = Constants.GETSTATIC; i <= Constants.INVOKESTATIC; ++i) {
0487: // b[i] = FIELDORMETH_INSN;
0488: // }
0489: // b[Constants.INVOKEINTERFACE] = ITFMETH_INSN;
0490: //
0491: // // LABEL(W)_INSN instructions
0492: // for (i = Constants.IFEQ; i <= Constants.JSR; ++i) {
0493: // b[i] = LABEL_INSN;
0494: // }
0495: // b[Constants.IFNULL] = LABEL_INSN;
0496: // b[Constants.IFNONNULL] = LABEL_INSN;
0497: // b[200] = LABELW_INSN; // GOTO_W
0498: // b[201] = LABELW_INSN; // JSR_W
0499: // // temporary opcodes used internally by ASM - see Label and
0500: // MethodWriter
0501: // for (i = 202; i < 220; ++i) {
0502: // b[i] = LABEL_INSN;
0503: // }
0504: //
0505: // // LDC(_W) instructions
0506: // b[Constants.LDC] = LDC_INSN;
0507: // b[19] = LDCW_INSN; // LDC_W
0508: // b[20] = LDCW_INSN; // LDC2_W
0509: //
0510: // // special instructions
0511: // b[Constants.IINC] = IINC_INSN;
0512: // b[Constants.TABLESWITCH] = TABL_INSN;
0513: // b[Constants.LOOKUPSWITCH] = LOOK_INSN;
0514: // b[Constants.MULTIANEWARRAY] = MANA_INSN;
0515: // b[196] = WIDE_INSN; // WIDE
0516: //
0517: // for (i = 0; i < b.length; ++i) {
0518: // System.err.print((char)('A' + b[i]));
0519: // }
0520: // System.err.println();
0521: }
0522:
0523: // ------------------------------------------------------------------------
0524: // Constructor
0525: // ------------------------------------------------------------------------
0526:
0527: /**
0528: * Constructs a new {@link ClassWriter} object.
0529: *
0530: * @param flags option flags that can be used to modify the default behavior
0531: * of this class. See {@link #COMPUTE_MAXS}, {@link #COMPUTE_FRAMES}.
0532: */
0533: public ClassWriter(final int flags) {
0534: index = 1;
0535: pool = new ByteVector();
0536: items = new Item[256];
0537: threshold = (int) (0.75d * items.length);
0538: key = new Item();
0539: key2 = new Item();
0540: key3 = new Item();
0541: this .computeMaxs = (flags & COMPUTE_MAXS) != 0;
0542: this .computeFrames = (flags & COMPUTE_FRAMES) != 0;
0543: }
0544:
0545: /**
0546: * Constructs a new {@link ClassWriter} object and enables optimizations for
0547: * "mostly add" bytecode transformations. These optimizations are the
0548: * following:
0549: *
0550: * <ul> <li>The constant pool from the original class is copied as is in
0551: * the new class, which saves time. New constant pool entries will be added
0552: * at the end if necessary, but unused constant pool entries <i>won't be
0553: * removed</i>.</li> <li>Methods that are not transformed are copied as
0554: * is in the new class, directly from the original class bytecode (i.e.
0555: * without emitting visit events for all the method instructions), which
0556: * saves a <i>lot</i> of time. Untransformed methods are detected by the
0557: * fact that the {@link ClassReader} receives {@link MethodVisitor} objects
0558: * that come from a {@link ClassWriter} (and not from a custom
0559: * {@link ClassAdapter} or any other {@link ClassVisitor} instance).</li>
0560: * </ul>
0561: *
0562: * @param classReader the {@link ClassReader} used to read the original
0563: * class. It will be used to copy the entire constant pool from the
0564: * original class and also to copy other fragments of original
0565: * bytecode where applicable.
0566: * @param flags option flags that can be used to modify the default behavior
0567: * of this class. See {@link #COMPUTE_MAXS}, {@link #COMPUTE_FRAMES}.
0568: */
0569: public ClassWriter(final ClassReader classReader, final int flags) {
0570: this (flags);
0571: classReader.copyPool(this );
0572: this .cr = classReader;
0573: }
0574:
0575: // ------------------------------------------------------------------------
0576: // Implementation of the ClassVisitor interface
0577: // ------------------------------------------------------------------------
0578:
0579: public void visit(final int version, final int access,
0580: final String name, final String signature,
0581: final String super Name, final String[] interfaces) {
0582: this .version = version;
0583: this .access = access;
0584: this .name = newClass(name);
0585: this Name = name;
0586: if (ClassReader.SIGNATURES && signature != null) {
0587: this .signature = newUTF8(signature);
0588: }
0589: this .super Name = super Name == null ? 0 : newClass(super Name);
0590: if (interfaces != null && interfaces.length > 0) {
0591: interfaceCount = interfaces.length;
0592: this .interfaces = new int[interfaceCount];
0593: for (int i = 0; i < interfaceCount; ++i) {
0594: this .interfaces[i] = newClass(interfaces[i]);
0595: }
0596: }
0597: }
0598:
0599: public void visitSource(final String file, final String debug) {
0600: if (file != null) {
0601: sourceFile = newUTF8(file);
0602: }
0603: if (debug != null) {
0604: sourceDebug = new ByteVector().putUTF8(debug);
0605: }
0606: }
0607:
0608: public void visitOuterClass(final String owner, final String name,
0609: final String desc) {
0610: enclosingMethodOwner = newClass(owner);
0611: if (name != null && desc != null) {
0612: enclosingMethod = newNameType(name, desc);
0613: }
0614: }
0615:
0616: public AnnotationVisitor visitAnnotation(final String desc,
0617: final boolean visible) {
0618: if (!ClassReader.ANNOTATIONS) {
0619: return null;
0620: }
0621: ByteVector bv = new ByteVector();
0622: // write type, and reserve space for values count
0623: bv.putShort(newUTF8(desc)).putShort(0);
0624: AnnotationWriter aw = new AnnotationWriter(this , true, bv, bv,
0625: 2);
0626: if (visible) {
0627: aw.next = anns;
0628: anns = aw;
0629: } else {
0630: aw.next = ianns;
0631: ianns = aw;
0632: }
0633: return aw;
0634: }
0635:
0636: public void visitAttribute(final Attribute attr) {
0637: attr.next = attrs;
0638: attrs = attr;
0639: }
0640:
0641: public void visitInnerClass(final String name,
0642: final String outerName, final String innerName,
0643: final int access) {
0644: if (innerClasses == null) {
0645: innerClasses = new ByteVector();
0646: }
0647: ++innerClassesCount;
0648: innerClasses.putShort(name == null ? 0 : newClass(name));
0649: innerClasses.putShort(outerName == null ? 0
0650: : newClass(outerName));
0651: innerClasses.putShort(innerName == null ? 0
0652: : newUTF8(innerName));
0653: innerClasses.putShort(access);
0654: }
0655:
0656: public FieldVisitor visitField(final int access, final String name,
0657: final String desc, final String signature,
0658: final Object value) {
0659: return new FieldWriter(this , access, name, desc, signature,
0660: value);
0661: }
0662:
0663: public MethodVisitor visitMethod(final int access,
0664: final String name, final String desc,
0665: final String signature, final String[] exceptions) {
0666: return new MethodWriter(this , access, name, desc, signature,
0667: exceptions, computeMaxs, computeFrames);
0668: }
0669:
0670: public void visitEnd() {
0671: }
0672:
0673: // ------------------------------------------------------------------------
0674: // Other public methods
0675: // ------------------------------------------------------------------------
0676:
0677: /**
0678: * Returns the bytecode of the class that was build with this class writer.
0679: *
0680: * @return the bytecode of the class that was build with this class writer.
0681: */
0682: public byte[] toByteArray() {
0683: // computes the real size of the bytecode of this class
0684: int size = 24 + 2 * interfaceCount;
0685: int nbFields = 0;
0686: FieldWriter fb = firstField;
0687: while (fb != null) {
0688: ++nbFields;
0689: size += fb.getSize();
0690: fb = fb.next;
0691: }
0692: int nbMethods = 0;
0693: MethodWriter mb = firstMethod;
0694: while (mb != null) {
0695: ++nbMethods;
0696: size += mb.getSize();
0697: mb = mb.next;
0698: }
0699: int attributeCount = 0;
0700: if (ClassReader.SIGNATURES && signature != 0) {
0701: ++attributeCount;
0702: size += 8;
0703: newUTF8("Signature");
0704: }
0705: if (sourceFile != 0) {
0706: ++attributeCount;
0707: size += 8;
0708: newUTF8("SourceFile");
0709: }
0710: if (sourceDebug != null) {
0711: ++attributeCount;
0712: size += sourceDebug.length + 4;
0713: newUTF8("SourceDebugExtension");
0714: }
0715: if (enclosingMethodOwner != 0) {
0716: ++attributeCount;
0717: size += 10;
0718: newUTF8("EnclosingMethod");
0719: }
0720: if ((access & Opcodes.ACC_DEPRECATED) != 0) {
0721: ++attributeCount;
0722: size += 6;
0723: newUTF8("Deprecated");
0724: }
0725: if ((access & Opcodes.ACC_SYNTHETIC) != 0
0726: && (version & 0xffff) < Opcodes.V1_5) {
0727: ++attributeCount;
0728: size += 6;
0729: newUTF8("Synthetic");
0730: }
0731: if (innerClasses != null) {
0732: ++attributeCount;
0733: size += 8 + innerClasses.length;
0734: newUTF8("InnerClasses");
0735: }
0736: if (ClassReader.ANNOTATIONS && anns != null) {
0737: ++attributeCount;
0738: size += 8 + anns.getSize();
0739: newUTF8("RuntimeVisibleAnnotations");
0740: }
0741: if (ClassReader.ANNOTATIONS && ianns != null) {
0742: ++attributeCount;
0743: size += 8 + ianns.getSize();
0744: newUTF8("RuntimeInvisibleAnnotations");
0745: }
0746: if (attrs != null) {
0747: attributeCount += attrs.getCount();
0748: size += attrs.getSize(this , null, 0, -1, -1);
0749: }
0750: size += pool.length;
0751: // allocates a byte vector of this size, in order to avoid unnecessary
0752: // arraycopy operations in the ByteVector.enlarge() method
0753: ByteVector out = new ByteVector(size);
0754: out.putInt(0xCAFEBABE).putInt(version);
0755: out.putShort(index).putByteArray(pool.data, 0, pool.length);
0756: out.putShort(access).putShort(name).putShort(super Name);
0757: out.putShort(interfaceCount);
0758: for (int i = 0; i < interfaceCount; ++i) {
0759: out.putShort(interfaces[i]);
0760: }
0761: out.putShort(nbFields);
0762: fb = firstField;
0763: while (fb != null) {
0764: fb.put(out);
0765: fb = fb.next;
0766: }
0767: out.putShort(nbMethods);
0768: mb = firstMethod;
0769: while (mb != null) {
0770: mb.put(out);
0771: mb = mb.next;
0772: }
0773: out.putShort(attributeCount);
0774: if (ClassReader.SIGNATURES && signature != 0) {
0775: out.putShort(newUTF8("Signature")).putInt(2).putShort(
0776: signature);
0777: }
0778: if (sourceFile != 0) {
0779: out.putShort(newUTF8("SourceFile")).putInt(2).putShort(
0780: sourceFile);
0781: }
0782: if (sourceDebug != null) {
0783: int len = sourceDebug.length - 2;
0784: out.putShort(newUTF8("SourceDebugExtension")).putInt(len);
0785: out.putByteArray(sourceDebug.data, 2, len);
0786: }
0787: if (enclosingMethodOwner != 0) {
0788: out.putShort(newUTF8("EnclosingMethod")).putInt(4);
0789: out.putShort(enclosingMethodOwner)
0790: .putShort(enclosingMethod);
0791: }
0792: if ((access & Opcodes.ACC_DEPRECATED) != 0) {
0793: out.putShort(newUTF8("Deprecated")).putInt(0);
0794: }
0795: if ((access & Opcodes.ACC_SYNTHETIC) != 0
0796: && (version & 0xffff) < Opcodes.V1_5) {
0797: out.putShort(newUTF8("Synthetic")).putInt(0);
0798: }
0799: if (innerClasses != null) {
0800: out.putShort(newUTF8("InnerClasses"));
0801: out.putInt(innerClasses.length + 2).putShort(
0802: innerClassesCount);
0803: out.putByteArray(innerClasses.data, 0, innerClasses.length);
0804: }
0805: if (ClassReader.ANNOTATIONS && anns != null) {
0806: out.putShort(newUTF8("RuntimeVisibleAnnotations"));
0807: anns.put(out);
0808: }
0809: if (ClassReader.ANNOTATIONS && ianns != null) {
0810: out.putShort(newUTF8("RuntimeInvisibleAnnotations"));
0811: ianns.put(out);
0812: }
0813: if (attrs != null) {
0814: attrs.put(this , null, 0, -1, -1, out);
0815: }
0816: if (invalidFrames) {
0817: ClassWriter cw = new ClassWriter(COMPUTE_FRAMES);
0818: new ClassReader(out.data).accept(cw,
0819: ClassReader.SKIP_FRAMES);
0820: return cw.toByteArray();
0821: }
0822: return out.data;
0823: }
0824:
0825: // ------------------------------------------------------------------------
0826: // Utility methods: constant pool management
0827: // ------------------------------------------------------------------------
0828:
0829: /**
0830: * Adds a number or string constant to the constant pool of the class being
0831: * build. Does nothing if the constant pool already contains a similar item.
0832: *
0833: * @param cst the value of the constant to be added to the constant pool.
0834: * This parameter must be an {@link Integer}, a {@link Float}, a
0835: * {@link Long}, a {@link Double}, a {@link String} or a
0836: * {@link Type}.
0837: * @return a new or already existing constant item with the given value.
0838: */
0839: Item newConstItem(final Object cst) {
0840: if (cst instanceof Integer) {
0841: int val = ((Integer) cst).intValue();
0842: return newInteger(val);
0843: } else if (cst instanceof Byte) {
0844: int val = ((Byte) cst).intValue();
0845: return newInteger(val);
0846: } else if (cst instanceof Character) {
0847: int val = ((Character) cst).charValue();
0848: return newInteger(val);
0849: } else if (cst instanceof Short) {
0850: int val = ((Short) cst).intValue();
0851: return newInteger(val);
0852: } else if (cst instanceof Boolean) {
0853: int val = ((Boolean) cst).booleanValue() ? 1 : 0;
0854: return newInteger(val);
0855: } else if (cst instanceof Float) {
0856: float val = ((Float) cst).floatValue();
0857: return newFloat(val);
0858: } else if (cst instanceof Long) {
0859: long val = ((Long) cst).longValue();
0860: return newLong(val);
0861: } else if (cst instanceof Double) {
0862: double val = ((Double) cst).doubleValue();
0863: return newDouble(val);
0864: } else if (cst instanceof String) {
0865: return newString((String) cst);
0866: } else if (cst instanceof Type) {
0867: Type t = (Type) cst;
0868: return newClassItem(t.getSort() == Type.OBJECT ? t
0869: .getInternalName() : t.getDescriptor());
0870: } else {
0871: throw new IllegalArgumentException("value " + cst);
0872: }
0873: }
0874:
0875: /**
0876: * Adds a number or string constant to the constant pool of the class being
0877: * build. Does nothing if the constant pool already contains a similar item.
0878: * <i>This method is intended for {@link Attribute} sub classes, and is
0879: * normally not needed by class generators or adapters.</i>
0880: *
0881: * @param cst the value of the constant to be added to the constant pool.
0882: * This parameter must be an {@link Integer}, a {@link Float}, a
0883: * {@link Long}, a {@link Double} or a {@link String}.
0884: * @return the index of a new or already existing constant item with the
0885: * given value.
0886: */
0887: public int newConst(final Object cst) {
0888: return newConstItem(cst).index;
0889: }
0890:
0891: /**
0892: * Adds an UTF8 string to the constant pool of the class being build. Does
0893: * nothing if the constant pool already contains a similar item. <i>This
0894: * method is intended for {@link Attribute} sub classes, and is normally not
0895: * needed by class generators or adapters.</i>
0896: *
0897: * @param value the String value.
0898: * @return the index of a new or already existing UTF8 item.
0899: */
0900: public int newUTF8(final String value) {
0901: key.set(UTF8, value, null, null);
0902: Item result = get(key);
0903: if (result == null) {
0904: pool.putByte(UTF8).putUTF8(value);
0905: result = new Item(index++, key);
0906: put(result);
0907: }
0908: return result.index;
0909: }
0910:
0911: /**
0912: * Adds a class reference to the constant pool of the class being build.
0913: * Does nothing if the constant pool already contains a similar item.
0914: * <i>This method is intended for {@link Attribute} sub classes, and is
0915: * normally not needed by class generators or adapters.</i>
0916: *
0917: * @param value the internal name of the class.
0918: * @return a new or already existing class reference item.
0919: */
0920: Item newClassItem(final String value) {
0921: key2.set(CLASS, value, null, null);
0922: Item result = get(key2);
0923: if (result == null) {
0924: pool.put12(CLASS, newUTF8(value));
0925: result = new Item(index++, key2);
0926: put(result);
0927: }
0928: return result;
0929: }
0930:
0931: /**
0932: * Adds a class reference to the constant pool of the class being build.
0933: * Does nothing if the constant pool already contains a similar item.
0934: * <i>This method is intended for {@link Attribute} sub classes, and is
0935: * normally not needed by class generators or adapters.</i>
0936: *
0937: * @param value the internal name of the class.
0938: * @return the index of a new or already existing class reference item.
0939: */
0940: public int newClass(final String value) {
0941: return newClassItem(value).index;
0942: }
0943:
0944: /**
0945: * Adds a field reference to the constant pool of the class being build.
0946: * Does nothing if the constant pool already contains a similar item.
0947: *
0948: * @param owner the internal name of the field's owner class.
0949: * @param name the field's name.
0950: * @param desc the field's descriptor.
0951: * @return a new or already existing field reference item.
0952: */
0953: Item newFieldItem(final String owner, final String name,
0954: final String desc) {
0955: key3.set(FIELD, owner, name, desc);
0956: Item result = get(key3);
0957: if (result == null) {
0958: put122(FIELD, newClass(owner), newNameType(name, desc));
0959: result = new Item(index++, key3);
0960: put(result);
0961: }
0962: return result;
0963: }
0964:
0965: /**
0966: * Adds a field reference to the constant pool of the class being build.
0967: * Does nothing if the constant pool already contains a similar item.
0968: * <i>This method is intended for {@link Attribute} sub classes, and is
0969: * normally not needed by class generators or adapters.</i>
0970: *
0971: * @param owner the internal name of the field's owner class.
0972: * @param name the field's name.
0973: * @param desc the field's descriptor.
0974: * @return the index of a new or already existing field reference item.
0975: */
0976: public int newField(final String owner, final String name,
0977: final String desc) {
0978: return newFieldItem(owner, name, desc).index;
0979: }
0980:
0981: /**
0982: * Adds a method reference to the constant pool of the class being build.
0983: * Does nothing if the constant pool already contains a similar item.
0984: *
0985: * @param owner the internal name of the method's owner class.
0986: * @param name the method's name.
0987: * @param desc the method's descriptor.
0988: * @param itf <tt>true</tt> if <tt>owner</tt> is an interface.
0989: * @return a new or already existing method reference item.
0990: */
0991: Item newMethodItem(final String owner, final String name,
0992: final String desc, final boolean itf) {
0993: int type = itf ? IMETH : METH;
0994: key3.set(type, owner, name, desc);
0995: Item result = get(key3);
0996: if (result == null) {
0997: put122(type, newClass(owner), newNameType(name, desc));
0998: result = new Item(index++, key3);
0999: put(result);
1000: }
1001: return result;
1002: }
1003:
1004: /**
1005: * Adds a method reference to the constant pool of the class being build.
1006: * Does nothing if the constant pool already contains a similar item.
1007: * <i>This method is intended for {@link Attribute} sub classes, and is
1008: * normally not needed by class generators or adapters.</i>
1009: *
1010: * @param owner the internal name of the method's owner class.
1011: * @param name the method's name.
1012: * @param desc the method's descriptor.
1013: * @param itf <tt>true</tt> if <tt>owner</tt> is an interface.
1014: * @return the index of a new or already existing method reference item.
1015: */
1016: public int newMethod(final String owner, final String name,
1017: final String desc, final boolean itf) {
1018: return newMethodItem(owner, name, desc, itf).index;
1019: }
1020:
1021: /**
1022: * Adds an integer to the constant pool of the class being build. Does
1023: * nothing if the constant pool already contains a similar item.
1024: *
1025: * @param value the int value.
1026: * @return a new or already existing int item.
1027: */
1028: Item newInteger(final int value) {
1029: key.set(value);
1030: Item result = get(key);
1031: if (result == null) {
1032: pool.putByte(INT).putInt(value);
1033: result = new Item(index++, key);
1034: put(result);
1035: }
1036: return result;
1037: }
1038:
1039: /**
1040: * Adds a float to the constant pool of the class being build. Does nothing
1041: * if the constant pool already contains a similar item.
1042: *
1043: * @param value the float value.
1044: * @return a new or already existing float item.
1045: */
1046: Item newFloat(final float value) {
1047: key.set(value);
1048: Item result = get(key);
1049: if (result == null) {
1050: pool.putByte(FLOAT).putInt(key.intVal);
1051: result = new Item(index++, key);
1052: put(result);
1053: }
1054: return result;
1055: }
1056:
1057: /**
1058: * Adds a long to the constant pool of the class being build. Does nothing
1059: * if the constant pool already contains a similar item.
1060: *
1061: * @param value the long value.
1062: * @return a new or already existing long item.
1063: */
1064: Item newLong(final long value) {
1065: key.set(value);
1066: Item result = get(key);
1067: if (result == null) {
1068: pool.putByte(LONG).putLong(value);
1069: result = new Item(index, key);
1070: put(result);
1071: index += 2;
1072: }
1073: return result;
1074: }
1075:
1076: /**
1077: * Adds a double to the constant pool of the class being build. Does nothing
1078: * if the constant pool already contains a similar item.
1079: *
1080: * @param value the double value.
1081: * @return a new or already existing double item.
1082: */
1083: Item newDouble(final double value) {
1084: key.set(value);
1085: Item result = get(key);
1086: if (result == null) {
1087: pool.putByte(DOUBLE).putLong(key.longVal);
1088: result = new Item(index, key);
1089: put(result);
1090: index += 2;
1091: }
1092: return result;
1093: }
1094:
1095: /**
1096: * Adds a string to the constant pool of the class being build. Does nothing
1097: * if the constant pool already contains a similar item.
1098: *
1099: * @param value the String value.
1100: * @return a new or already existing string item.
1101: */
1102: private Item newString(final String value) {
1103: key2.set(STR, value, null, null);
1104: Item result = get(key2);
1105: if (result == null) {
1106: pool.put12(STR, newUTF8(value));
1107: result = new Item(index++, key2);
1108: put(result);
1109: }
1110: return result;
1111: }
1112:
1113: /**
1114: * Adds a name and type to the constant pool of the class being build. Does
1115: * nothing if the constant pool already contains a similar item. <i>This
1116: * method is intended for {@link Attribute} sub classes, and is normally not
1117: * needed by class generators or adapters.</i>
1118: *
1119: * @param name a name.
1120: * @param desc a type descriptor.
1121: * @return the index of a new or already existing name and type item.
1122: */
1123: public int newNameType(final String name, final String desc) {
1124: key2.set(NAME_TYPE, name, desc, null);
1125: Item result = get(key2);
1126: if (result == null) {
1127: put122(NAME_TYPE, newUTF8(name), newUTF8(desc));
1128: result = new Item(index++, key2);
1129: put(result);
1130: }
1131: return result.index;
1132: }
1133:
1134: /**
1135: * Adds the given internal name to {@link #typeTable} and returns its index.
1136: * Does nothing if the type table already contains this internal name.
1137: *
1138: * @param type the internal name to be added to the type table.
1139: * @return the index of this internal name in the type table.
1140: */
1141: int addType(final String type) {
1142: key.set(TYPE_NORMAL, type, null, null);
1143: Item result = get(key);
1144: if (result == null) {
1145: result = addType(key);
1146: }
1147: return result.index;
1148: }
1149:
1150: /**
1151: * Adds the given "uninitialized" type to {@link #typeTable} and returns its
1152: * index. This method is used for UNINITIALIZED types, made of an internal
1153: * name and a bytecode offset.
1154: *
1155: * @param type the internal name to be added to the type table.
1156: * @param offset the bytecode offset of the NEW instruction that created
1157: * this UNINITIALIZED type value.
1158: * @return the index of this internal name in the type table.
1159: */
1160: int addUninitializedType(final String type, final int offset) {
1161: key.type = TYPE_UNINIT;
1162: key.intVal = offset;
1163: key.strVal1 = type;
1164: key.hashCode = 0x7FFFFFFF & (TYPE_UNINIT + type.hashCode() + offset);
1165: Item result = get(key);
1166: if (result == null) {
1167: result = addType(key);
1168: }
1169: return result.index;
1170: }
1171:
1172: /**
1173: * Adds the given Item to {@link #typeTable}.
1174: *
1175: * @param item the value to be added to the type table.
1176: * @return the added Item, which a new Item instance with the same value as
1177: * the given Item.
1178: */
1179: private Item addType(final Item item) {
1180: ++typeCount;
1181: Item result = new Item(typeCount, key);
1182: put(result);
1183: if (typeTable == null) {
1184: typeTable = new Item[16];
1185: }
1186: if (typeCount == typeTable.length) {
1187: Item[] newTable = new Item[2 * typeTable.length];
1188: System.arraycopy(typeTable, 0, newTable, 0,
1189: typeTable.length);
1190: typeTable = newTable;
1191: }
1192: typeTable[typeCount] = result;
1193: return result;
1194: }
1195:
1196: /**
1197: * Returns the index of the common super type of the two given types. This
1198: * method calls {@link #getCommonSuperClass} and caches the result in the
1199: * {@link #items} hash table to speedup future calls with the same
1200: * parameters.
1201: *
1202: * @param type1 index of an internal name in {@link #typeTable}.
1203: * @param type2 index of an internal name in {@link #typeTable}.
1204: * @return the index of the common super type of the two given types.
1205: */
1206: int getMergedType(final int type1, final int type2) {
1207: key2.type = TYPE_MERGED;
1208: key2.longVal = type1 | (((long) type2) << 32);
1209: key2.hashCode = 0x7FFFFFFF & (TYPE_MERGED + type1 + type2);
1210: Item result = get(key2);
1211: if (result == null) {
1212: String t = typeTable[type1].strVal1;
1213: String u = typeTable[type2].strVal1;
1214: key2.intVal = addType(getCommonSuperClass(t, u));
1215: result = new Item((short) 0, key2);
1216: put(result);
1217: }
1218: return result.intVal;
1219: }
1220:
1221: /**
1222: * Returns the common super type of the two given types. The default
1223: * implementation of this method <i>loads<i> the two given classes and uses
1224: * the java.lang.Class methods to find the common super class. It can be
1225: * overridden to compute this common super type in other ways, in particular
1226: * without actually loading any class, or to take into account the class
1227: * that is currently being generated by this ClassWriter, which can of
1228: * course not be loaded since it is under construction.
1229: *
1230: * @param type1 the internal name of a class.
1231: * @param type2 the internal name of another class.
1232: * @return the internal name of the common super class of the two given
1233: * classes.
1234: */
1235: protected String getCommonSuperClass(final String type1,
1236: final String type2) {
1237: Class c, d;
1238: try {
1239: c = Class.forName(type1.replace('/', '.'));
1240: d = Class.forName(type2.replace('/', '.'));
1241: } catch (Exception e) {
1242: throw new RuntimeException(e.toString());
1243: }
1244: if (c.isAssignableFrom(d)) {
1245: return type1;
1246: }
1247: if (d.isAssignableFrom(c)) {
1248: return type2;
1249: }
1250: if (c.isInterface() || d.isInterface()) {
1251: return "java/lang/Object";
1252: } else {
1253: do {
1254: c = c.getSuperclass();
1255: } while (!c.isAssignableFrom(d));
1256: return c.getName().replace('.', '/');
1257: }
1258: }
1259:
1260: /**
1261: * Returns the constant pool's hash table item which is equal to the given
1262: * item.
1263: *
1264: * @param key a constant pool item.
1265: * @return the constant pool's hash table item which is equal to the given
1266: * item, or <tt>null</tt> if there is no such item.
1267: */
1268: private Item get(final Item key) {
1269: Item i = items[key.hashCode % items.length];
1270: while (i != null && !key.isEqualTo(i)) {
1271: i = i.next;
1272: }
1273: return i;
1274: }
1275:
1276: /**
1277: * Puts the given item in the constant pool's hash table. The hash table
1278: * <i>must</i> not already contains this item.
1279: *
1280: * @param i the item to be added to the constant pool's hash table.
1281: */
1282: private void put(final Item i) {
1283: if (index > threshold) {
1284: int ll = items.length;
1285: int nl = ll * 2 + 1;
1286: Item[] newItems = new Item[nl];
1287: for (int l = ll - 1; l >= 0; --l) {
1288: Item j = items[l];
1289: while (j != null) {
1290: int index = j.hashCode % newItems.length;
1291: Item k = j.next;
1292: j.next = newItems[index];
1293: newItems[index] = j;
1294: j = k;
1295: }
1296: }
1297: items = newItems;
1298: threshold = (int) (nl * 0.75);
1299: }
1300: int index = i.hashCode % items.length;
1301: i.next = items[index];
1302: items[index] = i;
1303: }
1304:
1305: /**
1306: * Puts one byte and two shorts into the constant pool.
1307: *
1308: * @param b a byte.
1309: * @param s1 a short.
1310: * @param s2 another short.
1311: */
1312: private void put122(final int b, final int s1, final int s2) {
1313: pool.put12(b, s1).putShort(s2);
1314: }
1315: }
|