01: // Copyright (c) 1997 Per M.A. Bothner.
02: // This is free software; for terms and warranty disclaimer see ./COPYING.
03:
04: package gnu.bytecode;
05:
06: import java.io.*;
07:
08: /**
09: * An entry in the constant pool for a ClassType.
10: * Each entry belong to the constant pool table of the "owning"
11: * ClassType. Hashing is used to make each entry unique (with a ClassType).
12: * By convention, each sub-class has a static get_const method which is
13: * used when a contant pool entry is need: The get_const method will
14: * return an existing matching entry if possible, or allocate a new
15: * one if needed.
16: * @author Per Bothner
17: */
18:
19: abstract public class CpoolEntry {
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() {
27: return index;
28: }
29:
30: /** The next entry in the same hash bucket
31: * (of the owning ConstantPool's hashTab). */
32: CpoolEntry next;
33:
34: public abstract int getTag();
35:
36: public int hashCode() {
37: return hash;
38: }
39:
40: abstract void write(DataOutputStream str)
41: throws java.io.IOException;
42:
43: /**
44: * Enter current element into cpool.hashTab.
45: */
46: void add_hashed(ConstantPool cpool) {
47: CpoolEntry[] hashTab = cpool.hashTab;
48: int index = (hash & 0x7FFFFFFF) % hashTab.length;
49: next = hashTab[index];
50: hashTab[index] = this ;
51: }
52:
53: protected CpoolEntry() {
54: }
55:
56: public CpoolEntry(ConstantPool cpool, int h) {
57: hash = h;
58: if (cpool.locked)
59: throw new Error("adding new entry to locked contant pool");
60: index = ++cpool.count;
61:
62: // (Re-)allocate the cpool.pool array if need be.
63: if (cpool.pool == null)
64: cpool.pool = new CpoolEntry[60];
65: else if (index >= cpool.pool.length) {
66: int old_size = cpool.pool.length;
67: int new_size = 2 * cpool.pool.length;
68: CpoolEntry[] new_pool = new CpoolEntry[new_size];
69: for (int i = 0; i < old_size; i++)
70: new_pool[i] = cpool.pool[i];
71: cpool.pool = new_pool;
72: }
73:
74: // Re-hash cpool.hashTab hash_table if needed.
75: if (cpool.hashTab == null
76: || index >= 0.60 * cpool.hashTab.length)
77: cpool.rehash();
78:
79: // Enter into cpool.constant_pool array.
80: cpool.pool[index] = this ;
81: // Enter into cpool.hashTab hash table.
82: add_hashed(cpool);
83: }
84:
85: /** Print this constant pool entry.
86: * If verbosity==0, print very tersely (no extraneous text).
87: * If verbosity==1, prefix the type of the constant.
88: * If verbosity==2, add more descriptive text. */
89:
90: public abstract void print(ClassTypeWriter dst, int verbosity);
91: };
|