1 package alt.jiapi.file; 2 3 import java.io.ByteArrayOutputStream ; 4 import java.io.DataInputStream ; 5 import java.io.DataOutputStream ; 6 import java.io.IOException ; 7 import java.util.List ; 8 import java.util.LinkedList ; 9 import java.util.Iterator ; 10 11 17 public class CodeAttribute extends Attribute { 18 private short max_stack; 19 private short max_locals; 20 private byte[] code; 21 private List exceptionTable = new LinkedList (); 22 private List attributes = new LinkedList (); 23 24 private transient Configuration config = new Configuration(); 25 26 30 public CodeAttribute(ConstantPool cp, short max_stack, short max_locals) { 31 super(cp.addUtf8Info("Code").getEntryIndex()); 32 33 setConstantPool(cp); 34 this.max_stack = max_stack; 35 this.max_locals = max_locals; 36 this.code = new byte[0]; 37 } 38 39 40 CodeAttribute(ConstantPool cp, short nameIndex, DataInputStream dis) throws IOException { 41 super(nameIndex); 42 setConstantPool(cp); 43 44 this.max_stack = dis.readShort(); 45 this.max_locals = dis.readShort(); 46 47 50 int codeLength = dis.readInt(); 51 this.code = new byte[codeLength]; 52 53 54 for (int i = 0; i < codeLength; i++) { 55 code[i] = dis.readByte(); 56 } 57 58 short exceptionTableLength = dis.readShort(); 59 for (int i = 0; i < exceptionTableLength; i++) { 60 short start_pc = dis.readShort(); 61 short end_pc = dis.readShort(); 62 short handler_pc = dis.readShort(); 63 short catch_type = dis.readShort(); 64 65 69 exceptionTable.add(new ExceptionTableEntry(start_pc, end_pc, 70 handler_pc,catch_type)); 71 } 72 73 short attrCount = dis.readShort(); 75 for (int i = 0; i < attrCount; i++) { 76 short attrNameIndex = dis.readShort(); 77 int length = dis.readInt(); 78 Attribute a = null; 79 80 String attrName = cp.getStringValue(attrNameIndex); 81 if ("LineNumberTable".equals(attrName)) { 82 a = new LineNumberTableAttribute(attrNameIndex, dis); 83 } 84 else if ("LocalVariableTable".equals(attrName)) { 85 a = new LocalVariableTableAttribute(attrNameIndex, dis); 86 } 87 else { 88 a = new Attribute(attrNameIndex, length, dis); 89 } 90 91 a.setConstantPool(cp); 92 93 attributes.add(a); 94 } 95 } 96 97 public short getMaxStack() { 98 return max_stack; 99 } 100 101 public void setMaxStack(short maxStack) { 102 this.max_stack = maxStack; 103 } 104 105 public short getMaxLocals() { 106 return max_locals; 107 } 108 109 public void setMaxLocals(short maxLocals) { 110 this.max_locals = maxLocals; 111 } 112 113 public byte[] getByteCode() { 114 return code; 115 } 116 117 public void setByteCode(byte[] byteCode) { 118 this.code = byteCode; 119 } 120 121 public List getExceptionTable() { 122 return exceptionTable; 123 } 124 125 126 public byte[] getBytes() { 127 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 128 DataOutputStream dos = new DataOutputStream (baos); 129 130 try { 131 dos.writeShort(max_stack); 132 dos.writeShort(max_locals); 133 134 dos.writeInt(code.length); 135 for (int i = 0; i < code.length; i++) { 136 dos.writeByte(code[i]); 137 } 138 139 dos.writeShort(exceptionTable.size()); 140 Iterator i = exceptionTable.iterator(); 141 while(i.hasNext()) { 142 ExceptionTableEntry ete = (ExceptionTableEntry)i.next(); 143 dos.writeShort(ete.getStartPc()); 144 dos.writeShort(ete.getEndPc()); 145 dos.writeShort(ete.getHandlerPc()); 146 dos.writeShort(ete.getCatchType()); 147 } 148 149 dos.writeShort(attributes.size()); 150 Iterator iter = attributes.iterator(); 151 while(iter.hasNext()) { 152 Attribute a = (Attribute)iter.next(); 153 dos.writeShort(a.getAttributeNameIndex()); 154 byte[] bytes = a.getBytes(); 155 156 dos.writeInt(bytes.length); 157 for(int j = 0; j < bytes.length; j++) { 158 dos.writeByte(bytes[j]); 159 } 160 } 161 } 162 catch(IOException ioe) { 163 throw new RuntimeException (ioe); 165 } 166 167 return baos.toByteArray(); 168 } 169 170 public class ExceptionTableEntry { 171 private short start_pc; 172 private short end_pc; 173 private short handler_pc; 174 private short catch_type; 175 176 ExceptionTableEntry(short start_pc, short end_pc, short handler_pc, 177 short catch_type) { 178 this.start_pc = start_pc; 179 this.end_pc = end_pc; 180 this.handler_pc = handler_pc; 181 this.catch_type = catch_type; 182 } 183 184 public short getStartPc() { 185 return start_pc; 186 } 187 public void setStartPc(short start_pc) { 188 this.start_pc = start_pc; 189 } 190 191 public short getEndPc() { 192 return end_pc; 193 } 194 public void setEndPc(short end_pc) { 195 this.end_pc = end_pc; 196 } 197 198 public short getHandlerPc() { 199 return handler_pc; 200 } 201 public void setHandlerPc(short handler_pc) { 202 this.handler_pc = handler_pc; 203 } 204 205 public short getCatchType() { 206 return catch_type; 207 } 208 } 209 210 211 214 public List getAttributes() { 215 if (config.getBoolean("alt.jiapi.file.strip-optional-attributes",true)){ 216 return new LinkedList (); 217 } 218 219 return attributes; 220 } 221 222 223 226 public Attribute getAttribute(String name) { 227 Iterator i = attributes.iterator(); 228 229 while(i.hasNext()) { 230 Attribute a = (Attribute)i.next(); 231 if (a.getName().equals(name)) { 232 return a; 233 } 234 } 235 236 return null; 237 } 238 } 239 240 | Popular Tags |