comparison agent/src/share/classes/sun/jvm/hotspot/oops/ConstantPool.java @ 0:a61af66fc99e jdk7-b24

Initial load
author duke
date Sat, 01 Dec 2007 00:00:00 +0000
parents
children ba764ed4b6f2
comparison
equal deleted inserted replaced
-1:000000000000 0:a61af66fc99e
1 /*
2 * Copyright 2000-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.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 *
23 */
24
25 package sun.jvm.hotspot.oops;
26
27 import java.io.*;
28 import java.util.*;
29 import sun.jvm.hotspot.debugger.*;
30 import sun.jvm.hotspot.runtime.*;
31 import sun.jvm.hotspot.types.*;
32 import sun.jvm.hotspot.utilities.*;
33
34 // A ConstantPool is an array containing class constants
35 // as described in the class file
36
37 public class ConstantPool extends Array implements ClassConstants {
38 // Used for debugging this code
39 private static final boolean DEBUG = false;
40
41 protected void debugMessage(String message) {
42 System.out.println(message);
43 }
44
45 static {
46 VM.registerVMInitializedObserver(new Observer() {
47 public void update(Observable o, Object data) {
48 initialize(VM.getVM().getTypeDataBase());
49 }
50 });
51 }
52
53 private static synchronized void initialize(TypeDataBase db) throws WrongTypeException {
54 Type type = db.lookupType("constantPoolOopDesc");
55 tags = new OopField(type.getOopField("_tags"), 0);
56 cache = new OopField(type.getOopField("_cache"), 0);
57 poolHolder = new OopField(type.getOopField("_pool_holder"), 0);
58 headerSize = type.getSize();
59 elementSize = db.getOopSize();
60 }
61
62 ConstantPool(OopHandle handle, ObjectHeap heap) {
63 super(handle, heap);
64 }
65
66 public boolean isConstantPool() { return true; }
67
68 private static OopField tags;
69 private static OopField cache;
70 private static OopField poolHolder;
71
72
73 private static long headerSize;
74 private static long elementSize;
75
76 public TypeArray getTags() { return (TypeArray) tags.getValue(this); }
77 public ConstantPoolCache getCache() { return (ConstantPoolCache) cache.getValue(this); }
78 public Klass getPoolHolder() { return (Klass) poolHolder.getValue(this); }
79
80 private long indexOffset(long index) {
81 if (Assert.ASSERTS_ENABLED) {
82 Assert.that(index > 0 && index < getLength(), "invalid cp index");
83 }
84 return (index * elementSize) + headerSize;
85 }
86
87 public ConstantTag getTagAt(long index) {
88 return new ConstantTag(getTags().getByteAt((int) index));
89 }
90
91 public Oop getObjAt(long index){
92 return getHeap().newOop(getHandle().getOopHandleAt(indexOffset(index)));
93 }
94
95 public Symbol getSymbolAt(long index) {
96 return (Symbol) getObjAt(index);
97 }
98
99 public int getIntAt(long index){
100 return getHandle().getJIntAt(indexOffset(index));
101 }
102
103 public float getFloatAt(long index){
104 return getHandle().getJFloatAt(indexOffset(index));
105 }
106
107 public long getLongAt(long index) {
108 int oneHalf = getHandle().getJIntAt(indexOffset(index + 1));
109 int otherHalf = getHandle().getJIntAt(indexOffset(index));
110 // buildLongFromIntsPD accepts higher address value, lower address value
111 // in that order.
112 return VM.getVM().buildLongFromIntsPD(oneHalf, otherHalf);
113 }
114
115 public double getDoubleAt(long index) {
116 return Double.longBitsToDouble(getLongAt(index));
117 }
118
119 public int getFieldOrMethodAt(int which) {
120 if (DEBUG) {
121 System.err.print("ConstantPool.getFieldOrMethodAt(" + which + "): new index = ");
122 }
123 int i = -1;
124 ConstantPoolCache cache = getCache();
125 if (cache == null) {
126 i = which;
127 } else {
128 // change byte-ordering and go via cache
129 i = cache.getEntryAt(0xFFFF & VM.getVM().getBytes().swapShort((short) which)).getConstantPoolIndex();
130 }
131 if (Assert.ASSERTS_ENABLED) {
132 Assert.that(getTagAt(i).isFieldOrMethod(), "Corrupted constant pool");
133 }
134 if (DEBUG) {
135 System.err.println(i);
136 }
137 int res = getIntAt(i);
138 if (DEBUG) {
139 System.err.println("ConstantPool.getFieldOrMethodAt(" + i + "): result = " + res);
140 }
141 return res;
142 }
143
144 public int getNameAndTypeAt(int which) {
145 if (Assert.ASSERTS_ENABLED) {
146 Assert.that(getTagAt(which).isNameAndType(), "Corrupted constant pool");
147 }
148 int i = getIntAt(which);
149 if (DEBUG) {
150 System.err.println("ConstantPool.getNameAndTypeAt(" + which + "): result = " + i);
151 }
152 return i;
153 }
154
155 public Symbol getNameRefAt(int which) {
156 int refIndex = getNameAndTypeAt(getNameAndTypeRefIndexAt(which));
157 int nameIndex = extractLowShortFromInt(refIndex);
158 return getSymbolAt(nameIndex);
159 }
160
161 public Symbol getSignatureRefAt(int which) {
162 int refIndex = getNameAndTypeAt(getNameAndTypeRefIndexAt(which));
163 int sigIndex = extractHighShortFromInt(refIndex);
164 return getSymbolAt(sigIndex);
165 }
166
167 // returns null, if not resolved.
168 public Klass getKlassRefAt(int which) {
169 if( ! getTagAt(which).isKlass()) return null;
170 return (Klass) getObjAt(which);
171 }
172
173 // returns null, if not resolved.
174 public InstanceKlass getFieldOrMethodKlassRefAt(int which) {
175 int refIndex = getFieldOrMethodAt(which);
176 int klassIndex = extractLowShortFromInt(refIndex);
177 return (InstanceKlass) getKlassRefAt(klassIndex);
178 }
179
180 // returns null, if not resolved.
181 public Method getMethodRefAt(int which) {
182 InstanceKlass klass = getFieldOrMethodKlassRefAt(which);
183 if (klass == null) return null;
184 Symbol name = getNameRefAt(which);
185 Symbol sig = getSignatureRefAt(which);
186 return klass.findMethod(name, sig);
187 }
188
189 // returns null, if not resolved.
190 public Field getFieldRefAt(int which) {
191 InstanceKlass klass = getFieldOrMethodKlassRefAt(which);
192 if (klass == null) return null;
193 Symbol name = getNameRefAt(which);
194 Symbol sig = getSignatureRefAt(which);
195 return klass.findField(name, sig);
196 }
197
198 public int getNameAndTypeRefIndexAt(int index) {
199 int refIndex = getFieldOrMethodAt(index);
200 if (DEBUG) {
201 System.err.println("ConstantPool.getNameAndTypeRefIndexAt(" + index + "): refIndex = " + refIndex);
202 }
203 int i = extractHighShortFromInt(refIndex);
204 if (DEBUG) {
205 System.err.println("ConstantPool.getNameAndTypeRefIndexAt(" + index + "): result = " + i);
206 }
207 return i;
208 }
209
210 /** Lookup for entries consisting of (name_index, signature_index) */
211 public int getNameRefIndexAt(int index) {
212 int refIndex = getNameAndTypeAt(index);
213 if (DEBUG) {
214 System.err.println("ConstantPool.getNameRefIndexAt(" + index + "): refIndex = " + refIndex);
215 }
216 int i = extractLowShortFromInt(refIndex);
217 if (DEBUG) {
218 System.err.println("ConstantPool.getNameRefIndexAt(" + index + "): result = " + i);
219 }
220 return i;
221 }
222
223 /** Lookup for entries consisting of (name_index, signature_index) */
224 public int getSignatureRefIndexAt(int index) {
225 int refIndex = getNameAndTypeAt(index);
226 if (DEBUG) {
227 System.err.println("ConstantPool.getSignatureRefIndexAt(" + index + "): refIndex = " + refIndex);
228 }
229 int i = extractHighShortFromInt(refIndex);
230 if (DEBUG) {
231 System.err.println("ConstantPool.getSignatureRefIndexAt(" + index + "): result = " + i);
232 }
233 return i;
234 }
235
236 final private static String[] nameForTag = new String[] {
237 };
238
239 private String nameForTag(int tag) {
240 switch (tag) {
241 case JVM_CONSTANT_Utf8: return "JVM_CONSTANT_Utf8";
242 case JVM_CONSTANT_Unicode: return "JVM_CONSTANT_Unicode";
243 case JVM_CONSTANT_Integer: return "JVM_CONSTANT_Integer";
244 case JVM_CONSTANT_Float: return "JVM_CONSTANT_Float";
245 case JVM_CONSTANT_Long: return "JVM_CONSTANT_Long";
246 case JVM_CONSTANT_Double: return "JVM_CONSTANT_Double";
247 case JVM_CONSTANT_Class: return "JVM_CONSTANT_Class";
248 case JVM_CONSTANT_String: return "JVM_CONSTANT_String";
249 case JVM_CONSTANT_Fieldref: return "JVM_CONSTANT_Fieldref";
250 case JVM_CONSTANT_Methodref: return "JVM_CONSTANT_Methodref";
251 case JVM_CONSTANT_InterfaceMethodref: return "JVM_CONSTANT_InterfaceMethodref";
252 case JVM_CONSTANT_NameAndType: return "JVM_CONSTANT_NameAndType";
253 case JVM_CONSTANT_Invalid: return "JVM_CONSTANT_Invalid";
254 case JVM_CONSTANT_UnresolvedClass: return "JVM_CONSTANT_UnresolvedClass";
255 case JVM_CONSTANT_ClassIndex: return "JVM_CONSTANT_ClassIndex";
256 case JVM_CONSTANT_UnresolvedString: return "JVM_CONSTANT_UnresolvedString";
257 case JVM_CONSTANT_StringIndex: return "JVM_CONSTANT_StringIndex";
258 }
259 throw new InternalError("unknown tag");
260 }
261
262 public void iterateFields(OopVisitor visitor, boolean doVMFields) {
263 super.iterateFields(visitor, doVMFields);
264 if (doVMFields) {
265 visitor.doOop(tags, true);
266 visitor.doOop(cache, true);
267 visitor.doOop(poolHolder, true);
268
269 final int length = (int) getLength();
270 // zero'th pool entry is always invalid. ignore it.
271 for (int index = 1; index < length; index++) {
272 int ctag = (int) getTags().getByteAt((int) index);
273 switch (ctag) {
274 case JVM_CONSTANT_ClassIndex:
275 case JVM_CONSTANT_StringIndex:
276 case JVM_CONSTANT_Integer:
277 visitor.doInt(new IntField(new NamedFieldIdentifier(nameForTag(ctag)), indexOffset(index), true), true);
278 break;
279
280 case JVM_CONSTANT_Float:
281 visitor.doFloat(new FloatField(new NamedFieldIdentifier(nameForTag(ctag)), indexOffset(index), true), true);
282 break;
283
284 case JVM_CONSTANT_Long:
285 visitor.doLong(new LongField(new NamedFieldIdentifier(nameForTag(ctag)), indexOffset(index), true), true);
286 // long entries occupy two slots
287 index++;
288 break;
289
290 case JVM_CONSTANT_Double:
291 visitor.doDouble(new DoubleField(new NamedFieldIdentifier(nameForTag(ctag)), indexOffset(index), true), true);
292 // double entries occupy two slots
293 index++;
294 break;
295
296 case JVM_CONSTANT_UnresolvedClass:
297 case JVM_CONSTANT_Class:
298 case JVM_CONSTANT_UnresolvedString:
299 case JVM_CONSTANT_Utf8:
300 visitor.doOop(new OopField(new NamedFieldIdentifier(nameForTag(ctag)), indexOffset(index), true), true);
301 break;
302
303 case JVM_CONSTANT_Fieldref:
304 case JVM_CONSTANT_Methodref:
305 case JVM_CONSTANT_InterfaceMethodref:
306 case JVM_CONSTANT_NameAndType:
307 visitor.doInt(new IntField(new NamedFieldIdentifier(nameForTag(ctag)), indexOffset(index), true), true);
308 break;
309 }
310 }
311 }
312 /*
313 int length = getLength();
314 for (int index = 0; index < length; index++) {
315 long offset = baseOffset + (index + typeDataBase.getOopSize());
316 visitor.doOop(new IndexableField(index, offset, false), getObjAt(index));
317 }
318 */
319 }
320
321 public void writeBytes(OutputStream os) throws IOException {
322 // Map between any modified UTF-8 and it's constant pool index.
323 Map utf8ToIndex = new HashMap();
324 DataOutputStream dos = new DataOutputStream(os);
325 TypeArray tags = getTags();
326 int len = (int)getLength();
327 int ci = 0; // constant pool index
328
329 // collect all modified UTF-8 Strings from Constant Pool
330
331 for (ci = 1; ci < len; ci++) {
332 byte cpConstType = tags.getByteAt(ci);
333 if(cpConstType == JVM_CONSTANT_Utf8) {
334 Symbol sym = getSymbolAt(ci);
335 utf8ToIndex.put(sym.asString(), new Short((short) ci));
336 }
337 else if(cpConstType == JVM_CONSTANT_Long ||
338 cpConstType == JVM_CONSTANT_Double) {
339 ci++;
340 }
341 }
342
343
344 for(ci = 1; ci < len; ci++) {
345 int cpConstType = (int)tags.getByteAt(ci);
346 // write cp_info
347 // write constant type
348 switch(cpConstType) {
349 case JVM_CONSTANT_Utf8: {
350 dos.writeByte(cpConstType);
351 Symbol sym = getSymbolAt(ci);
352 dos.writeShort((short)sym.getLength());
353 dos.write(sym.asByteArray());
354 if (DEBUG) debugMessage("CP[" + ci + "] = modified UTF-8 " + sym.asString());
355 break;
356 }
357
358 case JVM_CONSTANT_Unicode:
359 throw new IllegalArgumentException("Unicode constant!");
360
361 case JVM_CONSTANT_Integer:
362 dos.writeByte(cpConstType);
363 dos.writeInt(getIntAt(ci));
364 if (DEBUG) debugMessage("CP[" + ci + "] = int " + getIntAt(ci));
365 break;
366
367 case JVM_CONSTANT_Float:
368 dos.writeByte(cpConstType);
369 dos.writeFloat(getFloatAt(ci));
370 if (DEBUG) debugMessage("CP[" + ci + "] = float " + getFloatAt(ci));
371 break;
372
373 case JVM_CONSTANT_Long: {
374 dos.writeByte(cpConstType);
375 long l = getLongAt(ci);
376 // long entries occupy two pool entries
377 ci++;
378 dos.writeLong(l);
379 break;
380 }
381
382 case JVM_CONSTANT_Double:
383 dos.writeByte(cpConstType);
384 dos.writeDouble(getDoubleAt(ci));
385 // double entries occupy two pool entries
386 ci++;
387 break;
388
389 case JVM_CONSTANT_Class: {
390 dos.writeByte(cpConstType);
391 // Klass already resolved. ConstantPool constains klassOop.
392 Klass refKls = (Klass) getObjAt(ci);
393 String klassName = refKls.getName().asString();
394 Short s = (Short) utf8ToIndex.get(klassName);
395 dos.writeShort(s.shortValue());
396 if (DEBUG) debugMessage("CP[" + ci + "] = class " + s);
397 break;
398 }
399
400 // case JVM_CONSTANT_ClassIndex:
401 case JVM_CONSTANT_UnresolvedClass: {
402 dos.writeByte(JVM_CONSTANT_Class);
403 String klassName = getSymbolAt(ci).asString();
404 Short s = (Short) utf8ToIndex.get(klassName);
405 dos.writeShort(s.shortValue());
406 if (DEBUG) debugMessage("CP[" + ci + "] = class " + s);
407 break;
408 }
409
410 case JVM_CONSTANT_String: {
411 dos.writeByte(cpConstType);
412 String str = OopUtilities.stringOopToString(getObjAt(ci));
413 Short s = (Short) utf8ToIndex.get(str);
414 dos.writeShort(s.shortValue());
415 if (DEBUG) debugMessage("CP[" + ci + "] = string " + s);
416 break;
417 }
418
419 // case JVM_CONSTANT_StringIndex:
420 case JVM_CONSTANT_UnresolvedString: {
421 dos.writeByte(JVM_CONSTANT_String);
422 String val = getSymbolAt(ci).asString();
423
424 Short s = (Short) utf8ToIndex.get(val);
425 dos.writeShort(s.shortValue());
426 if (DEBUG) debugMessage("CP[" + ci + "] = string " + s);
427 break;
428 }
429
430 // all external, internal method/field references
431 case JVM_CONSTANT_Fieldref:
432 case JVM_CONSTANT_Methodref:
433 case JVM_CONSTANT_InterfaceMethodref: {
434 dos.writeByte(cpConstType);
435 int value = getIntAt(ci);
436 short klassIndex = (short) extractLowShortFromInt(value);
437 short nameAndTypeIndex = (short) extractHighShortFromInt(value);
438 dos.writeShort(klassIndex);
439 dos.writeShort(nameAndTypeIndex);
440 if (DEBUG) debugMessage("CP[" + ci + "] = ref klass = " +
441 klassIndex + ", N&T = " + nameAndTypeIndex);
442 break;
443 }
444
445 case JVM_CONSTANT_NameAndType: {
446 dos.writeByte(cpConstType);
447 int value = getIntAt(ci);
448 short nameIndex = (short) extractLowShortFromInt(value);
449 short signatureIndex = (short) extractHighShortFromInt(value);
450 dos.writeShort(nameIndex);
451 dos.writeShort(signatureIndex);
452 if (DEBUG) debugMessage("CP[" + ci + "] = N&T name = " + nameIndex
453 + ", type = " + signatureIndex);
454 break;
455 }
456 } // switch
457 }
458 dos.flush();
459 return;
460 }
461
462 public void printValueOn(PrintStream tty) {
463 tty.print("ConstantPool for " + getPoolHolder().getName().asString());
464 }
465
466 public long getObjectSize() {
467 return alignObjectSize(headerSize + (getLength() * elementSize));
468 }
469
470 //----------------------------------------------------------------------
471 // Internals only below this point
472 //
473
474 private static int extractHighShortFromInt(int val) {
475 return (val >> 16) & 0xFFFF;
476 }
477
478 private static int extractLowShortFromInt(int val) {
479 return val & 0xFFFF;
480 }
481 }