1 26 package net.sf.javaguard.classfile; 27 28 import java.io.*; 29 import net.sf.javaguard.Tools; 30 31 32 37 public class CodeAttrInfo extends AttrInfo { 38 39 public static final int CONSTANT_FIELD_SIZE = 12; 40 41 43 private int maxStack; 44 45 private int maxLocals; 46 47 private int codeLength; 48 49 private byte[] byteCode; 50 51 private int exceptionTableLength; 52 53 private ExceptionInfo[] exceptionTable; 54 55 private int attributesLength; 56 57 private AttrInfo[] attributeInfo; 58 59 60 61 62 67 protected CodeAttrInfo(ClassFile cf, int attrNameIndex, int attrLength) { 68 super(cf, attrNameIndex, attrLength); 69 } 70 71 72 73 74 77 protected int getAttrInfoLength() { 78 int length = CONSTANT_FIELD_SIZE + getCodeLength() 79 + getExceptionTableLength() * ExceptionInfo.CONSTANT_FIELD_SIZE; 80 for (int i = 0; i < getAttributesLength(); i++) { 81 length += AttrInfo.CONSTANT_FIELD_SIZE + getAttributes()[i].getAttrInfoLength(); 82 } 83 return length; 84 } 85 86 87 88 89 92 protected String getAttrName() { 93 return ATTR_Code; 94 } 95 96 97 98 99 104 protected void setMaxStack(int depth) { 105 maxStack = depth; 106 } 107 108 109 114 protected int getMaxStack() { 115 return maxStack; 116 } 117 118 119 120 121 125 protected void setMaxLocals(int len) { 126 maxLocals = len; 127 } 128 129 130 134 protected int getMaxLocals() { 135 return maxLocals; 136 } 137 138 139 140 141 145 protected void setCodeLength(int len) { 146 codeLength = len; 147 } 148 149 150 154 protected int getCodeLength() { 155 return codeLength; 156 } 157 158 159 160 161 166 protected void setCode(byte[] code) { 167 byteCode = code; 168 } 169 170 171 176 protected byte[] getCode() { 177 return byteCode; 178 } 179 180 181 182 183 187 protected void setExceptionTableLength(int len) { 188 exceptionTableLength = len; 189 } 190 191 192 196 protected int getExceptionTableLength() { 197 return exceptionTableLength; 198 } 199 200 201 202 203 207 protected void setExceptionTable(ExceptionInfo[] table) { 208 exceptionTable = table; 209 } 210 211 212 216 protected ExceptionInfo[] getExceptionTable() { 217 return exceptionTable; 218 } 219 220 221 222 223 227 protected void setAttributesLength(int len) { 228 attributesLength = len; 229 } 230 231 232 236 protected int getAttributesLength() { 237 return attributesLength; 238 } 239 240 241 242 243 247 protected void setAttributes(AttrInfo[] array) { 248 attributeInfo = array; 249 } 250 251 252 256 protected AttrInfo[] getAttributes() { 257 return attributeInfo; 258 } 259 260 261 262 263 267 protected void trimAttrsExcept(String [] keepAttrs) { 268 for (int i = 0; i < getAttributesLength(); i++) { 270 if (Tools.isInArray(getAttributes()[i].getAttrName(), keepAttrs)) { 271 getAttributes()[i].trimAttrsExcept(keepAttrs); 272 } 273 else { 274 getAttributes()[i] = null; 275 } 276 } 277 278 AttrInfo[] left = new AttrInfo[getAttributesLength()]; 280 int j = 0; 281 for (int i = 0; i < getAttributesLength(); i++) { 282 if (getAttributes()[i] != null) { 283 left[j++] = getAttributes()[i]; 284 } 285 } 286 AttrInfo[] attrs = new AttrInfo[j]; 287 System.arraycopy(left, 0, attrs, 0, j); 288 setAttributes(attrs); 289 setAttributesLength(j); 290 } 291 292 293 294 295 299 protected void markUtf8RefsInInfo(ConstantPool pool) { 300 for (int i = 0; i < getAttributesLength(); i++) { 301 getAttributes()[i].markUtf8Refs(pool); 302 } 303 } 304 305 306 307 308 312 protected void readInfo(DataInput din) 313 throws IOException { 314 setMaxStack(din.readUnsignedShort()); 315 setMaxLocals(din.readUnsignedShort()); 316 setCodeLength(din.readInt()); 317 byte[] code = new byte[getCodeLength()]; 318 din.readFully(code); 319 setCode(code); 320 setExceptionTableLength(din.readUnsignedShort()); 321 ExceptionInfo[] exTable = new ExceptionInfo[getExceptionTableLength()]; 322 for (int i = 0; i < getExceptionTableLength(); i++) { 323 exTable[i] = ExceptionInfo.create(din); 324 } 325 setExceptionTable(exTable); 326 setAttributesLength(din.readUnsignedShort()); 327 AttrInfo[] attributes = new AttrInfo[getAttributesLength()]; 328 for (int i = 0; i < getAttributesLength(); i++) { 329 attributes[i] = AttrInfo.create(din, owner); 330 } 331 setAttributes(attributes); 332 } 333 334 335 340 public void writeInfo(DataOutput dout) 341 throws IOException { 342 dout.writeShort(getMaxStack()); 343 dout.writeShort(getMaxLocals()); 344 dout.writeInt(getCodeLength()); 345 dout.write(getCode()); 346 dout.writeShort(getExceptionTableLength()); 347 for (int i = 0; i < getExceptionTableLength(); i++) { 348 getExceptionTable()[i].write(dout); 349 } 350 dout.writeShort(getAttributesLength()); 351 for (int i = 0; i < getAttributesLength(); i++) { 352 getAttributes()[i].write(dout); 353 } 354 } 355 356 357 358 359 363 public void dump(PrintWriter pw, ClassFile cf) { 364 pw.println(getAttrName()); 365 pw.print("maxStack: "); pw.println(getMaxStack()); 366 pw.print("maxLocals: "); pw.println(getMaxLocals()); 367 pw.print("codeLength: "); pw.println(getCodeLength()); 368 pw.print("exceptions: "); pw.println(getExceptionTableLength()); 369 for (int i=0; i<getExceptionTableLength(); i++) { 370 getExceptionTable()[i].dump(pw, cf); 371 } 372 pw.print("attributes: "); pw.println(getAttributesLength()); 373 for (int i=0; i<getAttributesLength(); i++) { 374 getAttributes()[i].dump(pw, cf); 375 } 376 } 377 } 378 | Popular Tags |