KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > libraries > asm > ClassWriter


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

30
31 package oracle.toplink.libraries.asm;
32
33 /**
34  * A {@link ClassVisitor ClassVisitor} that generates Java class files. More
35  * precisely this visitor generates a byte array conforming to the Java class
36  * file format. It can be used alone, to generate a Java class "from scratch",
37  * or with one or more {@link ClassReader ClassReader} and adapter class
38  * visitor to generate a modified class from one or more existing Java classes.
39  *
40  * @author Eric Bruneton
41  */

42
43 public class ClassWriter implements ClassVisitor {
44
45   /**
46    * The type of CONSTANT_Class constant pool items.
47    */

48
49   final static int CLASS = 7;
50
51   /**
52    * The type of CONSTANT_Fieldref constant pool items.
53    */

54
55   final static int FIELD = 9;
56
57   /**
58    * The type of CONSTANT_Methodref constant pool items.
59    */

60
61   final static int METH = 10;
62
63   /**
64    * The type of CONSTANT_InterfaceMethodref constant pool items.
65    */

66
67   final static int IMETH = 11;
68
69   /**
70    * The type of CONSTANT_String constant pool items.
71    */

72
73   final static int STR = 8;
74
75   /**
76    * The type of CONSTANT_Integer constant pool items.
77    */

78
79   final static int INT = 3;
80
81   /**
82    * The type of CONSTANT_Float constant pool items.
83    */

84
85   final static int FLOAT = 4;
86
87   /**
88    * The type of CONSTANT_Long constant pool items.
89    */

90
91   final static int LONG = 5;
92
93   /**
94    * The type of CONSTANT_Double constant pool items.
95    */

96
97   final static int DOUBLE = 6;
98
99   /**
100    * The type of CONSTANT_NameAndType constant pool items.
101    */

102
103   final static int NAME_TYPE = 12;
104
105   /**
106    * The type of CONSTANT_Utf8 constant pool items.
107    */

108
109   final static int UTF8 = 1;
110
111   /**
112    * Minor and major version numbers of the class to be generated.
113    */

114
115   private int version;
116
117   /**
118    * Index of the next item to be added in the constant pool.
119    */

120
121   private short index;
122
123   /**
124    * The constant pool of this class.
125    */

126
127   private ByteVector pool;
128
129   /**
130    * The constant pool's hash table data.
131    */

132
133   private Item[] items;
134
135   /**
136    * The threshold of the constant pool's hash table.
137    */

138
139   private int threshold;
140
141   /**
142    * The access flags of this class.
143    */

144
145   private int access;
146
147   /**
148    * The constant pool item that contains the internal name of this class.
149    */

150
151   private int name;
152
153   /**
154    * The constant pool item that contains the internal name of the super class
155    * of this class.
156    */

157
158   private int superName;
159
160   /**
161    * Number of interfaces implemented or extended by this class or interface.
162    */

163
164   private int interfaceCount;
165
166   /**
167    * The interfaces implemented or extended by this class or interface. More
168    * precisely, this array contains the indexes of the constant pool items
169    * that contain the internal names of these interfaces.
170    */

171
172   private int[] interfaces;
173
174   /**
175    * The index of the constant pool item that contains the name of the source
176    * file from which this class was compiled.
177    */

178
179   private int sourceFile;
180
181   /**
182    * Number of fields of this class.
183    */

184
185   private int fieldCount;
186
187   /**
188    * The fields of this class.
189    */

190
191   private ByteVector fields;
192
193   /**
194    * <tt>true</tt> if the maximum stack size and number of local variables must
195    * be automatically computed.
196    */

197
198   private boolean computeMaxs;
199   
200   /**
201    * <tt>true</tt> to test that all attributes are known.
202    */

203   
204   boolean checkAttributes;
205
206   /**
207    * The methods of this class. These methods are stored in a linked list of
208    * {@link CodeWriter CodeWriter} objects, linked to each other by their {@link
209    * CodeWriter#next} field. This field stores the first element of this list.
210    */

211
212   CodeWriter firstMethod;
213
214   /**
215    * The methods of this class. These methods are stored in a linked list of
216    * {@link CodeWriter CodeWriter} objects, linked to each other by their {@link
217    * CodeWriter#next} field. This field stores the last element of this list.
218    */

219
220   CodeWriter lastMethod;
221
222   /**
223    * The number of entries in the InnerClasses attribute.
224    */

225
226   private int innerClassesCount;
227
228   /**
229    * The InnerClasses attribute.
230    */

231
232   private ByteVector innerClasses;
233
234   /**
235    * The non standard attributes of the class.
236    */

237
238   private Attribute attrs;
239
240   /**
241    * A reusable key used to look for items in the hash {@link #items items}.
242    */

243
244   Item key;
245
246   /**
247    * A reusable key used to look for items in the hash {@link #items items}.
248    */

249
250   Item key2;
251
252   /**
253    * A reusable key used to look for items in the hash {@link #items items}.
254    */

255
256   Item key3;
257
258   /**
259    * The type of instructions without any label.
260    */

261
262   final static int NOARG_INSN = 0;
263
264   /**
265    * The type of instructions with an signed byte label.
266    */

267
268   final static int SBYTE_INSN = 1;
269
270   /**
271    * The type of instructions with an signed short label.
272    */

273
274   final static int SHORT_INSN = 2;
275
276   /**
277    * The type of instructions with a local variable index label.
278    */

279
280   final static int VAR_INSN = 3;
281
282   /**
283    * The type of instructions with an implicit local variable index label.
284    */

285
286   final static int IMPLVAR_INSN = 4;
287
288   /**
289    * The type of instructions with a type descriptor argument.
290    */

291
292   final static int TYPE_INSN = 5;
293
294   /**
295    * The type of field and method invocations instructions.
296    */

297
298   final static int FIELDORMETH_INSN = 6;
299
300   /**
301    * The type of the INVOKEINTERFACE instruction.
302    */

303
304   final static int ITFMETH_INSN = 7;
305
306   /**
307    * The type of instructions with a 2 bytes bytecode offset label.
308    */

309
310   final static int LABEL_INSN = 8;
311
312   /**
313    * The type of instructions with a 4 bytes bytecode offset label.
314    */

315
316   final static int LABELW_INSN = 9;
317
318   /**
319    * The type of the LDC instruction.
320    */

321
322   final static int LDC_INSN = 10;
323
324   /**
325    * The type of the LDC_W and LDC2_W instructions.
326    */

327
328   final static int LDCW_INSN = 11;
329
330   /**
331    * The type of the IINC instruction.
332    */

333
334   final static int IINC_INSN = 12;
335
336   /**
337    * The type of the TABLESWITCH instruction.
338    */

339
340   final static int TABL_INSN = 13;
341
342   /**
343    * The type of the LOOKUPSWITCH instruction.
344    */

345
346   final static int LOOK_INSN = 14;
347
348   /**
349    * The type of the MULTIANEWARRAY instruction.
350    */

351
352   final static int MANA_INSN = 15;
353
354   /**
355    * The type of the WIDE instruction.
356    */

357
358   final static int WIDE_INSN = 16;
359
360   /**
361    * The instruction types of all JVM opcodes.
362    */

363
364   static byte[] TYPE;
365
366   // --------------------------------------------------------------------------
367
// Static initializer
368
// --------------------------------------------------------------------------
369

370   /**
371    * Computes the instruction types of JVM opcodes.
372    */

373
374   static {
375     int i;
376     byte[] b = new byte[220];
377     String JavaDoc s =
378       "AAAAAAAAAAAAAAAABCKLLDDDDDEEEEEEEEEEEEEEEEEEEEAAAAAAAADDDDDEEEEEEEEE" +
379       "EEEEEEEEEEEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMAAA" +
380       "AAAAAAAAAAAAAAAAAIIIIIIIIIIIIIIIIDNOAAAAAAGGGGGGGHAFBFAAFFAAQPIIJJII" +
381       "IIIIIIIIIIIIIIII";
382     for (i = 0; i < b.length; ++i) {
383       b[i] = (byte)(s.charAt(i) - 'A');
384     }
385     TYPE = b;
386
387     /* code to generate the above string
388
389     // SBYTE_INSN instructions
390     b[Constants.NEWARRAY] = SBYTE_INSN;
391     b[Constants.BIPUSH] = SBYTE_INSN;
392
393     // SHORT_INSN instructions
394     b[Constants.SIPUSH] = SHORT_INSN;
395
396     // (IMPL)VAR_INSN instructions
397     b[Constants.RET] = VAR_INSN;
398     for (i = Constants.ILOAD; i <= Constants.ALOAD; ++i) {
399       b[i] = VAR_INSN;
400     }
401     for (i = Constants.ISTORE; i <= Constants.ASTORE; ++i) {
402       b[i] = VAR_INSN;
403     }
404     for (i = 26; i <= 45; ++i) { // ILOAD_0 to ALOAD_3
405       b[i] = IMPLVAR_INSN;
406     }
407     for (i = 59; i <= 78; ++i) { // ISTORE_0 to ASTORE_3
408       b[i] = IMPLVAR_INSN;
409     }
410
411     // TYPE_INSN instructions
412     b[Constants.NEW] = TYPE_INSN;
413     b[Constants.ANEWARRAY] = TYPE_INSN;
414     b[Constants.CHECKCAST] = TYPE_INSN;
415     b[Constants.INSTANCEOF] = TYPE_INSN;
416
417     // (Set)FIELDORMETH_INSN instructions
418     for (i = Constants.GETSTATIC; i <= Constants.INVOKESTATIC; ++i) {
419       b[i] = FIELDORMETH_INSN;
420     }
421     b[Constants.INVOKEINTERFACE] = ITFMETH_INSN;
422
423     // LABEL(W)_INSN instructions
424     for (i = Constants.IFEQ; i <= Constants.JSR; ++i) {
425       b[i] = LABEL_INSN;
426     }
427     b[Constants.IFNULL] = LABEL_INSN;
428     b[Constants.IFNONNULL] = LABEL_INSN;
429     b[200] = LABELW_INSN; // GOTO_W
430     b[201] = LABELW_INSN; // JSR_W
431     // temporary opcodes used internally by ASM - see Label and CodeWriter
432     for (i = 202; i < 220; ++i) {
433       b[i] = LABEL_INSN;
434     }
435
436     // LDC(_W) instructions
437     b[Constants.LDC] = LDC_INSN;
438     b[19] = LDCW_INSN; // LDC_W
439     b[20] = LDCW_INSN; // LDC2_W
440
441     // special instructions
442     b[Constants.IINC] = IINC_INSN;
443     b[Constants.TABLESWITCH] = TABL_INSN;
444     b[Constants.LOOKUPSWITCH] = LOOK_INSN;
445     b[Constants.MULTIANEWARRAY] = MANA_INSN;
446     b[196] = WIDE_INSN; // WIDE
447
448     for (i = 0; i < b.length; ++i) {
449       System.err.print((char)('A' + b[i]));
450     }
451     System.err.println();
452     */

453   }
454
455   // --------------------------------------------------------------------------
456
// Constructor
457
// --------------------------------------------------------------------------
458

459   /**
460    * Constructs a new {@link ClassWriter ClassWriter} object.
461    *
462    * @param computeMaxs <tt>true</tt> if the maximum stack size and the maximum
463    * number of local variables must be automatically computed. If this flag
464    * is <tt>true</tt>, then the arguments of the {@link
465    * CodeVisitor#visitMaxs visitMaxs} method of the {@link CodeVisitor
466    * CodeVisitor} returned by the {@link #visitMethod visitMethod} method
467    * will be ignored, and computed automatically from the signature and
468    * the bytecode of each method.
469    */

470
471   public ClassWriter (final boolean computeMaxs) {
472     this(computeMaxs, false);
473   }
474
475   /**
476    * Constructs a new {@link ClassWriter ClassWriter} object.
477    *
478    * @param computeMaxs <tt>true</tt> if the maximum stack size and the maximum
479    * number of local variables must be automatically computed. If this flag
480    * is <tt>true</tt>, then the arguments of the {@link
481    * CodeVisitor#visitMaxs visitMaxs} method of the {@link CodeVisitor
482    * CodeVisitor} returned by the {@link #visitMethod visitMethod} method
483    * will be ignored, and computed automatically from the signature and
484    * the bytecode of each method.
485    * @param skipUnknownAttributes <tt>true</tt> to silently ignore unknown
486    * attributes, or <tt>false</tt> to throw an exception if an unknown
487    * attribute is found.
488    */

489
490   public ClassWriter (
491     final boolean computeMaxs,
492     final boolean skipUnknownAttributes)
493   {
494     index = 1;
495     pool = new ByteVector();
496     items = new Item[64];
497     threshold = (int)(0.75d*items.length);
498     key = new Item();
499     key2 = new Item();
500     key3 = new Item();
501     this.computeMaxs = computeMaxs;
502     this.checkAttributes = !skipUnknownAttributes;
503   }
504
505   // --------------------------------------------------------------------------
506
// Implementation of the ClassVisitor interface
507
// --------------------------------------------------------------------------
508

509   public void visit (
510     final int version,
511     final int access,
512     final String JavaDoc name,
513     final String JavaDoc superName,
514     final String JavaDoc[] interfaces,
515     final String JavaDoc sourceFile)
516   {
517     this.version = version;
518     this.access = access;
519     this.name = newClass(name);
520     this.superName = superName == null ? 0 : newClass(superName);
521     if (interfaces != null && interfaces.length > 0) {
522       interfaceCount = interfaces.length;
523       this.interfaces = new int[interfaceCount];
524       for (int i = 0; i < interfaceCount; ++i) {
525         this.interfaces[i] = newClass(interfaces[i]);
526       }
527     }
528     if (sourceFile != null) {
529       newUTF8("SourceFile");
530       this.sourceFile = newUTF8(sourceFile);
531     }
532     if ((access & Constants.ACC_DEPRECATED) != 0) {
533       newUTF8("Deprecated");
534     }
535     if ((access & Constants.ACC_SYNTHETIC) != 0) {
536       newUTF8("Synthetic");
537     }
538   }
539
540   public void visitInnerClass (
541     final String JavaDoc name,
542     final String JavaDoc outerName,
543     final String JavaDoc innerName,
544     final int access)
545   {
546     if (innerClasses == null) {
547       newUTF8("InnerClasses");
548       innerClasses = new ByteVector();
549     }
550     ++innerClassesCount;
551     innerClasses.putShort(name == null ? 0 : newClass(name));
552     innerClasses.putShort(outerName == null ? 0 : newClass(outerName));
553     innerClasses.putShort(innerName == null ? 0 : newUTF8(innerName));
554     innerClasses.putShort(access);
555   }
556
557   public void visitField (
558     final int access,
559     final String JavaDoc name,
560     final String JavaDoc desc,
561     final Object JavaDoc value,
562     final Attribute attrs)
563   {
564     ++fieldCount;
565     if (fields == null) {
566       fields = new ByteVector();
567     }
568     fields.putShort(access).putShort(newUTF8(name)).putShort(newUTF8(desc));
569     int attributeCount = 0;
570     if (value != null) {
571       ++attributeCount;
572     }
573     if ((access & Constants.ACC_SYNTHETIC) != 0) {
574       ++attributeCount;
575     }
576     if ((access & Constants.ACC_DEPRECATED) != 0) {
577       ++attributeCount;
578     }
579     if (attrs != null) {
580       attributeCount += attrs.getCount();
581     }
582     fields.putShort(attributeCount);
583     if (value != null) {
584       fields.putShort(newUTF8("ConstantValue"));
585       fields.putInt(2).putShort(newConstItem(value).index);
586     }
587     if ((access & Constants.ACC_SYNTHETIC) != 0) {
588       fields.putShort(newUTF8("Synthetic")).putInt(0);
589     }
590     if ((access & Constants.ACC_DEPRECATED) != 0) {
591       fields.putShort(newUTF8("Deprecated")).putInt(0);
592     }
593     if (attrs != null) {
594       attrs.put(this, null, 0, -1, -1, fields);
595     }
596   }
597
598   public CodeVisitor visitMethod (
599     final int access,
600     final String JavaDoc name,
601     final String JavaDoc desc,
602     final String JavaDoc[] exceptions,
603     final Attribute attrs)
604   {
605     CodeWriter cw = new CodeWriter(this, computeMaxs);
606     cw.init(access, name, desc, exceptions, attrs);
607     return cw;
608   }
609
610   public void visitAttribute (final Attribute attr) {
611     attr.next = attrs;
612     attrs = attr;
613   }
614
615   public void visitEnd () {
616   }
617
618   // --------------------------------------------------------------------------
619
// Other public methods
620
// --------------------------------------------------------------------------
621

622   /**
623    * Returns the bytecode of the class that was build with this class writer.
624    *
625    * @return the bytecode of the class that was build with this class writer.
626    */

627
628   public byte[] toByteArray () {
629     // computes the real size of the bytecode of this class
630
int size = 24 + 2*interfaceCount;
631     if (fields != null) {
632       size += fields.length;
633     }
634     int nbMethods = 0;
635     CodeWriter cb = firstMethod;
636     while (cb != null) {
637       ++nbMethods;
638       size += cb.getSize();
639       cb = cb.next;
640     }
641     int attributeCount = 0;
642     if (sourceFile != 0) {
643       ++attributeCount;
644       size += 8;
645     }
646     if ((access & Constants.ACC_DEPRECATED) != 0) {
647       ++attributeCount;
648       size += 6;
649     }
650     if ((access & Constants.ACC_SYNTHETIC) != 0) {
651       ++attributeCount;
652       size += 6;
653     }
654     if (innerClasses != null) {
655       ++attributeCount;
656       size += 8 + innerClasses.length;
657     }
658     if (attrs != null) {
659       attributeCount += attrs.getCount();
660       size += attrs.getSize(this, null, 0, -1, -1);
661     }
662     size += pool.length;
663     // allocates a byte vector of this size, in order to avoid unnecessary
664
// arraycopy operations in the ByteVector.enlarge() method
665
ByteVector out = new ByteVector(size);
666     out.putInt(0xCAFEBABE).putInt(version);
667     out.putShort(index).putByteArray(pool.data, 0, pool.length);
668     out.putShort(access).putShort(name).putShort(superName);
669     out.putShort(interfaceCount);
670     for (int i = 0; i < interfaceCount; ++i) {
671       out.putShort(interfaces[i]);
672     }
673     out.putShort(fieldCount);
674     if (fields != null) {
675       out.putByteArray(fields.data, 0, fields.length);
676     }
677     out.putShort(nbMethods);
678     cb = firstMethod;
679     while (cb != null) {
680       cb.put(out);
681       cb = cb.next;
682     }
683     out.putShort(attributeCount);
684     if (sourceFile != 0) {
685       out.putShort(newUTF8("SourceFile")).putInt(2).putShort(sourceFile);
686     }
687     if ((access & Constants.ACC_DEPRECATED) != 0) {
688       out.putShort(newUTF8("Deprecated")).putInt(0);
689     }
690     if ((access & Constants.ACC_SYNTHETIC) != 0) {
691       out.putShort(newUTF8("Synthetic")).putInt(0);
692     }
693     if (innerClasses != null) {
694       out.putShort(newUTF8("InnerClasses"));
695       out.putInt(innerClasses.length + 2).putShort(innerClassesCount);
696       out.putByteArray(innerClasses.data, 0, innerClasses.length);
697     }
698     if (attrs != null) {
699       attrs.put(this, null, 0, -1, -1, out);
700     }
701     return out.data;
702   }
703
704   // --------------------------------------------------------------------------
705
// Utility methods: constant pool management
706
// --------------------------------------------------------------------------
707

708   /**
709    * Adds a number or string constant to the constant pool of the class being
710    * build. Does nothing if the constant pool already contains a similar item.
711    *
712    * @param cst the value of the constant to be added to the constant pool. This
713    * parameter must be an {@link java.lang.Integer Integer}, a {@link
714    * java.lang.Float Float}, a {@link java.lang.Long Long}, a {@link
715    * java.lang.Double Double}, a {@link String String} or a {@link Type}.
716    * @return a new or already existing constant item with the given value.
717    */

718
719   Item newConstItem (final Object JavaDoc cst) {
720     if (cst instanceof Integer JavaDoc) {
721       int val = ((Integer JavaDoc)cst).intValue();
722       return newInteger(val);
723     } else if (cst instanceof Byte JavaDoc) {
724       int val = ((Byte JavaDoc)cst).intValue();
725       return newInteger(val);
726     } else if (cst instanceof Character JavaDoc) {
727       int val = ((Character JavaDoc)cst).charValue();
728       return newInteger(val);
729     } else if (cst instanceof Short JavaDoc) {
730       int val = ((Short JavaDoc)cst).intValue();
731       return newInteger(val);
732     } else if (cst instanceof Boolean JavaDoc) {
733       int val = ((Boolean JavaDoc)cst).booleanValue() ? 1 : 0;
734       return newInteger(val);
735     } else if (cst instanceof Float JavaDoc) {
736       float val = ((Float JavaDoc)cst).floatValue();
737       return newFloat(val);
738     } else if (cst instanceof Long JavaDoc) {
739       long val = ((Long JavaDoc)cst).longValue();
740       return newLong(val);
741     } else if (cst instanceof Double JavaDoc) {
742       double val = ((Double JavaDoc)cst).doubleValue();
743       return newDouble(val);
744     } else if (cst instanceof String JavaDoc) {
745       return newString((String JavaDoc)cst);
746     } else if (cst instanceof Type) {
747       Type t = (Type)cst;
748       return newClassItem(
749         t.getSort() == Type.OBJECT ? t.getInternalName() : t.getDescriptor());
750     } else {
751       throw new IllegalArgumentException JavaDoc("value " + cst);
752     }
753   }
754
755   /**
756    * Adds a number or string constant to the constant pool of the class being
757    * build. Does nothing if the constant pool already contains a similar item.
758    * <i>This method is intended for {@link Attribute} sub classes, and is
759    * normally not needed by class generators or adapters.</i>
760    *
761    * @param cst the value of the constant to be added to the constant pool. This
762    * parameter must be an {@link java.lang.Integer Integer}, a {@link
763    * java.lang.Float Float}, a {@link java.lang.Long Long}, a {@link
764           java.lang.Double Double} or a {@link String String}.
765    * @return the index of a new or already existing constant item with the given
766    * value.
767    */

768
769   public int newConst (final Object JavaDoc cst) {
770     return newConstItem(cst).index;
771   }
772
773   public int newConstInt (final int i) {
774     return newInteger(i).index;
775   }
776
777   public int newConstLong (final long l) {
778     return newLong(l).index;
779   }
780
781   public int newConstFloat (final float f) {
782     return newFloat(f).index;
783   }
784
785   public int newConstDouble (final double d) {
786     return newDouble(d).index;
787   }
788
789   /**
790    * Adds an UTF8 string to the constant pool of the class being build. Does
791    * nothing if the constant pool already contains a similar item. <i>This
792    * method is intended for {@link Attribute} sub classes, and is normally not
793    * needed by class generators or adapters.</i>
794    *
795    * @param value the String value.
796    * @return the index of a new or already existing UTF8 item.
797    */

798
799   public int newUTF8 (final String JavaDoc value) {
800     key.set(UTF8, value, null, null);
801     Item result = get(key);
802     if (result == null) {
803       pool.putByte(UTF8).putUTF8(value);
804       result = new Item(index++, key);
805       put(result);
806     }
807     return result.index;
808   }
809
810   /**
811    * Adds a class reference to the constant pool of the class being build. Does
812    * nothing if the constant pool already contains a similar item. <i>This
813    * method is intended for {@link Attribute} sub classes, and is normally not
814    * needed by class generators or adapters.</i>
815    *
816    * @param value the internal name of the class.
817    * @return the index of a new or already existing class reference item.
818    */

819
820   public int newClass (final String JavaDoc value) {
821     return newClassItem(value).index;
822   }
823
824   /**
825    * Adds a class reference to the constant pool of the class being build. Does
826    * nothing if the constant pool already contains a similar item. <i>This
827    * method is intended for {@link Attribute} sub classes, and is normally not
828    * needed by class generators or adapters.</i>
829    *
830    * @param value the internal name of the class.
831    * @return a new or already existing class reference item.
832    */

833   
834   private Item newClassItem (final String JavaDoc value) {
835     key2.set(CLASS, value, null, null);
836     Item result = get(key2);
837     if (result == null) {
838       pool.put12(CLASS, newUTF8(value));
839       result = new Item(index++, key2);
840       put(result);
841     }
842     return result;
843   }
844
845   /**
846    * Adds a field reference to the constant pool of the class being build. Does
847    * nothing if the constant pool already contains a similar item. <i>This
848    * method is intended for {@link Attribute} sub classes, and is normally not
849    * needed by class generators or adapters.</i>
850    *
851    * @param owner the internal name of the field's owner class.
852    * @param name the field's name.
853    * @param desc the field's descriptor.
854    * @return the index of a new or already existing field reference item.
855    */

856
857   public int newField (
858     final String JavaDoc owner,
859     final String JavaDoc name,
860     final String JavaDoc desc)
861   {
862     key3.set(FIELD, owner, name, desc);
863     Item result = get(key3);
864     if (result == null) {
865       put122(FIELD, newClass(owner), newNameType(name, desc));
866       result = new Item(index++, key3);
867       put(result);
868     }
869     return result.index;
870   }
871
872   /**
873    * Adds a method reference to the constant pool of the class being build. Does
874    * nothing if the constant pool already contains a similar item.
875    *
876    * @param owner the internal name of the method's owner class.
877    * @param name the method's name.
878    * @param desc the method's descriptor.
879    * @param itf <tt>true</tt> if <tt>owner</tt> is an interface.
880    * @return a new or already existing method reference item.
881    */

882
883   Item newMethodItem (
884     final String JavaDoc owner,
885     final String JavaDoc name,
886     final String JavaDoc desc,
887     final boolean itf)
888   {
889     key3.set(itf ? IMETH : METH, owner, name, desc);
890     Item result = get(key3);
891     if (result == null) {
892       put122(itf ? IMETH : METH, newClass(owner), newNameType(name, desc));
893       result = new Item(index++, key3);
894       put(result);
895     }
896     return result;
897   }
898
899   /**
900    * Adds a method reference to the constant pool of the class being build. Does
901    * nothing if the constant pool already contains a similar item. <i>This
902    * method is intended for {@link Attribute} sub classes, and is normally not
903    * needed by class generators or adapters.</i>
904    *
905    * @param owner the internal name of the method's owner class.
906    * @param name the method's name.
907    * @param desc the method's descriptor.
908    * @param itf <tt>true</tt> if <tt>owner</tt> is an interface.
909    * @return the index of a new or already existing method reference item.
910    */

911
912   public int newMethod (
913     final String JavaDoc owner,
914     final String JavaDoc name,
915     final String JavaDoc desc,
916     final boolean itf)
917   {
918     return newMethodItem(owner, name, desc, itf).index;
919   }
920
921   /**
922    * Adds an integer to the constant pool of the class being build. Does nothing
923    * if the constant pool already contains a similar item.
924    *
925    * @param value the int value.
926    * @return a new or already existing int item.
927    */

928
929   private Item newInteger (final int value) {
930     key.set(value);
931     Item result = get(key);
932     if (result == null) {
933       pool.putByte(INT).putInt(value);
934       result = new Item(index++, key);
935       put(result);
936     }
937     return result;
938   }
939
940   /**
941    * Adds a float to the constant pool of the class being build. Does nothing if
942    * the constant pool already contains a similar item.
943    *
944    * @param value the float value.
945    * @return a new or already existing float item.
946    */

947
948   private Item newFloat (final float value) {
949     key.set(value);
950     Item result = get(key);
951     if (result == null) {
952       pool.putByte(FLOAT).putInt(Float.floatToIntBits(value));
953       result = new Item(index++, key);
954       put(result);
955     }
956     return result;
957   }
958
959   /**
960    * Adds a long to the constant pool of the class being build. Does nothing if
961    * the constant pool already contains a similar item.
962    *
963    * @param value the long value.
964    * @return a new or already existing long item.
965    */

966
967   private Item newLong (final long value) {
968     key.set(value);
969     Item result = get(key);
970     if (result == null) {
971       pool.putByte(LONG).putLong(value);
972       result = new Item(index, key);
973       put(result);
974       index += 2;
975     }
976     return result;
977   }
978
979   /**
980    * Adds a double to the constant pool of the class being build. Does nothing
981    * if the constant pool already contains a similar item.
982    *
983    * @param value the double value.
984    * @return a new or already existing double item.
985    */

986
987   private Item newDouble (final double value) {
988     key.set(value);
989     Item result = get(key);
990     if (result == null) {
991       pool.putByte(DOUBLE).putLong(Double.doubleToLongBits(value));
992       result = new Item(index, key);
993       put(result);
994       index += 2;
995     }
996     return result;
997   }
998
999   /**
1000   * Adds a string 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 String value.
1004   * @return a new or already existing string item.
1005   */

1006
1007  private Item newString (final String JavaDoc value) {
1008    key2.set(STR, value, null, null);
1009    Item result = get(key2);
1010    if (result == null) {
1011      pool.put12(STR, newUTF8(value));
1012      result = new Item(index++, key2);
1013      put(result);
1014    }
1015    return result;
1016  }
1017
1018  /**
1019   * Adds a name and type to the constant pool of the class being build. Does
1020   * nothing if the constant pool already contains a similar item. <i>This
1021   * method is intended for {@link Attribute} sub classes, and is normally not
1022   * needed by class generators or adapters.</i>
1023   *
1024   * @param name a name.
1025   * @param desc a type descriptor.
1026   * @return the index of a new or already existing name and type item.
1027   */

1028
1029  public int newNameType (final String JavaDoc name, final String JavaDoc desc) {
1030    key2.set(NAME_TYPE, name, desc, null);
1031    Item result = get(key2);
1032    if (result == null) {
1033      put122(NAME_TYPE, newUTF8(name), newUTF8(desc));
1034      result = new Item(index++, key2);
1035      put(result);
1036    }
1037    return result.index;
1038  }
1039
1040  /**
1041   * Returns the constant pool's hash table item which is equal to the given
1042   * item.
1043   *
1044   * @param key a constant pool item.
1045   * @return the constant pool's hash table item which is equal to the given
1046   * item, or <tt>null</tt> if there is no such item.
1047   */

1048
1049  private Item get (final Item key) {
1050    int h = key.hashCode;
1051    Item i = items[h % items.length];
1052    while (i != null) {
1053      if (i.hashCode == h && key.isEqualTo(i)) {
1054        return i;
1055      }
1056      i = i.next;
1057    }
1058    return null;
1059  }
1060
1061  /**
1062   * Puts the given item in the constant pool's hash table. The hash table
1063   * <i>must</i> not already contains this item.
1064   *
1065   * @param i the item to be added to the constant pool's hash table.
1066   */

1067
1068  private void put (final Item i) {
1069    if (index > threshold) {
1070      Item[] newItems = new Item[items.length * 2 + 1];
1071      for (int l = items.length - 1; l >= 0; --l) {
1072        Item j = items[l];
1073        while (j != null) {
1074          int index = j.hashCode % newItems.length;
1075          Item k = j.next;
1076          j.next = newItems[index];
1077          newItems[index] = j;
1078          j = k;
1079        }
1080      }
1081      items = newItems;
1082      threshold = (int)(items.length * 0.75);
1083    }
1084    int index = i.hashCode % items.length;
1085    i.next = items[index];
1086    items[index] = i;
1087  }
1088
1089  /**
1090   * Puts one byte and two shorts into the constant pool.
1091   *
1092   * @param b a byte.
1093   * @param s1 a short.
1094   * @param s2 another short.
1095   */

1096
1097  private void put122 (final int b, final int s1, final int s2) {
1098    pool.put12(b, s1).putShort(s2);
1099  }
1100}
1101
Popular Tags