1 7 8 package org.gjt.jclasslib.structures.attributes; 9 10 import org.gjt.jclasslib.structures.AttributeInfo; 11 import org.gjt.jclasslib.structures.InvalidByteCodeException; 12 13 import java.io.*; 14 15 21 public class CodeAttribute extends AttributeInfo { 22 23 24 public static final String ATTRIBUTE_NAME = "Code"; 25 26 private static final int INITIAL_LENGTH = 12; 27 28 private int maxStack; 29 private int maxLocals; 30 private byte[] code; 31 private ExceptionTableEntry[] exceptionTable; 32 33 37 public int getMaxStack() { 38 return maxStack; 39 } 40 41 45 public void setMaxStack(int maxStack) { 46 this.maxStack = maxStack; 47 } 48 49 53 public int getMaxLocals() { 54 return maxLocals; 55 } 56 57 61 public void setMaxLocals(int maxLocals) { 62 this.maxLocals = maxLocals; 63 } 64 65 69 public byte[] getCode() { 70 return code; 71 } 72 73 77 public void setCode(byte[] code) { 78 this.code = code; 79 } 80 81 86 public ExceptionTableEntry[] getExceptionTable() { 87 return exceptionTable; 88 } 89 90 95 public void setExceptionTable(ExceptionTableEntry[] exceptionTable) { 96 this.exceptionTable = exceptionTable; 97 } 98 99 public void read(DataInput in) 100 throws InvalidByteCodeException, IOException { 101 102 maxStack = in.readUnsignedShort(); 103 maxLocals = in.readUnsignedShort(); 104 int codeLength = in.readInt(); 105 code = new byte[codeLength]; 106 in.readFully(code); 107 108 readExceptionTable(in); 109 readAttributes(in); 110 if (debug) debug("read "); 111 } 112 113 public void write(DataOutput out) 114 throws InvalidByteCodeException, IOException { 115 116 super.write(out); 117 out.writeShort(maxStack); 118 out.writeShort(maxLocals); 119 out.writeInt(getLength(code)); 120 out.write(code); 121 122 writeExceptionTable(out); 123 writeAttributes(out); 124 125 if (debug) debug("wrote "); 126 } 127 128 private void readExceptionTable(DataInput in) 129 throws InvalidByteCodeException, IOException { 130 131 int exceptionTableLength = in.readUnsignedShort(); 132 exceptionTable = new ExceptionTableEntry[exceptionTableLength]; 133 for (int i = 0; i < exceptionTableLength; i++) { 134 exceptionTable[i] = ExceptionTableEntry.create(in, classFile); 135 } 136 137 } 138 139 private void writeExceptionTable(DataOutput out) 140 throws InvalidByteCodeException, IOException { 141 142 int exceptionTableLength = getLength(exceptionTable); 143 144 out.writeShort(exceptionTableLength); 145 for (int i = 0; i < exceptionTableLength; i++) { 146 exceptionTable[i].write(out); 147 } 148 149 } 150 151 public int getAttributeLength() { 152 return INITIAL_LENGTH + getLength(code) + 153 getLength(exceptionTable) * ExceptionTableEntry.LENGTH + 154 6 * getLength(attributes) + 155 getTotalAttributesLength() ; 156 } 157 158 protected void debug(String message) { 159 super.debug(message + "Code attribute with max_stack " + maxStack + 160 ", max_locals " + maxLocals + ", code_length " + getLength(code)); 161 } 162 163 } 164 | Popular Tags |