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

Initial load
author duke
date Sat, 01 Dec 2007 00:00:00 +0000
parents
children c18cbe5936b8
comparison
equal deleted inserted replaced
-1:000000000000 0:a61af66fc99e
1 /*
2 * Copyright 2000-2004 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 Symbol is a canonicalized string.
35 // All Symbols reside in global symbolTable.
36
37 public class Symbol extends Oop {
38 static {
39 VM.registerVMInitializedObserver(new Observer() {
40 public void update(Observable o, Object data) {
41 initialize(VM.getVM().getTypeDataBase());
42 }
43 });
44 }
45
46 private static synchronized void initialize(TypeDataBase db) throws WrongTypeException {
47 Type type = db.lookupType("symbolOopDesc");
48 length = new CIntField(type.getCIntegerField("_length"), 0);
49 baseOffset = type.getField("_body").getOffset();
50 }
51
52 // Format:
53 // [header]
54 // [klass ]
55 // [length] byte size of uft8 string
56 // ..body..
57
58 Symbol(OopHandle handle, ObjectHeap heap) {
59 super(handle, heap);
60 }
61
62 public boolean isSymbol() { return true; }
63
64 private static long baseOffset; // tells where the array part starts
65
66 // Fields
67 private static CIntField length;
68
69 // Accessors for declared fields
70 public long getLength() { return length.getValue(this); }
71
72 public byte getByteAt(long index) {
73 return getHandle().getJByteAt(baseOffset + index);
74 }
75
76 public boolean equals(byte[] modUTF8Chars) {
77 int l = (int) getLength();
78 if (l != modUTF8Chars.length) return false;
79 while (l-- > 0) {
80 if (modUTF8Chars[l] != getByteAt(l)) return false;
81 }
82 if (Assert.ASSERTS_ENABLED) {
83 Assert.that(l == -1, "we should be at the beginning");
84 }
85 return true;
86 }
87
88 public byte[] asByteArray() {
89 int length = (int) getLength();
90 byte [] result = new byte [length];
91 for (int index = 0; index < length; index++) {
92 result[index] = getByteAt(index);
93 }
94 return result;
95 }
96
97 public String asString() {
98 // Decode the byte array and return the string.
99 try {
100 return readModifiedUTF8(asByteArray());
101 } catch(IOException e) {
102 return null;
103 }
104 }
105
106 public boolean startsWith(String str) {
107 return asString().startsWith(str);
108 }
109
110 public void printValueOn(PrintStream tty) {
111 tty.print("#" + asString());
112 }
113
114 public long getObjectSize() {
115 return alignObjectSize(baseOffset + getLength());
116 }
117
118 void iterateFields(OopVisitor visitor, boolean doVMFields) {
119 super.iterateFields(visitor, doVMFields);
120 if (doVMFields) {
121 visitor.doCInt(length, true);
122 int length = (int) getLength();
123 for (int index = 0; index < length; index++) {
124 visitor.doByte(new ByteField(new IndexableFieldIdentifier(index), baseOffset + index, false), true);
125 }
126 }
127 }
128
129 /** Note: this comparison is used for vtable sorting only; it
130 doesn't matter what order it defines, as long as it is a total,
131 time-invariant order Since symbolOops are in permSpace, their
132 relative order in memory never changes, so use address
133 comparison for speed. */
134 public int fastCompare(Symbol other) {
135 return (int) getHandle().minus(other.getHandle());
136 }
137
138 private static String readModifiedUTF8(byte[] buf) throws IOException {
139 final int len = buf.length;
140 byte[] tmp = new byte[len + 2];
141 // write modified UTF-8 length as short in big endian
142 tmp[0] = (byte) ((len >>> 8) & 0xFF);
143 tmp[1] = (byte) ((len >>> 0) & 0xFF);
144 // copy the data
145 System.arraycopy(buf, 0, tmp, 2, len);
146 DataInputStream dis = new DataInputStream(new ByteArrayInputStream(tmp));
147 return dis.readUTF();
148 }
149 }