1 | /* |
2 | * Copyright 1999-2005 Sun Microsystems, Inc. All Rights Reserved. |
3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 | * |
5 | * This code is free software; you can redistribute it and/or modify it |
6 | * under the terms of the GNU General Public License version 2 only, as |
7 | * published by the Free Software Foundation. Sun designates this |
8 | * particular file as subject to the "Classpath" exception as provided |
9 | * by Sun in the LICENSE file that accompanied this code. |
10 | * |
11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
14 | * version 2 for more details (a copy is included in the LICENSE file that |
15 | * accompanied this code). |
16 | * |
17 | * You should have received a copy of the GNU General Public License version |
18 | * 2 along with this work; if not, write to the Free Software Foundation, |
19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
20 | * |
21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
23 | * have any questions. |
24 | */ |
25 | |
26 | package com.sun.tools.javac.jvm; |
27 | |
28 | import java.util.*; |
29 | |
30 | import com.sun.tools.javac.util.*; |
31 | import com.sun.tools.javac.code.Symbol.*; |
32 | import com.sun.tools.javac.code.Type; |
33 | |
34 | /** An internal structure that corresponds to the constant pool of a classfile. |
35 | * |
36 | * <p><b>This is NOT part of any API supported by Sun Microsystems. If |
37 | * you write code that depends on this, you do so at your own risk. |
38 | * This code and its internal interfaces are subject to change or |
39 | * deletion without notice.</b> |
40 | */ |
41 | public class Pool { |
42 | |
43 | public static final int MAX_ENTRIES = 0xFFFF; |
44 | public static final int MAX_STRING_LENGTH = 0xFFFF; |
45 | |
46 | /** Index of next constant to be entered. |
47 | */ |
48 | int pp; |
49 | |
50 | /** The initial pool buffer. |
51 | */ |
52 | Object[] pool; |
53 | |
54 | /** A hashtable containing all constants in the pool. |
55 | */ |
56 | Map<Object,Integer> indices; |
57 | |
58 | /** Construct a pool with given number of elements and element array. |
59 | */ |
60 | public Pool(int pp, Object[] pool) { |
61 | this.pp = pp; |
62 | this.pool = pool; |
63 | this.indices = new HashMap<Object,Integer>(pool.length); |
64 | for (int i = 1; i < pp; i++) { |
65 | if (pool[i] != null) indices.put(pool[i], i); |
66 | } |
67 | } |
68 | |
69 | /** Construct an empty pool. |
70 | */ |
71 | public Pool() { |
72 | this(1, new Object[64]); |
73 | } |
74 | |
75 | /** Return the number of entries in the constant pool. |
76 | */ |
77 | public int numEntries() { |
78 | return pp; |
79 | } |
80 | |
81 | /** Remove everything from this pool. |
82 | */ |
83 | public void reset() { |
84 | pp = 1; |
85 | indices.clear(); |
86 | } |
87 | |
88 | /** Double pool buffer in size. |
89 | */ |
90 | private void doublePool() { |
91 | Object[] newpool = new Object[pool.length * 2]; |
92 | System.arraycopy(pool, 0, newpool, 0, pool.length); |
93 | pool = newpool; |
94 | } |
95 | |
96 | /** Place an object in the pool, unless it is already there. |
97 | * If object is a symbol also enter its owner unless the owner is a |
98 | * package. Return the object's index in the pool. |
99 | */ |
100 | public int put(Object value) { |
101 | if (value instanceof MethodSymbol) |
102 | value = new Method((MethodSymbol)value); |
103 | else if (value instanceof VarSymbol) |
104 | value = new Variable((VarSymbol)value); |
105 | // assert !(value instanceof Type.TypeVar); |
106 | Integer index = indices.get(value); |
107 | if (index == null) { |
108 | // System.err.println("put " + value + " " + value.getClass());//DEBUG |
109 | index = pp; |
110 | indices.put(value, index); |
111 | if (pp == pool.length) doublePool(); |
112 | pool[pp++] = value; |
113 | if (value instanceof Long || value instanceof Double) { |
114 | if (pp == pool.length) doublePool(); |
115 | pool[pp++] = null; |
116 | } |
117 | } |
118 | return index.intValue(); |
119 | } |
120 | |
121 | /** Return the given object's index in the pool, |
122 | * or -1 if object is not in there. |
123 | */ |
124 | public int get(Object o) { |
125 | Integer n = indices.get(o); |
126 | return n == null ? -1 : n.intValue(); |
127 | } |
128 | |
129 | static class Method extends DelegatedSymbol { |
130 | MethodSymbol m; |
131 | Method(MethodSymbol m) { |
132 | super(m); |
133 | this.m = m; |
134 | } |
135 | public boolean equals(Object other) { |
136 | if (!(other instanceof Method)) return false; |
137 | MethodSymbol o = ((Method)other).m; |
138 | return |
139 | o.name == m.name && |
140 | o.owner == m.owner && |
141 | o.type.equals(m.type); |
142 | } |
143 | public int hashCode() { |
144 | return |
145 | m.name.hashCode() * 33 + |
146 | m.owner.hashCode() * 9 + |
147 | m.type.hashCode(); |
148 | } |
149 | } |
150 | |
151 | static class Variable extends DelegatedSymbol { |
152 | VarSymbol v; |
153 | Variable(VarSymbol v) { |
154 | super(v); |
155 | this.v = v; |
156 | } |
157 | public boolean equals(Object other) { |
158 | if (!(other instanceof Variable)) return false; |
159 | VarSymbol o = ((Variable)other).v; |
160 | return |
161 | o.name == v.name && |
162 | o.owner == v.owner && |
163 | o.type.equals(v.type); |
164 | } |
165 | public int hashCode() { |
166 | return |
167 | v.name.hashCode() * 33 + |
168 | v.owner.hashCode() * 9 + |
169 | v.type.hashCode(); |
170 | } |
171 | } |
172 | } |