KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > bytecode > CpoolEntry


1 // Copyright (c) 1997 Per M.A. Bothner.
2
// This is free software; for terms and warranty disclaimer see ./COPYING.
3

4 package gnu.bytecode;
5 import java.io.*;
6
7 /**
8  * An entry in the constant pool for a ClassType.
9  * Each entry belong to the constant pool table of the "owning"
10  * ClassType. Hashing is used to make each entry unique (with a ClassType).
11  * By convention, each sub-class has a static get_const method which is
12  * used when a contant pool entry is need: The get_const method will
13  * return an existing matching entry if possible, or allocate a new
14  * one if needed.
15  * @author Per Bothner
16  */

17
18 abstract public class CpoolEntry
19 {
20   /** A hashvalue so we do not get duplicate constant pool entries. */
21   int hash;
22
23   /** This entry's index in the constant pool. */
24   public int index;
25
26   public int getIndex() { return index; }
27
28   /** The next entry in the same hash bucket
29    * (of the owning ConstantPool's hashTab). */

30   CpoolEntry next;
31
32   public abstract int getTag();
33
34   public int hashCode () { return hash; }
35
36   abstract void write(DataOutputStream str)
37        throws java.io.IOException JavaDoc;
38
39   /**
40    * Enter current element into cpool.hashTab.
41    */

42   void add_hashed (ConstantPool cpool)
43   {
44     CpoolEntry[] hashTab = cpool.hashTab;
45     int index = (hash & 0x7FFFFFFF) % hashTab.length;
46     next = hashTab[index];
47     hashTab[index] = this;
48   }
49
50   protected CpoolEntry () { }
51
52   public CpoolEntry (ConstantPool cpool, int h)
53   {
54      hash = h;
55      if (cpool.locked)
56        throw new Error JavaDoc("adding new entry to locked contant pool");
57      index = ++cpool.count;
58
59      // (Re-)allocate the cpool.pool array if need be.
60
if (cpool.pool == null)
61     cpool.pool = new CpoolEntry[60];
62      else if (index >= cpool.pool.length)
63        {
64      int old_size = cpool.pool.length;
65      int new_size = 2 * cpool.pool.length;
66      CpoolEntry[] new_pool = new CpoolEntry[new_size];
67      for (int i = 0; i < old_size; i++)
68        new_pool[i] = cpool.pool[i];
69      cpool.pool = new_pool;
70        }
71
72      // Re-hash cpool.hashTab hash_table if needed.
73
if (cpool.hashTab == null || index >= 0.60 * cpool.hashTab.length)
74        cpool.rehash();
75
76      // Enter into cpool.constant_pool array.
77
cpool.pool[index] = this;
78      // Enter into cpool.hashTab hash table.
79
add_hashed (cpool);
80   }
81
82   /** Print this constant pool entry.
83    * If verbosity==0, print very tersely (no extraneous text).
84    * If verbosity==1, prefix the type of the constant.
85    * If verbosity==2, add more descriptive text. */

86
87   public abstract void print (ClassTypeWriter dst, int verbosity);
88 };
89
Popular Tags