KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > bytecode > ConstantPool


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.bytecode;
31
32 import com.caucho.log.Log;
33
34 import java.io.IOException JavaDoc;
35 import java.util.ArrayList JavaDoc;
36 import java.util.HashMap JavaDoc;
37 import java.util.logging.Logger JavaDoc;
38
39 /**
40  * Represents a constant pool entry.
41  */

42 public class ConstantPool {
43   static private final Logger JavaDoc log = Log.open(ConstantPool.class);
44
45   public static final int CP_CLASS = 7;
46   public static final int CP_FIELD_REF = 9;
47   public static final int CP_METHOD_REF = 10;
48   public static final int CP_INTERFACE_METHOD_REF = 11;
49   public static final int CP_STRING = 8;
50   public static final int CP_INTEGER = 3;
51   public static final int CP_FLOAT = 4;
52   public static final int CP_LONG = 5;
53   public static final int CP_DOUBLE = 6;
54   public static final int CP_NAME_AND_TYPE = 12;
55   public static final int CP_UTF8 = 1;
56
57   private ArrayList JavaDoc<ConstantPoolEntry> _entries;
58   private HashMap JavaDoc<String JavaDoc,Utf8Constant> _utf8Map
59     = new HashMap JavaDoc<String JavaDoc,Utf8Constant>();
60
61   ConstantPool()
62   {
63     _entries = new ArrayList JavaDoc<ConstantPoolEntry>();
64     _entries.add(null);
65   }
66
67   /**
68    * Returns an entry
69    */

70   public ConstantPoolEntry getEntry(int index)
71   {
72     return _entries.get(index);
73   }
74
75   /**
76    * Returns all the entries.
77    */

78   public ArrayList JavaDoc<ConstantPoolEntry> getEntries()
79   {
80     return _entries;
81   }
82
83   /**
84    * Returns a class constant
85    */

86   public ClassConstant getClass(int index)
87   {
88     return (ClassConstant) _entries.get(index);
89   }
90
91   /**
92    * Returns a field ref entry
93    */

94   public FieldRefConstant getFieldRef(int index)
95   {
96     return (FieldRefConstant) _entries.get(index);
97   }
98
99   /**
100    * Returns a method ref entry
101    */

102   public MethodRefConstant getMethodRef(int index)
103   {
104     return (MethodRefConstant) _entries.get(index);
105   }
106
107   /**
108    * Returns an interface method ref entry
109    */

110   public InterfaceMethodRefConstant getInterfaceMethodRef(int index)
111   {
112     return (InterfaceMethodRefConstant) _entries.get(index);
113   }
114
115   /**
116    * Returns a string constant
117    */

118   public StringConstant getString(int index)
119   {
120     return (StringConstant) _entries.get(index);
121   }
122
123   /**
124    * Returns an integer constant
125    */

126   public IntegerConstant getInteger(int index)
127   {
128     return (IntegerConstant) _entries.get(index);
129   }
130
131   /**
132    * Returns a long constant
133    */

134   public LongConstant getLong(int index)
135   {
136     return (LongConstant) _entries.get(index);
137   }
138
139   /**
140    * Returns a float constant
141    */

142   public FloatConstant getFloat(int index)
143   {
144     return (FloatConstant) _entries.get(index);
145   }
146
147   /**
148    * Returns a double constant
149    */

150   public DoubleConstant getDouble(int index)
151   {
152     return (DoubleConstant) _entries.get(index);
153   }
154
155   /**
156    * Returns a name-and-type constant
157    */

158   public NameAndTypeConstant getNameAndType(int index)
159   {
160     return (NameAndTypeConstant) _entries.get(index);
161   }
162
163   /**
164    * Returns a utf-8 constant
165    */

166   public Utf8Constant getUtf8(int index)
167   {
168     return (Utf8Constant) _entries.get(index);
169   }
170
171   /**
172    * Returns a utf-8 constant as a string
173    */

174   public String JavaDoc getUtf8AsString(int index)
175   {
176     Utf8Constant utf8 = (Utf8Constant) _entries.get(index);
177
178     if (utf8 == null)
179       return null;
180     else
181       return utf8.getValue();
182   }
183
184   /**
185    * Adds a new constant.
186    */

187   public void addConstant(ConstantPoolEntry entry)
188   {
189     if (entry instanceof Utf8Constant) {
190       Utf8Constant utf8 = (Utf8Constant) entry;
191       
192       _utf8Map.put(utf8.getValue(), utf8);
193     }
194     
195     _entries.add(entry);
196   }
197
198   /**
199    * Gets a UTF-8 constant.
200    */

201   public Utf8Constant getUTF8(String JavaDoc value)
202   {
203     return _utf8Map.get(value);
204   }
205
206   /**
207    * Adds a UTF-8 constant.
208    */

209   public Utf8Constant addUTF8(String JavaDoc value)
210   {
211     Utf8Constant entry = getUTF8(value);
212
213     if (entry != null)
214       return entry;
215
216     entry = new Utf8Constant(this, _entries.size(), value);
217     
218     addConstant(entry);
219
220     return entry;
221   }
222
223   /**
224    * Gets a string constant.
225    */

226   public StringConstant getString(String JavaDoc name)
227   {
228     for (int i = 0; i < _entries.size(); i++) {
229       ConstantPoolEntry entry = _entries.get(i);
230
231       if (! (entry instanceof StringConstant))
232     continue;
233
234       StringConstant stringEntry = (StringConstant) entry;
235
236       if (stringEntry.getString().equals(name))
237     return stringEntry;
238     }
239
240     return null;
241   }
242
243   /**
244    * Adds a string constant.
245    */

246   public StringConstant addString(String JavaDoc name)
247   {
248     StringConstant entry = getString(name);
249
250     if (entry != null)
251       return entry;
252
253     Utf8Constant utf8 = addUTF8(name);
254
255     entry = new StringConstant(this, _entries.size(), utf8.getIndex());
256
257     addConstant(entry);
258
259     return entry;
260   }
261
262   /**
263    * Gets a integer constant.
264    */

265   public IntegerConstant getIntegerByValue(int value)
266   {
267     for (int i = 0; i < _entries.size(); i++) {
268       ConstantPoolEntry entry = _entries.get(i);
269
270       if (! (entry instanceof IntegerConstant))
271     continue;
272
273       IntegerConstant integerEntry = (IntegerConstant) entry;
274
275       if (integerEntry.getValue() == value)
276     return integerEntry;
277     }
278
279     return null;
280   }
281
282   /**
283    * Adds a integer constant.
284    */

285   public IntegerConstant addInteger(int value)
286   {
287     IntegerConstant entry = getIntegerByValue(value);
288
289     if (entry != null)
290       return entry;
291
292     entry = new IntegerConstant(this, _entries.size(), value);
293     
294     addConstant(entry);
295
296     return entry;
297   }
298
299   /**
300    * Gets a long constant.
301    */

302   public LongConstant getLongByValue(long value)
303   {
304     for (int i = 0; i < _entries.size(); i++) {
305       ConstantPoolEntry entry = _entries.get(i);
306
307       if (! (entry instanceof LongConstant))
308     continue;
309
310       LongConstant longEntry = (LongConstant) entry;
311
312       if (longEntry.getValue() == value)
313     return longEntry;
314     }
315
316     return null;
317   }
318
319   /**
320    * Adds a long constant.
321    */

322   public LongConstant addLong(long value)
323   {
324     LongConstant entry = getLongByValue(value);
325
326     if (entry != null)
327       return entry;
328
329     entry = new LongConstant(this, _entries.size(), value);
330     
331     addConstant(entry);
332     addConstant(null);
333
334     return entry;
335   }
336
337   /**
338    * Gets a float constant.
339    */

340   public FloatConstant getFloatByValue(float value)
341   {
342     for (int i = 0; i < _entries.size(); i++) {
343       ConstantPoolEntry entry = _entries.get(i);
344
345       if (! (entry instanceof FloatConstant))
346     continue;
347
348       FloatConstant floatEntry = (FloatConstant) entry;
349
350       if (floatEntry.getValue() == value)
351     return floatEntry;
352     }
353
354     return null;
355   }
356
357   /**
358    * Adds a float constant.
359    */

360   public FloatConstant addFloat(float value)
361   {
362     FloatConstant entry = getFloatByValue(value);
363
364     if (entry != null)
365       return entry;
366
367     entry = new FloatConstant(this, _entries.size(), value);
368     
369     addConstant(entry);
370
371     return entry;
372   }
373
374   /**
375    * Gets a double constant.
376    */

377   public DoubleConstant getDoubleByValue(double value)
378   {
379     for (int i = 0; i < _entries.size(); i++) {
380       ConstantPoolEntry entry = _entries.get(i);
381
382       if (! (entry instanceof DoubleConstant))
383     continue;
384
385       DoubleConstant doubleEntry = (DoubleConstant) entry;
386
387       if (doubleEntry.getValue() == value)
388     return doubleEntry;
389     }
390
391     return null;
392   }
393
394   /**
395    * Adds a double constant.
396    */

397   public DoubleConstant addDouble(double value)
398   {
399     DoubleConstant entry = getDoubleByValue(value);
400
401     if (entry != null)
402       return entry;
403
404     entry = new DoubleConstant(this, _entries.size(), value);
405     
406     addConstant(entry);
407     addConstant(null);
408
409     return entry;
410   }
411
412   /**
413    * Gets a class constant.
414    */

415   public ClassConstant getClass(String JavaDoc name)
416   {
417     for (int i = 0; i < _entries.size(); i++) {
418       ConstantPoolEntry entry = _entries.get(i);
419
420       if (! (entry instanceof ClassConstant))
421     continue;
422
423       ClassConstant classEntry = (ClassConstant) entry;
424
425       if (classEntry.getName().equals(name))
426     return classEntry;
427     }
428
429     return null;
430   }
431
432   /**
433    * Adds a class constant.
434    */

435   public ClassConstant addClass(String JavaDoc name)
436   {
437     ClassConstant entry = getClass(name);
438
439     if (entry != null)
440       return entry;
441
442     Utf8Constant utf8 = addUTF8(name);
443
444     entry = new ClassConstant(this, _entries.size(), utf8.getIndex());
445
446     addConstant(entry);
447
448     return entry;
449   }
450
451   /**
452    * Gets a name-and-type constant.
453    */

454   public NameAndTypeConstant getNameAndType(String JavaDoc name, String JavaDoc type)
455   {
456     for (int i = 0; i < _entries.size(); i++) {
457       ConstantPoolEntry entry = _entries.get(i);
458
459       if (! (entry instanceof NameAndTypeConstant))
460     continue;
461
462       NameAndTypeConstant methodEntry = (NameAndTypeConstant) entry;
463
464       if (methodEntry.getName().equals(name) &&
465       methodEntry.getType().equals(type))
466     return methodEntry;
467     }
468
469     return null;
470   }
471
472   /**
473    * Adds a name-and-type constant.
474    */

475   public NameAndTypeConstant addNameAndType(String JavaDoc name, String JavaDoc type)
476   {
477     NameAndTypeConstant entry = getNameAndType(name, type);
478
479     if (entry != null)
480       return entry;
481
482     Utf8Constant nameEntry = addUTF8(name);
483     Utf8Constant typeEntry = addUTF8(type);
484
485     entry = new NameAndTypeConstant(this, _entries.size(),
486                     nameEntry.getIndex(),
487                     typeEntry.getIndex());
488
489     addConstant(entry);
490
491     return entry;
492   }
493
494   /**
495    * Gets a field ref constant.
496    */

497   public FieldRefConstant getFieldRef(String JavaDoc className,
498                       String JavaDoc name,
499                       String JavaDoc type)
500   {
501     for (int i = 0; i < _entries.size(); i++) {
502       ConstantPoolEntry entry = _entries.get(i);
503
504       if (! (entry instanceof FieldRefConstant))
505     continue;
506
507       FieldRefConstant fieldEntry = (FieldRefConstant) entry;
508
509       if (fieldEntry.getClassName().equals(className) &&
510       fieldEntry.getName().equals(name) &&
511       fieldEntry.getType().equals(type))
512     return fieldEntry;
513     }
514
515     return null;
516   }
517
518   /**
519    * Gets a field ref constant.
520    */

521   public FieldRefConstant getFieldRef(String JavaDoc name)
522   {
523     for (int i = 0; i < _entries.size(); i++) {
524       ConstantPoolEntry entry = _entries.get(i);
525
526       if (! (entry instanceof FieldRefConstant))
527     continue;
528
529       FieldRefConstant fieldEntry = (FieldRefConstant) entry;
530
531       if (fieldEntry.getName().equals(name))
532     return fieldEntry;
533     }
534
535     return null;
536   }
537
538   /**
539    * Adds a field ref constant.
540    */

541   public FieldRefConstant addFieldRef(String JavaDoc className,
542                       String JavaDoc name,
543                       String JavaDoc type)
544   {
545     FieldRefConstant entry = getFieldRef(className, name, type);
546
547     if (entry != null)
548       return entry;
549
550     ClassConstant classEntry = addClass(className);
551     NameAndTypeConstant typeEntry = addNameAndType(name, type);
552
553     entry = new FieldRefConstant(this, _entries.size(),
554                  classEntry.getIndex(),
555                  typeEntry.getIndex());
556
557     addConstant(entry);
558
559     return entry;
560   }
561
562   /**
563    * Gets a method ref constant.
564    */

565   public MethodRefConstant getMethodRef(String JavaDoc className,
566                     String JavaDoc name,
567                     String JavaDoc type)
568   {
569     for (int i = 0; i < _entries.size(); i++) {
570       ConstantPoolEntry entry = _entries.get(i);
571
572       if (! (entry instanceof MethodRefConstant))
573     continue;
574
575       MethodRefConstant methodEntry = (MethodRefConstant) entry;
576
577       if (methodEntry.getClassName().equals(className) &&
578       methodEntry.getName().equals(name) &&
579       methodEntry.getType().equals(type))
580     return methodEntry;
581     }
582
583     return null;
584   }
585
586   /**
587    * Adds a method ref constant.
588    */

589   public MethodRefConstant addMethodRef(String JavaDoc className,
590                     String JavaDoc name,
591                     String JavaDoc type)
592   {
593     MethodRefConstant entry = getMethodRef(className, name, type);
594
595     if (entry != null)
596       return entry;
597
598     ClassConstant classEntry = addClass(className);
599     NameAndTypeConstant typeEntry = addNameAndType(name, type);
600
601     entry = new MethodRefConstant(this, _entries.size(),
602                   classEntry.getIndex(),
603                   typeEntry.getIndex());
604
605     addConstant(entry);
606
607     return entry;
608   }
609
610   /**
611    * Gets an interface constant.
612    */

613   public InterfaceMethodRefConstant getInterfaceRef(String JavaDoc className,
614                             String JavaDoc name,
615                             String JavaDoc type)
616   {
617     for (int i = 0; i < _entries.size(); i++) {
618       ConstantPoolEntry entry = _entries.get(i);
619
620       if (! (entry instanceof InterfaceMethodRefConstant))
621     continue;
622
623       InterfaceMethodRefConstant methodEntry;
624       methodEntry = (InterfaceMethodRefConstant) entry;
625
626       if (methodEntry.getClassName().equals(className) &&
627       methodEntry.getName().equals(name) &&
628       methodEntry.getType().equals(type))
629     return methodEntry;
630     }
631
632     return null;
633   }
634
635   /**
636    * Adds an interface ref constant.
637    */

638   public InterfaceMethodRefConstant addInterfaceRef(String JavaDoc className,
639                             String JavaDoc name,
640                             String JavaDoc type)
641   {
642     InterfaceMethodRefConstant entry = getInterfaceRef(className, name, type);
643
644     if (entry != null)
645       return entry;
646
647     ClassConstant classEntry = addClass(className);
648     NameAndTypeConstant typeEntry = addNameAndType(name, type);
649
650     entry = new InterfaceMethodRefConstant(this, _entries.size(),
651                        classEntry.getIndex(),
652                        typeEntry.getIndex());
653
654     addConstant(entry);
655
656     return entry;
657   }
658
659   /**
660    * Writes the contents of the pool.
661    */

662   void write(ByteCodeWriter out)
663     throws IOException JavaDoc
664   {
665     out.writeShort(_entries.size());
666
667     for (int i = 1; i < _entries.size(); i++) {
668       ConstantPoolEntry entry = _entries.get(i);
669
670       if (entry != null)
671     entry.write(out);
672     }
673   }
674 }
675
Popular Tags