1 package com.sun.org.apache.bcel.internal.classfile; 2 3 56 57 import com.sun.org.apache.bcel.internal.Constants; 58 import java.io.*; 59 60 79 public final class Code extends Attribute { 80 private int max_stack; private int max_locals; private int code_length; private byte[] code; 85 private int exception_table_length; 86 private CodeException[] exception_table; private int attributes_count; private Attribute[] attributes; 90 94 public Code(Code c) { 95 this(c.getNameIndex(), c.getLength(), c.getMaxStack(), c.getMaxLocals(), 96 c.getCode(), c.getExceptionTable(), c.getAttributes(), 97 c.getConstantPool()); 98 } 99 100 106 Code(int name_index, int length, DataInputStream file, 107 ConstantPool constant_pool) throws IOException 108 { 109 this(name_index, length, 111 file.readUnsignedShort(), file.readUnsignedShort(), 112 (byte[])null, (CodeException[])null, (Attribute[])null, 113 constant_pool); 114 115 code_length = file.readInt(); 116 code = new byte[code_length]; file.readFully(code); 118 119 122 exception_table_length = file.readUnsignedShort(); 123 exception_table = new CodeException[exception_table_length]; 124 125 for(int i=0; i < exception_table_length; i++) 126 exception_table[i] = new CodeException(file); 127 128 131 attributes_count = file.readUnsignedShort(); 132 attributes = new Attribute[attributes_count]; 133 for(int i=0; i < attributes_count; i++) 134 attributes[i] = Attribute.readAttribute(file, constant_pool); 135 136 140 this.length = length; 141 } 142 143 153 public Code(int name_index, int length, 154 int max_stack, int max_locals, 155 byte[] code, 156 CodeException[] exception_table, 157 Attribute[] attributes, 158 ConstantPool constant_pool) 159 { 160 super(Constants.ATTR_CODE, name_index, length, constant_pool); 161 162 this.max_stack = max_stack; 163 this.max_locals = max_locals; 164 165 setCode(code); 166 setExceptionTable(exception_table); 167 setAttributes(attributes); } 169 170 177 public void accept(Visitor v) { 178 v.visitCode(this); 179 } 180 181 187 public final void dump(DataOutputStream file) throws IOException 188 { 189 super.dump(file); 190 191 file.writeShort(max_stack); 192 file.writeShort(max_locals); 193 file.writeInt(code_length); 194 file.write(code, 0, code_length); 195 196 file.writeShort(exception_table_length); 197 for(int i=0; i < exception_table_length; i++) 198 exception_table[i].dump(file); 199 200 file.writeShort(attributes_count); 201 for(int i=0; i < attributes_count; i++) 202 attributes[i].dump(file); 203 } 204 205 209 public final Attribute[] getAttributes() { return attributes; } 210 211 214 public LineNumberTable getLineNumberTable() { 215 for(int i=0; i < attributes_count; i++) 216 if(attributes[i] instanceof LineNumberTable) 217 return (LineNumberTable)attributes[i]; 218 219 return null; 220 } 221 222 225 public LocalVariableTable getLocalVariableTable() { 226 for(int i=0; i < attributes_count; i++) 227 if(attributes[i] instanceof LocalVariableTable) 228 return (LocalVariableTable)attributes[i]; 229 230 return null; 231 } 232 233 236 public final byte[] getCode() { return code; } 237 238 242 public final CodeException[] getExceptionTable() { return exception_table; } 243 244 247 public final int getMaxLocals() { return max_locals; } 248 249 252 253 public final int getMaxStack() { return max_stack; } 254 255 259 private final int getInternalLength() { 260 return 2 + 2 + 4 261 + code_length 262 + 2 263 + 8 * exception_table_length 264 + 2 ; 265 } 266 267 271 private final int calculateLength() { 272 int len = 0; 273 274 for(int i=0; i < attributes_count; i++) 275 len += attributes[i].length + 6 ; 276 277 return len + getInternalLength(); 278 } 279 280 283 public final void setAttributes(Attribute[] attributes) { 284 this.attributes = attributes; 285 attributes_count = (attributes == null)? 0 : attributes.length; 286 length = calculateLength(); } 288 289 292 public final void setCode(byte[] code) { 293 this.code = code; 294 code_length = (code == null)? 0 : code.length; 295 } 296 297 300 public final void setExceptionTable(CodeException[] exception_table) { 301 this.exception_table = exception_table; 302 exception_table_length = (exception_table == null)? 0 : 303 exception_table.length; 304 } 305 306 309 public final void setMaxLocals(int max_locals) { 310 this.max_locals = max_locals; 311 } 312 313 316 public final void setMaxStack(int max_stack) { 317 this.max_stack = max_stack; 318 } 319 320 323 public final String toString(boolean verbose) { 324 StringBuffer buf; 325 326 buf = new StringBuffer ("Code(max_stack = " + max_stack + 327 ", max_locals = " + max_locals + 328 ", code_length = " + code_length + ")\n" + 329 Utility.codeToString(code, constant_pool, 0, -1, verbose)); 330 331 if(exception_table_length > 0) { 332 buf.append("\nException handler(s) = \n" + "From\tTo\tHandler\tType\n"); 333 334 for(int i=0; i < exception_table_length; i++) 335 buf.append(exception_table[i].toString(constant_pool, verbose) + "\n"); 336 } 337 338 if(attributes_count > 0) { 339 buf.append("\nAttribute(s) = \n"); 340 341 for(int i=0; i < attributes_count; i++) 342 buf.append(attributes[i].toString() + "\n"); 343 } 344 345 return buf.toString(); 346 } 347 348 351 public final String toString() { 352 return toString(true); 353 } 354 355 358 public Attribute copy(ConstantPool constant_pool) { 359 Code c = (Code)clone(); 360 c.code = (byte[])code.clone(); 361 c.constant_pool = constant_pool; 362 363 c.exception_table = new CodeException[exception_table_length]; 364 for(int i=0; i < exception_table_length; i++) 365 c.exception_table[i] = exception_table[i].copy(); 366 367 c.attributes = new Attribute[attributes_count]; 368 for(int i=0; i < attributes_count; i++) 369 c.attributes[i] = attributes[i].copy(constant_pool); 370 371 return c; 372 } 373 } 374 | Popular Tags |