| 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 | |
| 15 | package net.sf.madmap; |
| 16 | |
| 17 | import java.io.*; |
| 18 | import java.util.*; |
| 19 | |
| 20 | /** |
| 21 | * HprofBinClassElement is the representation of a java class in a binary heap dump. |
| 22 | * |
| 23 | * @author ecaspole |
| 24 | * |
| 25 | */ |
| 26 | public class HprofBinClassElement extends HprofClassElement { |
| 27 | ArrayList<Byte> _nonstatic_type_info = null; |
| 28 | |
| 29 | static int _refSize; |
| 30 | public static void setRefSize(int sz) { _refSize = sz; } |
| 31 | public static int getRefSize() { return _refSize; } |
| 32 | |
| 33 | void setLoader(long loader) { _loader = loader; } |
| 34 | void setSuper(long sooper) { _super = sooper; } |
| 35 | long getSuper() { return _super; } |
| 36 | |
| 37 | void setTrace(long trace) { /* ignored for now */; } |
| 38 | |
| 39 | void setNonStaticTypeInfo(ArrayList<Byte> info) { |
| 40 | assert info != null: "should not be null"; |
| 41 | _nonstatic_type_info = info; |
| 42 | } |
| 43 | ArrayList<Byte> getNonStaticTypeInfo() { return _nonstatic_type_info; } |
| 44 | |
| 45 | public void updateLoadClass(int trc, long instanceSize, long sooperclass, long loader, ArrayList n, ArrayList r) { |
| 46 | _super = sooperclass; |
| 47 | _loader = loader; |
| 48 | _instance_size = instanceSize; |
| 49 | if (( n != null ) && ( r != null )) { |
| 50 | buildActualChildrenRefs( n, r ); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | public HprofBinClassElement( long addr, String tn ) { |
| 55 | super(addr, tn); |
| 56 | _nonstatic_type_info = new ArrayList<Byte>(); |
| 57 | _nonstatic_names = new ArrayList(); |
| 58 | } |
| 59 | |
| 60 | public int getSizeForTypeInfo() { |
| 61 | int totalSize = 0; |
| 62 | |
| 63 | if (_nonstatic_type_info != null) { |
| 64 | ListIterator<Byte> ityp = _nonstatic_type_info.listIterator(); |
| 65 | while(ityp.hasNext()) { |
| 66 | |
| 67 | Byte typeCode = ityp.next(); |
| 68 | if (typeCode == 2) { |
| 69 | totalSize += getRefSize(); |
| 70 | } else { |
| 71 | totalSize += BasicTypeInfo.DUMMY.get(typeCode).getSize(); |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | return totalSize; |
| 76 | } |
| 77 | |
| 78 | boolean _typeComplete = false; |
| 79 | |
| 80 | public ArrayList<Byte> buildSuperTypeInfo(java.util.AbstractMap<HprofClassElement,HprofClassElement> h) { |
| 81 | if (getSuper() != 0 && (!_typeComplete)) { |
| 82 | int currTypeInfoSize = getSizeForTypeInfo(); |
| 83 | if (currTypeInfoSize < instanceSize()) { |
| 84 | HprofBinClassElement sooper = (HprofBinClassElement)h.get(new HprofHeapElement(getSuper())); |
| 85 | _nonstatic_type_info.addAll(sooper.buildSuperTypeInfo(h)); |
| 86 | _nonstatic_names.addAll(sooper.nonstatic_field_names()); |
| 87 | } |
| 88 | _typeComplete = true; |
| 89 | } |
| 90 | |
| 91 | //if (MadmapMain.runDump()) { |
| 92 | // System. out. println ( "buildSuperTypeInfo: 0x" + Long.toHexString( this.addr() ) + " " + this.className() + |
| 93 | // " super: 0x" + Long.toHexString(getSuper()) + " " + _nonstatic_type_info); |
| 94 | //} |
| 95 | return _nonstatic_type_info; |
| 96 | } |
| 97 | |
| 98 | public String toString() { |
| 99 | StringBuffer p = new StringBuffer(); |
| 100 | |
| 101 | p.append( "CLS " + Long.toHexString(_objectaddr) + " (name=" + _type_name + /* ", trace=" + getStackTrace() + */ ")\n" ); |
| 102 | p.append( " " + "super" + "\t" + Long.toHexString( _super ) + "\n"); |
| 103 | p.append( " " + "loader" + "\t" + Long.toHexString( _loader ) + "\n"); |
| 104 | if ( _static_names != null ) { |
| 105 | for (int i = 0; i < _static_names.length; i++ ) { |
| 106 | String n = (String) _static_names[ i ]; |
| 107 | Long r = (Long) _static_refs[ i ]; |
| 108 | p.append( " static " + n + "\t" + Long.toHexString( r ) + "\n"); |
| 109 | } |
| 110 | } |
| 111 | if ( _nonstatic_names != null ) { |
| 112 | p.append(" # _nonstatic_names.length : " + _nonstatic_names.size() + "\n"); |
| 113 | p.append(" # _nonstatic_names = " + _nonstatic_names + "\n"); |
| 114 | if (_nonstatic_type_info != null) { |
| 115 | p.append(" # _nonstatic_type_info sz = " + _nonstatic_type_info.size() + "\n"); |
| 116 | } |
| 117 | } |
| 118 | p.append( " # _instance_size = " + _instance_size + "\n"); |
| 119 | p.append( " # instance count = " + getInstanceCount() + "\n"); |
| 120 | p.append( " # instance total sz = " + getInstanceSize() + "\n"); |
| 121 | |
| 122 | return p.toString(); |
| 123 | } |
| 124 | } |