1 /* 2 * Copyright (c) 1997, 2017, 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 jdk.test.lib.hprof.model; 34 35 import java.io.IOException; 36 import jdk.test.lib.hprof.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 return getValue(); 91 } 92 93 public int compareTo(JavaThing other) { 94 if (other instanceof JavaObjectArray) { 95 return 0; 96 } 97 return super.compareTo(other); 98 } 99 100 public int getLength() { 101 return (int)(getValueLength() / idSize()); 102 } 103 104 public void visitReferencedObjects(JavaHeapObjectVisitor v) { 105 super.visitReferencedObjects(v); 106 JavaThing[] elements = getElements(); 107 for (int i = 0; i < elements.length; i++) { 108 if (elements[i] != null && elements[i] instanceof JavaHeapObject) { 109 v.visit((JavaHeapObject) elements[i]); 110 } 111 } 112 } 113 114 /** 115 * Describe the reference that this thing has to target. This will only 116 * be called if target is in the array returned by getChildrenForRootset. 117 */ 118 public String describeReferenceTo(JavaThing target, Snapshot ss) { 119 JavaThing[] elements = getElements(); 120 for (int i = 0; i < elements.length; i++) { 121 if (elements[i] == target) { 122 return "Element " + i + " of " + this; 123 } 124 } 125 return super.describeReferenceTo(target, ss); 126 } 127 128 /* 129 * Java object array record (HPROF_GC_OBJ_ARRAY_DUMP) 130 * looks as below: 131 * 132 * object ID 133 * stack trace serial number (int) 134 * array length (int) 135 * array class ID 136 * array element IDs 137 */ 138 @Override 139 protected final long readValueLength() throws IOException { 140 long offset = getOffset() + idSize() + 4; 141 // length of the array in elements 142 long len = buf().getInt(offset); 143 // byte length of array 144 return len * idSize(); 145 } 146 147 private long dataStartOffset() { 148 return getOffset() + idSize() + 4 + 4 + idSize(); 149 } 150 151 @Override 152 protected final JavaThing[] readValue() throws IOException { 153 Snapshot snapshot = getClazz().getSnapshot(); 154 int len = getLength(); 155 long offset = dataStartOffset(); 156 157 JavaThing[] res = new JavaThing[len]; 158 for (int i = 0; i < len; i++) { 159 long id = objectIdAt(offset); 160 res[i] = snapshot.findThing(id); 161 offset += idSize(); 162 } 163 return res; 164 } 165 }