1 18 19 package alt.jiapi.reflect.instruction; 20 21 import alt.jiapi.reflect.Instruction; 22 import alt.jiapi.file.ConstantPool; 23 24 32 public class CPInstruction extends Instruction { 33 protected ConstantPool cp; 34 35 public CPInstruction(byte[] bytes, ConstantPool cp) { 36 super(bytes); 37 this.cp = cp; 38 } 39 40 44 public int getIndex() { 45 byte[] bytes = getBytes(); 46 47 int index = 0; 48 if (bytes.length == 2) { 49 index = bytes[1] & 0xff; 50 } 51 else { 52 index = (short)(bytes[1] << 8); 53 index |= bytes[2] & 0xff; 54 } 55 56 return (0xffff) & index; 57 } 58 59 60 private void setIndex(short index) { 62 byte[] bytes = getBytes(); 63 64 if (bytes.length == 2) { 65 if (index > 256) { 66 if (bytes[0] == Opcodes.LDC) { 69 bytes = new byte[3]; 70 bytes[0] = Opcodes.LDC_W; 71 bytes[1] = (byte)((index >> 8) & 0xff); 72 bytes[2] = (byte)(index & 0xff); 73 setBytes(bytes); 74 } 75 else { 76 System.err.println("ERROR: cannot change index to " +index+ 77 ", because byte[] has only one byte. " + 78 this); 79 } 80 } 81 else { 82 bytes[1] = (byte)index; 83 } 84 } 85 else { 86 bytes[1] = (byte)((index >> 8) & 0xff); 87 bytes[2] = (byte)(index & 0xff); 88 } 89 90 if (getIndex() != index) { 91 System.out.println("ERROR setting index: " + getIndex() + " != " + 92 index + ": " + this); 93 } 94 } 95 96 97 public void replaceConstantPool(ConstantPool newCP) { 98 ConstantPool.Entry entry = getEntry(); 99 100 short index = newCP.add(entry); 101 102 setIndex(index); 103 this.cp = newCP; 104 } 105 106 107 110 public ConstantPool.Entry getEntry() { 111 return cp.get(getIndex()); 112 } 113 114 117 public ConstantPool getConstantPool() { 118 return cp; 119 } 120 121 public String toString() { 122 StringBuffer sb = new StringBuffer (super.toString()); 123 sb.append(" ; "); 124 sb.append(cp.get(getIndex())); 125 126 return sb.toString(); 127 } 128 129 } 130 | Popular Tags |