KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > optional > depend > constantpool > ConstantPool


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18 package org.apache.tools.ant.taskdefs.optional.depend.constantpool;
19
20 import java.io.DataInputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.util.Enumeration JavaDoc;
23 import java.util.Hashtable JavaDoc;
24 import java.util.Vector JavaDoc;
25
26 /**
27  * The constant pool of a Java class. The constant pool is a collection of
28  * constants used in a Java class file. It stores strings, constant values,
29  * class names, method names, field names etc.
30  *
31  * @see <a HREF="http://java.sun.com/docs/books/vmspec/">The Java Virtual
32  * Machine Specification</a>
33  */

34 public class ConstantPool {
35
36     /** The entries in the constant pool. */
37     private Vector JavaDoc entries;
38
39     /**
40      * A Hashtable of UTF8 entries - used to get constant pool indexes of
41      * the UTF8 values quickly
42      */

43     private Hashtable JavaDoc utf8Indexes;
44
45     /** Initialise the constant pool. */
46     public ConstantPool() {
47         entries = new Vector JavaDoc();
48
49         // The zero index is never present in the constant pool itself so
50
// we add a null entry for it
51
entries.addElement(null);
52
53         utf8Indexes = new Hashtable JavaDoc();
54     }
55
56     /**
57      * Read the constant pool from a class input stream.
58      *
59      * @param classStream the DataInputStream of a class file.
60      * @exception IOException if there is a problem reading the constant pool
61      * from the stream
62      */

63     public void read(DataInputStream JavaDoc classStream) throws IOException JavaDoc {
64         int numEntries = classStream.readUnsignedShort();
65
66         for (int i = 1; i < numEntries;) {
67             ConstantPoolEntry nextEntry
68                  = ConstantPoolEntry.readEntry(classStream);
69
70             i += nextEntry.getNumEntries();
71
72             addEntry(nextEntry);
73         }
74     }
75
76     /**
77      * Get the size of the constant pool.
78      *
79      * @return the size of the constant pool
80      */

81     public int size() {
82         return entries.size();
83     }
84
85     /**
86      * Add an entry to the constant pool.
87      *
88      * @param entry the new entry to be added to the constant pool.
89      * @return the index into the constant pool at which the entry is
90      * stored.
91      */

92     public int addEntry(ConstantPoolEntry entry) {
93         int index = entries.size();
94
95         entries.addElement(entry);
96
97         int numSlots = entry.getNumEntries();
98
99         // add null entries for any additional slots required.
100
for (int j = 0; j < numSlots - 1; ++j) {
101             entries.addElement(null);
102         }
103
104         if (entry instanceof Utf8CPInfo) {
105             Utf8CPInfo utf8Info = (Utf8CPInfo) entry;
106
107             utf8Indexes.put(utf8Info.getValue(), new Integer JavaDoc(index));
108         }
109
110         return index;
111     }
112
113     /**
114      * Resolve the entries in the constant pool. Resolution of the constant
115      * pool involves transforming indexes to other constant pool entries
116      * into the actual data for that entry.
117      */

118     public void resolve() {
119         for (Enumeration JavaDoc i = entries.elements(); i.hasMoreElements();) {
120             ConstantPoolEntry poolInfo = (ConstantPoolEntry) i.nextElement();
121
122             if (poolInfo != null && !poolInfo.isResolved()) {
123                 poolInfo.resolve(this);
124             }
125         }
126     }
127
128
129     /**
130      * Get an constant pool entry at a particular index.
131      *
132      * @param index the index into the constant pool.
133      * @return the constant pool entry at that index.
134      */

135     public ConstantPoolEntry getEntry(int index) {
136         return (ConstantPoolEntry) entries.elementAt(index);
137     }
138
139     /**
140      * Get the index of a given UTF8 constant pool entry.
141      *
142      * @param value the string value of the UTF8 entry.
143      * @return the index at which the given string occurs in the constant
144      * pool or -1 if the value does not occur.
145      */

146     public int getUTF8Entry(String JavaDoc value) {
147         int index = -1;
148         Integer JavaDoc indexInteger = (Integer JavaDoc) utf8Indexes.get(value);
149
150         if (indexInteger != null) {
151             index = indexInteger.intValue();
152         }
153
154         return index;
155     }
156
157     /**
158      * Get the index of a given CONSTANT_CLASS entry in the constant pool.
159      *
160      * @param className the name of the class for which the class entry
161      * index is required.
162      * @return the index at which the given class entry occurs in the
163      * constant pool or -1 if the value does not occur.
164      */

165     public int getClassEntry(String JavaDoc className) {
166         int index = -1;
167
168         for (int i = 0; i < entries.size() && index == -1; ++i) {
169             Object JavaDoc element = entries.elementAt(i);
170
171             if (element instanceof ClassCPInfo) {
172                 ClassCPInfo classinfo = (ClassCPInfo) element;
173
174                 if (classinfo.getClassName().equals(className)) {
175                     index = i;
176                 }
177             }
178         }
179
180         return index;
181     }
182
183     /**
184      * Get the index of a given constant value entry in the constant pool.
185      *
186      * @param constantValue the constant value for which the index is
187      * required.
188      * @return the index at which the given value entry occurs in the
189      * constant pool or -1 if the value does not occur.
190      */

191     public int getConstantEntry(Object JavaDoc constantValue) {
192         int index = -1;
193
194         for (int i = 0; i < entries.size() && index == -1; ++i) {
195             Object JavaDoc element = entries.elementAt(i);
196
197             if (element instanceof ConstantCPInfo) {
198                 ConstantCPInfo constantEntry = (ConstantCPInfo) element;
199
200                 if (constantEntry.getValue().equals(constantValue)) {
201                     index = i;
202                 }
203             }
204         }
205
206         return index;
207     }
208
209     /**
210      * Get the index of a given CONSTANT_METHODREF entry in the constant
211      * pool.
212      *
213      * @param methodClassName the name of the class which contains the
214      * method being referenced.
215      * @param methodName the name of the method being referenced.
216      * @param methodType the type descriptor of the method being referenced.
217      * @return the index at which the given method ref entry occurs in the
218      * constant pool or -1 if the value does not occur.
219      */

220     public int getMethodRefEntry(String JavaDoc methodClassName, String JavaDoc methodName,
221                                  String JavaDoc methodType) {
222         int index = -1;
223
224         for (int i = 0; i < entries.size() && index == -1; ++i) {
225             Object JavaDoc element = entries.elementAt(i);
226
227             if (element instanceof MethodRefCPInfo) {
228                 MethodRefCPInfo methodRefEntry = (MethodRefCPInfo) element;
229
230                 if (methodRefEntry.getMethodClassName().equals(methodClassName)
231                      && methodRefEntry.getMethodName().equals(methodName)
232                      && methodRefEntry.getMethodType().equals(methodType)) {
233                     index = i;
234                 }
235             }
236         }
237
238         return index;
239     }
240
241     /**
242      * Get the index of a given CONSTANT_INTERFACEMETHODREF entry in the
243      * constant pool.
244      *
245      * @param interfaceMethodClassName the name of the interface which
246      * contains the method being referenced.
247      * @param interfaceMethodName the name of the method being referenced.
248      * @param interfaceMethodType the type descriptor of the method being
249      * referenced.
250      * @return the index at which the given method ref entry occurs in the
251      * constant pool or -1 if the value does not occur.
252      */

253     public int getInterfaceMethodRefEntry(String JavaDoc interfaceMethodClassName,
254                                           String JavaDoc interfaceMethodName,
255                                           String JavaDoc interfaceMethodType) {
256         int index = -1;
257
258         for (int i = 0; i < entries.size() && index == -1; ++i) {
259             Object JavaDoc element = entries.elementAt(i);
260
261             if (element instanceof InterfaceMethodRefCPInfo) {
262                 InterfaceMethodRefCPInfo interfaceMethodRefEntry
263                      = (InterfaceMethodRefCPInfo) element;
264
265                 if (interfaceMethodRefEntry.getInterfaceMethodClassName().equals(
266                         interfaceMethodClassName)
267                      && interfaceMethodRefEntry.getInterfaceMethodName().equals(
268                          interfaceMethodName)
269                      && interfaceMethodRefEntry.getInterfaceMethodType().equals(
270                          interfaceMethodType)) {
271                     index = i;
272                 }
273             }
274         }
275
276         return index;
277     }
278
279     /**
280      * Get the index of a given CONSTANT_FIELDREF entry in the constant
281      * pool.
282      *
283      * @param fieldClassName the name of the class which contains the field
284      * being referenced.
285      * @param fieldName the name of the field being referenced.
286      * @param fieldType the type descriptor of the field being referenced.
287      * @return the index at which the given field ref entry occurs in the
288      * constant pool or -1 if the value does not occur.
289      */

290     public int getFieldRefEntry(String JavaDoc fieldClassName, String JavaDoc fieldName,
291                                 String JavaDoc fieldType) {
292         int index = -1;
293
294         for (int i = 0; i < entries.size() && index == -1; ++i) {
295             Object JavaDoc element = entries.elementAt(i);
296
297             if (element instanceof FieldRefCPInfo) {
298                 FieldRefCPInfo fieldRefEntry = (FieldRefCPInfo) element;
299
300                 if (fieldRefEntry.getFieldClassName().equals(fieldClassName)
301                      && fieldRefEntry.getFieldName().equals(fieldName)
302                      && fieldRefEntry.getFieldType().equals(fieldType)) {
303                     index = i;
304                 }
305             }
306         }
307
308         return index;
309     }
310
311     /**
312      * Get the index of a given CONSTANT_NAMEANDTYPE entry in the constant
313      * pool.
314      *
315      * @param name the name
316      * @param type the type
317      * @return the index at which the given NameAndType entry occurs in the
318      * constant pool or -1 if the value does not occur.
319      */

320     public int getNameAndTypeEntry(String JavaDoc name, String JavaDoc type) {
321         int index = -1;
322
323         for (int i = 0; i < entries.size() && index == -1; ++i) {
324             Object JavaDoc element = entries.elementAt(i);
325
326             if (element instanceof NameAndTypeCPInfo) {
327                 NameAndTypeCPInfo nameAndTypeEntry
328                     = (NameAndTypeCPInfo) element;
329
330                 if (nameAndTypeEntry.getName().equals(name)
331                      && nameAndTypeEntry.getType().equals(type)) {
332                     index = i;
333                 }
334             }
335         }
336
337         return index;
338     }
339
340     /**
341      * Dump the constant pool to a string.
342      *
343      * @return the constant pool entries as strings
344      */

345     public String JavaDoc toString() {
346         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("\n");
347         int size = entries.size();
348
349         for (int i = 0; i < size; ++i) {
350             sb.append("[" + i + "] = " + getEntry(i) + "\n");
351         }
352
353         return sb.toString();
354     }
355
356 }
357
358
Popular Tags