|
1 /* |
|
2 * Copyright 2005-2006 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. Sun designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Sun in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 * CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 * have any questions. |
|
24 */ |
|
25 |
|
26 |
|
27 /* |
|
28 * The contents of this file are subject to the Sun Public License |
|
29 * Version 1.0 (the "License"); you may not use this file except in |
|
30 * compliance with the License. A copy of the License is available at |
|
31 * http://www.sun.com/, and in the file LICENSE.html in the |
|
32 * doc directory. |
|
33 * |
|
34 * The Original Code is HAT. The Initial Developer of the |
|
35 * Original Code is Bill Foote, with contributions from others |
|
36 * at JavaSoft/Sun. Portions created by Bill Foote and others |
|
37 * at Javasoft/Sun are Copyright (C) 1997-2004. All Rights Reserved. |
|
38 * |
|
39 * In addition to the formal license, I ask that you don't |
|
40 * change the history or donations files without permission. |
|
41 * |
|
42 */ |
|
43 |
|
44 package com.sun.tools.hat.internal.model; |
|
45 |
|
46 import java.io.IOException; |
|
47 import com.sun.tools.hat.internal.parser.ReadBuffer; |
|
48 |
|
49 /** |
|
50 * @author Bill Foote |
|
51 */ |
|
52 public class JavaObjectArray extends JavaLazyReadObject { |
|
53 |
|
54 private Object clazz; // Long before resolve, the class after resolve |
|
55 |
|
56 public JavaObjectArray(long classID, long offset) { |
|
57 super(offset); |
|
58 this.clazz = makeId(classID); |
|
59 } |
|
60 |
|
61 public JavaClass getClazz() { |
|
62 return (JavaClass) clazz; |
|
63 } |
|
64 |
|
65 public void resolve(Snapshot snapshot) { |
|
66 if (clazz instanceof JavaClass) { |
|
67 return; |
|
68 } |
|
69 long classID = getIdValue((Number)clazz); |
|
70 if (snapshot.isNewStyleArrayClass()) { |
|
71 // Modern heap dumps do this |
|
72 JavaThing t = snapshot.findThing(classID); |
|
73 if (t instanceof JavaClass) { |
|
74 clazz = (JavaClass) t; |
|
75 } |
|
76 } |
|
77 if (!(clazz instanceof JavaClass)) { |
|
78 JavaThing t = snapshot.findThing(classID); |
|
79 if (t != null && t instanceof JavaClass) { |
|
80 JavaClass el = (JavaClass) t; |
|
81 String nm = el.getName(); |
|
82 if (!nm.startsWith("[")) { |
|
83 nm = "L" + el.getName() + ";"; |
|
84 } |
|
85 clazz = snapshot.getArrayClass(nm); |
|
86 } |
|
87 } |
|
88 |
|
89 if (!(clazz instanceof JavaClass)) { |
|
90 clazz = snapshot.getOtherArrayType(); |
|
91 } |
|
92 ((JavaClass)clazz).addInstance(this); |
|
93 super.resolve(snapshot); |
|
94 } |
|
95 |
|
96 public JavaThing[] getValues() { |
|
97 return getElements(); |
|
98 } |
|
99 |
|
100 public JavaThing[] getElements() { |
|
101 Snapshot snapshot = getClazz().getSnapshot(); |
|
102 byte[] data = getValue(); |
|
103 final int idSize = snapshot.getIdentifierSize(); |
|
104 final int numElements = data.length / idSize; |
|
105 JavaThing[] elements = new JavaThing[numElements]; |
|
106 int index = 0; |
|
107 for (int i = 0; i < elements.length; i++) { |
|
108 long id = objectIdAt(index, data); |
|
109 index += idSize; |
|
110 elements[i] = snapshot.findThing(id); |
|
111 } |
|
112 return elements; |
|
113 } |
|
114 |
|
115 public int compareTo(JavaThing other) { |
|
116 if (other instanceof JavaObjectArray) { |
|
117 return 0; |
|
118 } |
|
119 return super.compareTo(other); |
|
120 } |
|
121 |
|
122 public int getLength() { |
|
123 return getValueLength() / getClazz().getIdentifierSize(); |
|
124 } |
|
125 |
|
126 public void visitReferencedObjects(JavaHeapObjectVisitor v) { |
|
127 super.visitReferencedObjects(v); |
|
128 JavaThing[] elements = getElements(); |
|
129 for (int i = 0; i < elements.length; i++) { |
|
130 if (elements[i] != null && elements[i] instanceof JavaHeapObject) { |
|
131 v.visit((JavaHeapObject) elements[i]); |
|
132 } |
|
133 } |
|
134 } |
|
135 |
|
136 /** |
|
137 * Describe the reference that this thing has to target. This will only |
|
138 * be called if target is in the array returned by getChildrenForRootset. |
|
139 */ |
|
140 public String describeReferenceTo(JavaThing target, Snapshot ss) { |
|
141 JavaThing[] elements = getElements(); |
|
142 for (int i = 0; i < elements.length; i++) { |
|
143 if (elements[i] == target) { |
|
144 return "Element " + i + " of " + this; |
|
145 } |
|
146 } |
|
147 return super.describeReferenceTo(target, ss); |
|
148 } |
|
149 |
|
150 /* |
|
151 * Java object array record (HPROF_GC_OBJ_ARRAY_DUMP) |
|
152 * looks as below: |
|
153 * |
|
154 * object ID |
|
155 * stack trace serial number (int) |
|
156 * array length (int) |
|
157 * array class ID |
|
158 * array element IDs |
|
159 */ |
|
160 protected final int readValueLength() throws IOException { |
|
161 JavaClass cl = getClazz(); |
|
162 ReadBuffer buf = cl.getReadBuffer(); |
|
163 int idSize = cl.getIdentifierSize(); |
|
164 long offset = getOffset() + idSize + 4; |
|
165 int len = buf.getInt(offset); |
|
166 return len * cl.getIdentifierSize(); |
|
167 } |
|
168 |
|
169 protected final byte[] readValue() throws IOException { |
|
170 JavaClass cl = getClazz(); |
|
171 ReadBuffer buf = cl.getReadBuffer(); |
|
172 int idSize = cl.getIdentifierSize(); |
|
173 long offset = getOffset() + idSize + 4; |
|
174 int len = buf.getInt(offset); |
|
175 if (len == 0) { |
|
176 return Snapshot.EMPTY_BYTE_ARRAY; |
|
177 } else { |
|
178 byte[] res = new byte[len * idSize]; |
|
179 buf.get(offset + 4 + idSize, res); |
|
180 return res; |
|
181 } |
|
182 } |
|
183 } |