1 32 33 package com.jeantessier.classreader; 34 35 import java.io.*; 36 import java.util.*; 37 38 import org.apache.log4j.*; 39 40 public class Code_attribute extends Attribute_info { 41 private int maxStack; 42 private int maxLocals; 43 private byte[] code; 44 private Collection exceptionHandlers = new LinkedList(); 45 private Collection attributes = new LinkedList(); 46 47 public Code_attribute(Classfile classfile, Visitable owner, DataInputStream in) throws IOException { 48 super(classfile, owner); 49 50 int byteCount = in.readInt(); 51 Logger.getLogger(getClass()).debug("Attribute length: " + byteCount); 52 53 maxStack = in.readUnsignedShort(); 54 Logger.getLogger(getClass()).debug("Code max stack: " + maxStack); 55 56 maxLocals = in.readUnsignedShort(); 57 Logger.getLogger(getClass()).debug("Code max locals: " + maxLocals); 58 59 int codeLength = in.readInt(); 60 Logger.getLogger(getClass()).debug("Code length: " + codeLength); 61 code = new byte[codeLength]; 62 int bytesRead = in.read(code); 63 Logger.getLogger(getClass()).debug("Bytes read: " + bytesRead); 64 65 Iterator ci = iterator(); 66 while (ci.hasNext()) { 67 Instruction instr = (Instruction) ci.next(); 68 int start = instr.getStart(); 69 70 switch (instr.getOpcode()) { 71 case 0xb2: case 0xb3: case 0xb4: case 0xb5: case 0xb6: case 0xb7: case 0xb8: case 0xb9: case 0xbb: case 0xbd: case 0xc0: case 0xc1: case 0xc5: int index = ((code[start+1] & 0xff) << 8) | (code[start+2] & 0xff); 85 Logger.getLogger(getClass()).debug(" " + start + ": " + instr + " " + index + " (" + getClassfile().getConstantPool().get(index) + ")"); 86 break; 87 default: 88 Logger.getLogger(getClass()).debug(" " + start + ": " + instr + " (" + instr.getLength() + " byte(s))"); 89 break; 90 } 91 } 92 93 int exceptionTableLength = in.readUnsignedShort(); 94 Logger.getLogger(getClass()).debug("Reading " + exceptionTableLength + " exception handler(s) ..."); 95 for (int i=0; i<exceptionTableLength; i++) { 96 Logger.getLogger(getClass()).debug("Exception handler " + i + ":"); 97 exceptionHandlers.add(new ExceptionHandler(this, in)); 98 } 99 100 int attributeCount = in.readUnsignedShort(); 101 Logger.getLogger(getClass()).debug("Reading " + attributeCount + " code attribute(s)"); 102 for (int i=0; i<attributeCount; i++) { 103 Logger.getLogger(getClass()).debug("code attribute " + i + ":"); 104 attributes.add(AttributeFactory.create(getClassfile(), this, in)); 105 } 106 } 107 108 public int getMaxStack() { 109 return maxStack; 110 } 111 112 public int getMaxLocals() { 113 return maxLocals; 114 } 115 116 public byte[] getCode() { 117 return code; 118 } 119 120 public Iterator iterator() { 121 return new CodeIterator(code); 122 } 123 124 public Collection getExceptionHandlers() { 125 return exceptionHandlers; 126 } 127 128 public Collection getAttributes() { 129 return attributes; 130 } 131 132 public String toString() { 133 return "Code"; 134 } 135 136 public void accept(Visitor visitor) { 137 visitor.visitCode_attribute(this); 138 } 139 } 140 | Popular Tags |