1 9 package com.vladium.jcd.cls.attribute; 10 11 import java.io.IOException ; 12 13 import com.vladium.jcd.cls.ElementFactory; 14 import com.vladium.jcd.cls.IAttributeCollection; 15 import com.vladium.jcd.cls.IConstantCollection; 16 import com.vladium.jcd.lib.UDataInputStream; 17 import com.vladium.jcd.lib.UDataOutputStream; 18 19 74 public 75 final class CodeAttribute_info extends Attribute_info 76 { 77 79 80 public static final byte [] EMPTY_BYTE_ARRAY = new byte [0]; 81 82 public int m_max_stack, m_max_locals; 83 84 85 86 public CodeAttribute_info (final int attribute_name_index, 87 final int max_stack, int max_locals, 88 final byte [] code, 89 final IExceptionHandlerTable exceptionHandlerTable, 90 final IAttributeCollection attributes) 91 { 92 super (attribute_name_index, 8 + (code != null ? code.length : 0) + exceptionHandlerTable.length () + attributes.length ()); 93 94 m_max_stack = max_stack; 95 m_max_locals = max_locals; 96 97 m_code = (code != null ? code : EMPTY_BYTE_ARRAY); 98 m_codeSize = m_code.length; 99 100 m_exceptionHandlerTable = exceptionHandlerTable; 101 m_attributes = attributes; 102 } 103 104 108 public final byte [] getCode () 109 { 110 return m_code; 111 } 112 113 public final int getCodeSize () 114 { 115 return m_codeSize; 116 } 117 118 public IAttributeCollection getAttributes () 119 { 120 return m_attributes; 121 } 122 123 public IExceptionHandlerTable getExceptionTable () 124 { 125 return m_exceptionHandlerTable; 126 } 127 128 public long length () 129 { 130 return 14 + m_codeSize + m_exceptionHandlerTable.length () + m_attributes.length (); 131 } 132 133 135 public void accept (final IAttributeVisitor visitor, final Object ctx) 136 { 137 visitor.visit (this, ctx); 138 } 139 140 141 public String toString () 142 { 143 String eol = System.getProperty ("line.separator"); 144 145 StringBuffer s = new StringBuffer (); 146 147 s.append ("CodeAttribute_info: [attribute_name_index = " + m_name_index + ", attribute_length = " + m_attribute_length + "]" + eol); 148 s.append (" max_stack/max_locals = " + m_max_stack + '/' + m_max_locals + eol); 149 s.append (" code [length " + m_codeSize + "]" + eol); 150 151 for (int a = 0; a < m_attributes.size (); ++ a) 152 { 153 s.append (" " + m_attributes.get (a) + eol); 154 } 155 156 157 return s.toString (); 158 } 159 160 162 165 public Object clone () 166 { 167 final CodeAttribute_info _clone = (CodeAttribute_info) super.clone (); 168 169 171 _clone.m_code = (m_codeSize == 0 ? EMPTY_BYTE_ARRAY : (byte []) m_code.clone ()); _clone.m_exceptionHandlerTable = (IExceptionHandlerTable) m_exceptionHandlerTable.clone (); 173 _clone.m_attributes = (IAttributeCollection) m_attributes.clone (); 174 175 return _clone; 176 } 177 178 179 181 public void writeInClassFormat (final UDataOutputStream out) throws IOException 182 { 183 super.writeInClassFormat (out); 184 185 out.writeU2 (m_max_stack); 186 out.writeU2 (m_max_locals); 187 188 out.writeU4 (m_codeSize); 189 out.write (m_code, 0, m_codeSize); 191 m_exceptionHandlerTable.writeInClassFormat (out); 192 m_attributes.writeInClassFormat (out); 193 } 194 195 196 public void setCode (final byte [] code, final int codeSize) 197 { 198 m_code = code; 199 m_codeSize = codeSize; 200 } 201 202 204 206 207 CodeAttribute_info (final IConstantCollection constants, 208 final int attribute_name_index, final long attribute_length, 209 final UDataInputStream bytes) 210 throws IOException 211 { 212 super (attribute_name_index, attribute_length); 213 214 m_max_stack = bytes.readU2 (); 215 m_max_locals = bytes.readU2 (); 216 217 final long code_length = bytes.readU4 (); 218 219 m_code = new byte [(int) code_length]; 220 bytes.readFully (m_code); 221 m_codeSize = (int) code_length; 222 223 224 final int exception_table_length = bytes.readU2 (); 225 m_exceptionHandlerTable = AttributeElementFactory.newExceptionHandlerTable (exception_table_length); 226 227 for (int i = 0; i < exception_table_length; ++ i) 228 { 229 Exception_info exception_info = new Exception_info (bytes); 230 if (DEBUG) System.out.println ("\t[" + i + "] exception: " + exception_info); 231 232 m_exceptionHandlerTable.add (exception_info); 233 } 234 235 236 final int attributes_count = bytes.readU2 (); 238 m_attributes = ElementFactory.newAttributeCollection (attributes_count); 239 240 for (int i = 0; i < attributes_count; ++ i) 241 { 242 Attribute_info attribute_info = Attribute_info.new_Attribute_info (constants, bytes); 243 if (DEBUG) System.out.println ("\t[" + i + "] attribute: " + attribute_info); 244 245 m_attributes.add (attribute_info); 246 } 247 } 248 249 251 252 private byte [] m_code; private int m_codeSize; 254 255 private IExceptionHandlerTable m_exceptionHandlerTable; private IAttributeCollection m_attributes; 258 259 private static final boolean DEBUG = false; 260 261 } | Popular Tags |