source: release-kits/lirk3/resources/gs3-release-maker/apache-ant-1.6.5/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/ConstantPool.java@ 14982

Last change on this file since 14982 was 14982, checked in by oranfry, 16 years ago

initial import of LiRK3

File size: 11.6 KB
Line 
1/*
2 * Copyright 2000-2004 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17package org.apache.tools.ant.taskdefs.optional.depend.constantpool;
18
19import java.io.DataInputStream;
20import java.io.IOException;
21import java.util.Enumeration;
22import java.util.Hashtable;
23import java.util.Vector;
24
25/**
26 * The constant pool of a Java class. The constant pool is a collection of
27 * constants used in a Java class file. It stores strings, constant values,
28 * class names, method names, field names etc.
29 *
30 * @see <a href="http://java.sun.com/docs/books/vmspec/">The Java Virtual
31 * Machine Specification</a>
32 */
33public class ConstantPool {
34
35 /** The entries in the constant pool. */
36 private Vector entries;
37
38 /**
39 * A Hashtable of UTF8 entries - used to get constant pool indexes of
40 * the UTF8 values quickly
41 */
42 private Hashtable utf8Indexes;
43
44 /** Initialise the constant pool. */
45 public ConstantPool() {
46 entries = new Vector();
47
48 // The zero index is never present in the constant pool itself so
49 // we add a null entry for it
50 entries.addElement(null);
51
52 utf8Indexes = new Hashtable();
53 }
54
55 /**
56 * Read the constant pool from a class input stream.
57 *
58 * @param classStream the DataInputStream of a class file.
59 * @exception IOException if there is a problem reading the constant pool
60 * from the stream
61 */
62 public void read(DataInputStream classStream) throws IOException {
63 int numEntries = classStream.readUnsignedShort();
64
65 for (int i = 1; i < numEntries;) {
66 ConstantPoolEntry nextEntry
67 = ConstantPoolEntry.readEntry(classStream);
68
69 i += nextEntry.getNumEntries();
70
71 addEntry(nextEntry);
72 }
73 }
74
75 /**
76 * Get the size of the constant pool.
77 *
78 * @return the size of the constant pool
79 */
80 public int size() {
81 return entries.size();
82 }
83
84 /**
85 * Add an entry to the constant pool.
86 *
87 * @param entry the new entry to be added to the constant pool.
88 * @return the index into the constant pool at which the entry is
89 * stored.
90 */
91 public int addEntry(ConstantPoolEntry entry) {
92 int index = entries.size();
93
94 entries.addElement(entry);
95
96 int numSlots = entry.getNumEntries();
97
98 // add null entries for any additional slots required.
99 for (int j = 0; j < numSlots - 1; ++j) {
100 entries.addElement(null);
101 }
102
103 if (entry instanceof Utf8CPInfo) {
104 Utf8CPInfo utf8Info = (Utf8CPInfo) entry;
105
106 utf8Indexes.put(utf8Info.getValue(), new Integer(index));
107 }
108
109 return index;
110 }
111
112 /**
113 * Resolve the entries in the constant pool. Resolution of the constant
114 * pool involves transforming indexes to other constant pool entries
115 * into the actual data for that entry.
116 */
117 public void resolve() {
118 for (Enumeration i = entries.elements(); i.hasMoreElements();) {
119 ConstantPoolEntry poolInfo = (ConstantPoolEntry) i.nextElement();
120
121 if (poolInfo != null && !poolInfo.isResolved()) {
122 poolInfo.resolve(this);
123 }
124 }
125 }
126
127
128 /**
129 * Get an constant pool entry at a particular index.
130 *
131 * @param index the index into the constant pool.
132 * @return the constant pool entry at that index.
133 */
134 public ConstantPoolEntry getEntry(int index) {
135 return (ConstantPoolEntry) entries.elementAt(index);
136 }
137
138 /**
139 * Get the index of a given UTF8 constant pool entry.
140 *
141 * @param value the string value of the UTF8 entry.
142 * @return the index at which the given string occurs in the constant
143 * pool or -1 if the value does not occur.
144 */
145 public int getUTF8Entry(String value) {
146 int index = -1;
147 Integer indexInteger = (Integer) utf8Indexes.get(value);
148
149 if (indexInteger != null) {
150 index = indexInteger.intValue();
151 }
152
153 return index;
154 }
155
156 /**
157 * Get the index of a given CONSTANT_CLASS entry in the constant pool.
158 *
159 * @param className the name of the class for which the class entry
160 * index is required.
161 * @return the index at which the given class entry occurs in the
162 * constant pool or -1 if the value does not occur.
163 */
164 public int getClassEntry(String className) {
165 int index = -1;
166
167 for (int i = 0; i < entries.size() && index == -1; ++i) {
168 Object element = entries.elementAt(i);
169
170 if (element instanceof ClassCPInfo) {
171 ClassCPInfo classinfo = (ClassCPInfo) element;
172
173 if (classinfo.getClassName().equals(className)) {
174 index = i;
175 }
176 }
177 }
178
179 return index;
180 }
181
182 /**
183 * Get the index of a given constant value entry in the constant pool.
184 *
185 * @param constantValue the constant value for which the index is
186 * required.
187 * @return the index at which the given value entry occurs in the
188 * constant pool or -1 if the value does not occur.
189 */
190 public int getConstantEntry(Object constantValue) {
191 int index = -1;
192
193 for (int i = 0; i < entries.size() && index == -1; ++i) {
194 Object element = entries.elementAt(i);
195
196 if (element instanceof ConstantCPInfo) {
197 ConstantCPInfo constantEntry = (ConstantCPInfo) element;
198
199 if (constantEntry.getValue().equals(constantValue)) {
200 index = i;
201 }
202 }
203 }
204
205 return index;
206 }
207
208 /**
209 * Get the index of a given CONSTANT_METHODREF entry in the constant
210 * pool.
211 *
212 * @param methodClassName the name of the class which contains the
213 * method being referenced.
214 * @param methodName the name of the method being referenced.
215 * @param methodType the type descriptor of the method being referenced.
216 * @return the index at which the given method ref entry occurs in the
217 * constant pool or -1 if the value does not occur.
218 */
219 public int getMethodRefEntry(String methodClassName, String methodName,
220 String methodType) {
221 int index = -1;
222
223 for (int i = 0; i < entries.size() && index == -1; ++i) {
224 Object element = entries.elementAt(i);
225
226 if (element instanceof MethodRefCPInfo) {
227 MethodRefCPInfo methodRefEntry = (MethodRefCPInfo) element;
228
229 if (methodRefEntry.getMethodClassName().equals(methodClassName)
230 && methodRefEntry.getMethodName().equals(methodName)
231 && methodRefEntry.getMethodType().equals(methodType)) {
232 index = i;
233 }
234 }
235 }
236
237 return index;
238 }
239
240 /**
241 * Get the index of a given CONSTANT_INTERFACEMETHODREF entry in the
242 * constant pool.
243 *
244 * @param interfaceMethodClassName the name of the interface which
245 * contains the method being referenced.
246 * @param interfaceMethodName the name of the method being referenced.
247 * @param interfaceMethodType the type descriptor of the method being
248 * referenced.
249 * @return the index at which the given method ref entry occurs in the
250 * constant pool or -1 if the value does not occur.
251 */
252 public int getInterfaceMethodRefEntry(String interfaceMethodClassName,
253 String interfaceMethodName,
254 String interfaceMethodType) {
255 int index = -1;
256
257 for (int i = 0; i < entries.size() && index == -1; ++i) {
258 Object element = entries.elementAt(i);
259
260 if (element instanceof InterfaceMethodRefCPInfo) {
261 InterfaceMethodRefCPInfo interfaceMethodRefEntry
262 = (InterfaceMethodRefCPInfo) element;
263
264 if (interfaceMethodRefEntry.getInterfaceMethodClassName().equals(interfaceMethodClassName)
265 && interfaceMethodRefEntry.getInterfaceMethodName().equals(interfaceMethodName)
266 && interfaceMethodRefEntry.getInterfaceMethodType().equals(interfaceMethodType)) {
267 index = i;
268 }
269 }
270 }
271
272 return index;
273 }
274
275 /**
276 * Get the index of a given CONSTANT_FIELDREF entry in the constant
277 * pool.
278 *
279 * @param fieldClassName the name of the class which contains the field
280 * being referenced.
281 * @param fieldName the name of the field being referenced.
282 * @param fieldType the type descriptor of the field being referenced.
283 * @return the index at which the given field ref entry occurs in the
284 * constant pool or -1 if the value does not occur.
285 */
286 public int getFieldRefEntry(String fieldClassName, String fieldName,
287 String fieldType) {
288 int index = -1;
289
290 for (int i = 0; i < entries.size() && index == -1; ++i) {
291 Object element = entries.elementAt(i);
292
293 if (element instanceof FieldRefCPInfo) {
294 FieldRefCPInfo fieldRefEntry = (FieldRefCPInfo) element;
295
296 if (fieldRefEntry.getFieldClassName().equals(fieldClassName)
297 && fieldRefEntry.getFieldName().equals(fieldName)
298 && fieldRefEntry.getFieldType().equals(fieldType)) {
299 index = i;
300 }
301 }
302 }
303
304 return index;
305 }
306
307 /**
308 * Get the index of a given CONSTANT_NAMEANDTYPE entry in the constant
309 * pool.
310 *
311 * @param name the name
312 * @param type the type
313 * @return the index at which the given NameAndType entry occurs in the
314 * constant pool or -1 if the value does not occur.
315 */
316 public int getNameAndTypeEntry(String name, String type) {
317 int index = -1;
318
319 for (int i = 0; i < entries.size() && index == -1; ++i) {
320 Object element = entries.elementAt(i);
321
322 if (element instanceof NameAndTypeCPInfo) {
323 NameAndTypeCPInfo nameAndTypeEntry
324 = (NameAndTypeCPInfo) element;
325
326 if (nameAndTypeEntry.getName().equals(name)
327 && nameAndTypeEntry.getType().equals(type)) {
328 index = i;
329 }
330 }
331 }
332
333 return index;
334 }
335
336 /**
337 * Dump the constant pool to a string.
338 *
339 * @return the constant pool entries as strings
340 */
341 public String toString() {
342 StringBuffer sb = new StringBuffer("\n");
343 int size = entries.size();
344
345 for (int i = 0; i < size; ++i) {
346 sb.append("[" + i + "] = " + getEntry(i) + "\n");
347 }
348
349 return sb.toString();
350 }
351
352}
353
Note: See TracBrowser for help on using the repository browser.