001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: * The Original Software is NetBeans. The Initial Developer of the Original
026: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
027: * Microsystems, Inc. All Rights Reserved.
028: *
029: * If you wish your version of this file to be governed by only the CDDL
030: * or only the GPL Version 2, indicate your decision by adding
031: * "[Contributor] elects to include this software in this distribution
032: * under the [CDDL or GPL Version 2] license." If you do not indicate a
033: * single choice of license, a recipient has the option to distribute
034: * your version of this file under either the CDDL, the GPL Version 2 or
035: * to extend the choice of license to its licensees as provided above.
036: * However, if you add GPL Version 2 code and therefore, elected the GPL
037: * Version 2 license, then the option applies only if the new code is
038: * made subject to such option by the copyright holder.
039: */
040:
041: package org.netbeans.lib.profiler.heap;
042:
043: import java.util.ArrayList;
044: import java.util.Collections;
045: import java.util.Iterator;
046: import java.util.List;
047:
048: /**
049: *
050: * @author Tomas Hurka
051: */
052: class InstanceDump extends HprofObject implements Instance {
053: //~ Instance fields ----------------------------------------------------------------------------------------------------------
054:
055: final ClassDump dumpClass;
056:
057: //~ Constructors -------------------------------------------------------------------------------------------------------------
058:
059: InstanceDump(ClassDump cls, long offset) {
060: super (offset);
061: dumpClass = cls;
062: }
063:
064: //~ Methods ------------------------------------------------------------------------------------------------------------------
065:
066: public List getFieldValues() {
067: long offset = fileOffset + getInstanceFieldValuesOffset();
068: List fields = dumpClass.getAllInstanceFields();
069: List values = new ArrayList(fields.size());
070: Iterator fit = fields.iterator();
071:
072: while (fit.hasNext()) {
073: HprofField field = (HprofField) fit.next();
074:
075: if (field.getVauleType() == HprofHeap.OBJECT) {
076: values.add(new HprofInstanceObjectValue(this , field,
077: offset));
078: } else {
079: values.add(new HprofInstanceValue(this , field, offset));
080: }
081:
082: offset += field.getValueSize();
083: }
084:
085: return values;
086: }
087:
088: public boolean isGCRoot() {
089: return dumpClass.getHprof().getGCRoot(this ) != null;
090: }
091:
092: public long getInstanceId() {
093: return dumpClass.getHprofBuffer().getID(fileOffset + 1);
094: }
095:
096: public int getInstanceNumber() {
097: return dumpClass.getHprof().idToOffsetMap.get(getInstanceId())
098: .getIndex();
099: }
100:
101: public JavaClass getJavaClass() {
102: return dumpClass;
103: }
104:
105: public Instance getNearestGCRootPointer() {
106: return dumpClass.getHprof().nearestGCRoot
107: .getNearestGCRootPointer(this );
108: }
109:
110: public int getReachableSize() {
111: return 0;
112: }
113:
114: public List getReferences() {
115: HprofByteBuffer buffer = dumpClass.getHprofBuffer();
116: byte[] idArr = new byte[buffer.getIDSize()];
117:
118: buffer.get(fileOffset + 1, idArr);
119:
120: return dumpClass.getHprof().findReferencesFor(getInstanceId(),
121: idArr);
122: }
123:
124: public int getRetainedSize() {
125: return 0;
126: }
127:
128: public int getSize() {
129: return dumpClass.getInstanceSize();
130: }
131:
132: public List /*<FieldValue>*/getStaticFieldValues() {
133: return dumpClass.getStaticFieldValues();
134: }
135:
136: public Object getValueOfField(String name) {
137: Iterator fIt = getFieldValues().iterator();
138: FieldValue matchingFieldValue = null;
139:
140: while (fIt.hasNext()) {
141: FieldValue fieldValue = (FieldValue) fIt.next();
142:
143: if (fieldValue.getField().getName().equals(name)) {
144: matchingFieldValue = fieldValue;
145: }
146: }
147:
148: if (matchingFieldValue == null) {
149: return null;
150: }
151:
152: if (matchingFieldValue instanceof HprofInstanceObjectValue) {
153: return ((HprofInstanceObjectValue) matchingFieldValue)
154: .getInstance();
155: } else {
156: return ((HprofInstanceValue) matchingFieldValue)
157: .getTypeValue();
158: }
159: }
160:
161: private int getInstanceFieldValuesOffset() {
162: int idSize = dumpClass.getHprofBuffer().getIDSize();
163:
164: return 1 + idSize + 4 + idSize + 4;
165: }
166: }
|