1 /* |
|
2 * Copyright (c) 1997, 2008, Oracle and/or its affiliates. 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. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 |
|
26 |
|
27 /* |
|
28 * The Original Code is HAT. The Initial Developer of the |
|
29 * Original Code is Bill Foote, with contributions from others |
|
30 * at JavaSoft/Sun. |
|
31 */ |
|
32 |
|
33 package com.sun.tools.hat.internal.model; |
|
34 |
|
35 import java.io.IOException; |
|
36 import com.sun.tools.hat.internal.parser.ReadBuffer; |
|
37 |
|
38 /** |
|
39 * @author Bill Foote |
|
40 */ |
|
41 public class JavaObjectArray extends JavaLazyReadObject { |
|
42 |
|
43 private Object clazz; // Long before resolve, the class after resolve |
|
44 |
|
45 public JavaObjectArray(long classID, long offset) { |
|
46 super(offset); |
|
47 this.clazz = makeId(classID); |
|
48 } |
|
49 |
|
50 public JavaClass getClazz() { |
|
51 return (JavaClass) clazz; |
|
52 } |
|
53 |
|
54 public void resolve(Snapshot snapshot) { |
|
55 if (clazz instanceof JavaClass) { |
|
56 return; |
|
57 } |
|
58 long classID = getIdValue((Number)clazz); |
|
59 if (snapshot.isNewStyleArrayClass()) { |
|
60 // Modern heap dumps do this |
|
61 JavaThing t = snapshot.findThing(classID); |
|
62 if (t instanceof JavaClass) { |
|
63 clazz = (JavaClass) t; |
|
64 } |
|
65 } |
|
66 if (!(clazz instanceof JavaClass)) { |
|
67 JavaThing t = snapshot.findThing(classID); |
|
68 if (t != null && t instanceof JavaClass) { |
|
69 JavaClass el = (JavaClass) t; |
|
70 String nm = el.getName(); |
|
71 if (!nm.startsWith("[")) { |
|
72 nm = "L" + el.getName() + ";"; |
|
73 } |
|
74 clazz = snapshot.getArrayClass(nm); |
|
75 } |
|
76 } |
|
77 |
|
78 if (!(clazz instanceof JavaClass)) { |
|
79 clazz = snapshot.getOtherArrayType(); |
|
80 } |
|
81 ((JavaClass)clazz).addInstance(this); |
|
82 super.resolve(snapshot); |
|
83 } |
|
84 |
|
85 public JavaThing[] getValues() { |
|
86 return getElements(); |
|
87 } |
|
88 |
|
89 public JavaThing[] getElements() { |
|
90 Snapshot snapshot = getClazz().getSnapshot(); |
|
91 byte[] data = getValue(); |
|
92 final int idSize = snapshot.getIdentifierSize(); |
|
93 final int numElements = data.length / idSize; |
|
94 JavaThing[] elements = new JavaThing[numElements]; |
|
95 int index = 0; |
|
96 for (int i = 0; i < elements.length; i++) { |
|
97 long id = objectIdAt(index, data); |
|
98 index += idSize; |
|
99 elements[i] = snapshot.findThing(id); |
|
100 } |
|
101 return elements; |
|
102 } |
|
103 |
|
104 public int compareTo(JavaThing other) { |
|
105 if (other instanceof JavaObjectArray) { |
|
106 return 0; |
|
107 } |
|
108 return super.compareTo(other); |
|
109 } |
|
110 |
|
111 public int getLength() { |
|
112 return getValueLength() / getClazz().getIdentifierSize(); |
|
113 } |
|
114 |
|
115 public void visitReferencedObjects(JavaHeapObjectVisitor v) { |
|
116 super.visitReferencedObjects(v); |
|
117 JavaThing[] elements = getElements(); |
|
118 for (int i = 0; i < elements.length; i++) { |
|
119 if (elements[i] != null && elements[i] instanceof JavaHeapObject) { |
|
120 v.visit((JavaHeapObject) elements[i]); |
|
121 } |
|
122 } |
|
123 } |
|
124 |
|
125 /** |
|
126 * Describe the reference that this thing has to target. This will only |
|
127 * be called if target is in the array returned by getChildrenForRootset. |
|
128 */ |
|
129 public String describeReferenceTo(JavaThing target, Snapshot ss) { |
|
130 JavaThing[] elements = getElements(); |
|
131 for (int i = 0; i < elements.length; i++) { |
|
132 if (elements[i] == target) { |
|
133 return "Element " + i + " of " + this; |
|
134 } |
|
135 } |
|
136 return super.describeReferenceTo(target, ss); |
|
137 } |
|
138 |
|
139 /* |
|
140 * Java object array record (HPROF_GC_OBJ_ARRAY_DUMP) |
|
141 * looks as below: |
|
142 * |
|
143 * object ID |
|
144 * stack trace serial number (int) |
|
145 * array length (int) |
|
146 * array class ID |
|
147 * array element IDs |
|
148 */ |
|
149 protected final int readValueLength() throws IOException { |
|
150 JavaClass cl = getClazz(); |
|
151 ReadBuffer buf = cl.getReadBuffer(); |
|
152 int idSize = cl.getIdentifierSize(); |
|
153 long offset = getOffset() + idSize + 4; |
|
154 int len = buf.getInt(offset); |
|
155 return len * cl.getIdentifierSize(); |
|
156 } |
|
157 |
|
158 protected final byte[] readValue() throws IOException { |
|
159 JavaClass cl = getClazz(); |
|
160 ReadBuffer buf = cl.getReadBuffer(); |
|
161 int idSize = cl.getIdentifierSize(); |
|
162 long offset = getOffset() + idSize + 4; |
|
163 int len = buf.getInt(offset); |
|
164 if (len == 0) { |
|
165 return Snapshot.EMPTY_BYTE_ARRAY; |
|
166 } else { |
|
167 byte[] res = new byte[len * idSize]; |
|
168 buf.get(offset + 4 + idSize, res); |
|
169 return res; |
|
170 } |
|
171 } |
|
172 } |
|