| 1 | /* |
| 2 | * Copyright 2008 Eric Caspole |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this |
| 5 | * file except in compliance with the License. You may obtain a copy of the License at |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software distributed under |
| 10 | * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 11 | * KIND, either express or implied. See the License for the specific language governing |
| 12 | * permissions and limitations under the License. |
| 13 | */ |
| 14 | package net.sf.madmap; |
| 15 | |
| 16 | import java.io.*; |
| 17 | import java.util.*; |
| 18 | |
| 19 | /** |
| 20 | * HprofObject is the representation of a java object in the heap of the hprof |
| 21 | * target process. It has data members representing the object fields of the |
| 22 | * target object and retrieves the member field names from the HprofClassElement |
| 23 | * that corresponds to the java class for the target object. |
| 24 | * |
| 25 | * @author ecaspole |
| 26 | * |
| 27 | */ |
| 28 | public final class HprofObject extends HprofHeapAllocation { |
| 29 | |
| 30 | short[] _member_name_indexes = null; |
| 31 | |
| 32 | public HprofObject( long addr, String klass, HprofClassElement cls_addr, long size, int trace, ArrayList names, ArrayList objs ) { |
| 33 | super( addr, trace, cls_addr ); |
| 34 | |
| 35 | assert (( objs == null ) ? true : (( MadmapMain.getSaveMemberNames() ) ? names.size() == objs.size() : true)): "object members are messed up." ; |
| 36 | set_children( longArrayListtoLongArray( objs ) ); |
| 37 | } |
| 38 | |
| 39 | public long size() { |
| 40 | long objSize = class_addr().instanceSize(); |
| 41 | |
| 42 | // In binary dumps, Object has 0 size |
| 43 | //if ( objSize == 0 ) { |
| 44 | // System.out.println( "### instanceSize is 0: " + Long.toHexString(addr()) + " " + className()); |
| 45 | // assert objSize != 0 : "instanceSize is 0"; |
| 46 | //} |
| 47 | return objSize; |
| 48 | } |
| 49 | |
| 50 | public void set_member_name_indexes( short[] nmIdx) { _member_name_indexes = nmIdx; } |
| 51 | |
| 52 | public String toString() { |
| 53 | StringBuffer p = new StringBuffer(); |
| 54 | |
| 55 | p.append( new String("OBJ " + Long.toHexString(addr()) + " (sz= " + size() + ", trace=" + getStackTrace() + |
| 56 | ", class=" + className() + "@" + Long.toHexString(class_addr().addr()) + ")")); |
| 57 | |
| 58 | try { |
| 59 | if (children() != null) { |
| 60 | if ( MadmapMain.getSaveMemberNames() ) { |
| 61 | if ( (!(( children().length > 0 ) && ( _member_name_indexes != null ))) && |
| 62 | (!(( children().length == 0 ) && ( _member_name_indexes == null ))) ) { |
| 63 | System.out.println("members are messed up:" ); |
| 64 | System.out.println( "k.name() = " + class_addr().className() + " -- this classname() = " + className() ); |
| 65 | System.out.println(" _member_refs.length = " + children().length ); |
| 66 | System.out.println(" _member_name_indexes.length = " + _member_name_indexes.length ); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | for (int i = 0 ; i < children().length; i++ ) { |
| 71 | p.append("\n"); |
| 72 | Long id = (Long) children()[ i ]; |
| 73 | String name = ""; |
| 74 | if ( MadmapMain.getSaveMemberNames() ) { |
| 75 | name = (String) class_addr().nonstatic_field_names().get( _member_name_indexes[i] ); |
| 76 | } |
| 77 | p.append( new String(" " + name + "\t" + Long.toHexString( id ))); |
| 78 | } |
| 79 | } |
| 80 | } catch ( Exception e ) { |
| 81 | System.out.println( "Exception " + e ); |
| 82 | //System.out.println( " _member_refs.length : " + children().length ); |
| 83 | //System.out.println( " _member_name_indexes.length : " + _member_name_indexes.length ); |
| 84 | //System.out.println( " k : " + class_addr().toString() ); |
| 85 | e.printStackTrace(); |
| 86 | System.exit( -1 ); |
| 87 | } |
| 88 | |
| 89 | return p.toString(); |
| 90 | } |
| 91 | } |