KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > compiler > base > bytecode > ConstantPool


1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  *
3  * This file is part of the compiler and core tools for the AspectJ(tm)
4  * programming language; see http://aspectj.org
5  *
6  * The contents of this file are subject to the Mozilla Public License
7  * Version 1.1 (the "License"); you may not use this file except in
8  * compliance with the License. You may obtain a copy of the License at
9  * either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/.
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is AspectJ.
17  *
18  * The Initial Developer of the Original Code is Xerox Corporation. Portions
19  * created by Xerox Corporation are Copyright (C) 1999-2002 Xerox Corporation.
20  * All Rights Reserved.
21  *
22  * Contributor(s):
23  */

24
25 package org.aspectj.compiler.base.bytecode;
26
27 import java.io.*;
28 import java.lang.reflect.Modifier JavaDoc;
29
30 class ConstantPool {
31     Entry[] constants;
32
33     public Entry getEntry(int index) {
34         return constants[index];
35     }
36
37     public Class JavaDoc getClass(int index) {
38         return (Class JavaDoc)constants[index];
39     }
40
41     public UTF getUTF(int index) {
42         //System.err.println("getUTF: "+index);
43
if (index > constants.length) {
44             throw new InvalidClassFileException("invalid constant: "+index);
45         }
46         return (UTF)constants[index];
47     }
48
49     private UTF readUTF(DataInputStream stream) throws IOException {
50         int index = stream.readUnsignedShort();
51         //System.err.println("readUTF: "+index+", "+constants[index+1]+", "+constants[index-1]);
52
Entry entry = constants[index];
53         if (entry == null) {
54             entry = new UTF();
55             constants[index] = entry;
56         } else {
57             // check that entry is a UTF
58
}
59         return (UTF)entry;
60     }
61
62     private Class JavaDoc readClass(DataInputStream stream) throws IOException {
63         int index = stream.readUnsignedShort();
64         //System.err.println("clas: "+index);
65

66         Entry entry = constants[index];
67         if (entry == null) {
68             entry = new Class JavaDoc();
69             constants[index] = entry;
70         } else {
71             // check that entry is a Class
72
}
73         return (Class JavaDoc)entry;
74     }
75
76     private NameAndType readNameAndType(DataInputStream stream) throws IOException {
77         int index = stream.readUnsignedShort();
78         //System.err.println("nat: "+index);
79
Entry entry = constants[index];
80         if (entry == null) {
81             entry = new NameAndType();
82             constants[index] = entry;
83         } else {
84             // check that entry is a NameAndType
85
}
86         return (NameAndType)entry;
87     }
88
89     public Entry readEntry(int tag, DataInputStream stream) throws IOException {
90         Entry entry=null;
91         switch (tag) {
92         case CLASS_TAG:
93             entry = new Class JavaDoc(); break;
94         case FIELD_REF_TAG:
95             entry = new FieldRef(); break;
96         case METHOD_REF_TAG:
97             entry = new MethodRef(); break;
98         case INTERFACE_METHOD_REF_TAG:
99             entry = new InterfaceMethodRef(); break;
100         case STRING_TAG:
101             entry = new String JavaDoc(); break;
102         case INTEGER_TAG:
103             entry = new Integer JavaDoc(); break;
104         case FLOAT_TAG:
105             entry = new Float JavaDoc(); break;
106         case LONG_TAG:
107             entry = new Long JavaDoc(); break;
108         case DOUBLE_TAG:
109             entry = new Double JavaDoc(); break;
110         case NAME_AND_TYPE_TAG:
111             entry = new NameAndType(); break;
112         case UTF_TAG:
113             entry = new UTF(); break;
114         }
115         entry.readFrom(stream);
116         return entry;
117     }
118
119
120     public void readFrom(DataInputStream stream) throws IOException {
121         int constantCount = stream.readUnsignedShort();
122         //System.err.println("constants: "+constantCount);
123
constants = new Entry[constantCount];
124         for(int index = 1; index<constantCount; index++) {
125             int tag = stream.readUnsignedByte();
126             //System.err.println(""+index+": "+tag);
127

128             Entry entry = constants[index];
129             if (entry != null) {
130                 //System.err.println("entry exists: "+entry+", "+tag);
131
// check tag vs. entry.getTag()
132
entry.readFrom(stream);
133             } else {
134                 entry = readEntry(tag, stream);
135                 constants[index] = entry;
136             }
137             if (tag == DOUBLE_TAG || tag == LONG_TAG) {
138                 index++;
139             }
140         }
141     }
142
143
144     abstract class Entry {
145         int index;
146         void writeIndex(DataOutputStream stream) throws IOException {
147             stream.writeShort(index);
148         }
149         abstract void readFrom(DataInputStream stream) throws IOException;
150         abstract void writeTo(DataOutputStream stream) throws IOException;
151
152         public int getIndex() {
153             return index;
154         }
155     }
156
157     static final int CLASS_TAG = 7;
158     class Class extends Entry {
159         UTF name;
160
161         void readFrom(DataInputStream stream) throws IOException {
162             name = readUTF(stream);
163         }
164
165         void writeTo(DataOutputStream stream) throws IOException {
166             stream.writeByte(CLASS_TAG);
167             name.writeIndex(stream);
168         }
169
170         public java.lang.String JavaDoc getName() {
171             return name.value.replace('/', '.');
172         }
173
174         public java.lang.String JavaDoc toString() {
175             if (name == null) return "Class()";
176             return "Class("+name.value+")";
177         }
178     }
179
180     abstract class MemberRef extends Entry {
181         Class JavaDoc _class;
182         NameAndType nameAndType;
183
184         void readFrom(DataInputStream stream) throws IOException {
185             _class = readClass(stream);
186             nameAndType = readNameAndType(stream);
187         }
188
189         void writeTo(DataOutputStream stream) throws IOException {
190             stream.writeByte(getTag());
191             _class.writeIndex(stream);
192             nameAndType.writeIndex(stream);
193         }
194
195         abstract int getTag();
196
197         public java.lang.String JavaDoc toString() {
198             return "MemberRef(" + _class + " " + nameAndType + ")";
199         }
200     }
201
202     static final int FIELD_REF_TAG = 9;
203     class FieldRef extends MemberRef {
204         int getTag() { return FIELD_REF_TAG; }
205     }
206
207     static final int METHOD_REF_TAG = 10;
208     class MethodRef extends MemberRef {
209         int getTag() { return METHOD_REF_TAG; }
210     }
211
212     static final int INTERFACE_METHOD_REF_TAG = 11;
213     class InterfaceMethodRef extends MemberRef {
214
215         int getTag() { return INTERFACE_METHOD_REF_TAG; }
216     }
217
218     static final int STRING_TAG = 8;
219     class String extends Entry {
220         UTF value;
221
222         void readFrom(DataInputStream stream) throws IOException {
223             value = readUTF(stream);
224         }
225
226         void writeTo(DataOutputStream stream) throws IOException {
227             stream.writeByte(STRING_TAG);
228             value.writeIndex(stream);
229         }
230     }
231
232     static final int INTEGER_TAG = 3;
233     class Integer extends Entry {
234         int value;
235
236         void readFrom(DataInputStream stream) throws IOException {
237             value = stream.readInt();
238         }
239
240         void writeTo(DataOutputStream stream) throws IOException {
241             stream.writeByte(INTEGER_TAG);
242             stream.writeInt(value);
243         }
244     }
245
246     static final int FLOAT_TAG = 4;
247     class Float extends Entry {
248         float value;
249
250         void readFrom(DataInputStream stream) throws IOException {
251             value = stream.readFloat();
252         }
253
254         void writeTo(DataOutputStream stream) throws IOException {
255             stream.writeByte(FLOAT_TAG);
256             stream.writeFloat(value);
257         }
258     }
259
260     static final int LONG_TAG = 5;
261     class Long extends Entry {
262         long value;
263
264         void readFrom(DataInputStream stream) throws IOException {
265             value = stream.readLong();
266         }
267
268         void writeTo(DataOutputStream stream) throws IOException {
269             stream.writeByte(LONG_TAG);
270             stream.writeLong(value);
271         }
272     }
273
274     static final int DOUBLE_TAG = 6;
275     class Double extends Entry {
276         double value;
277
278         void readFrom(DataInputStream stream) throws IOException {
279             value = stream.readDouble();
280         }
281
282         void writeTo(DataOutputStream stream) throws IOException {
283             stream.writeByte(DOUBLE_TAG);
284             stream.writeDouble(value);
285         }
286     }
287
288     static final int NAME_AND_TYPE_TAG = 12;
289     class NameAndType extends Entry {
290         UTF name;
291         UTF descriptor;
292
293         void readFrom(DataInputStream stream) throws IOException {
294             name = readUTF(stream);
295             descriptor = readUTF(stream);
296         }
297
298         void writeTo(DataOutputStream stream) throws IOException {
299             stream.writeByte(NAME_AND_TYPE_TAG);
300             name.writeTo(stream);
301             descriptor.writeTo(stream);
302         }
303         public java.lang.String JavaDoc toString() {
304             return "NameAndType(" + name + " " + descriptor + ")";
305         }
306     }
307
308     static final int UTF_TAG = 1;
309     class UTF extends Entry {
310         java.lang.String JavaDoc value;
311
312         void readFrom(DataInputStream stream) throws IOException {
313             value = stream.readUTF();
314         }
315
316         void writeTo(DataOutputStream stream) throws IOException {
317             stream.writeByte(UTF_TAG);
318             stream.writeUTF(value);
319         }
320         public java.lang.String JavaDoc toString() {
321             return "UTF(" + value + ")";
322         }
323     }
324 }
325
Popular Tags