1 7 8 package org.gjt.jclasslib.io; 9 10 import org.gjt.jclasslib.bytecode.*; 11 12 import java.io.ByteArrayInputStream ; 13 import java.io.IOException ; 14 import java.util.ArrayList ; 15 import java.util.List ; 16 17 24 public class ByteCodeReader implements Opcodes { 25 26 private ByteCodeReader() { 27 } 28 29 35 public static List readByteCode(byte[] code) throws IOException { 36 return readByteCode(code, null); 37 } 38 39 46 public static List readByteCode(byte[] code, 47 AbstractInstruction[] prependInstructions) 48 throws IOException { 49 50 ByteCodeInputStream bcis = new ByteCodeInputStream( 51 new ByteArrayInputStream (code) 52 ); 53 54 ArrayList instructions = new ArrayList (); 55 if (prependInstructions != null) { 56 for (int i = 0; i < prependInstructions.length; i++) { 57 instructions.add(prependInstructions[i]); 58 } 59 } 60 61 boolean wide = false; 62 AbstractInstruction currentInstruction; 63 while (bcis.getBytesRead() < code.length) { 64 currentInstruction = readNextInstruction(bcis, wide); 65 wide = (currentInstruction.getOpcode() == OPCODE_WIDE); 66 instructions.add(currentInstruction); 67 } 68 69 return instructions; 70 } 71 72 private static AbstractInstruction readNextInstruction(ByteCodeInputStream bcis, boolean wide) 73 throws IOException 74 { 75 AbstractInstruction instruction; 76 77 int opcode = bcis.readUnsignedByte(); 78 79 switch (opcode) { 80 81 case OPCODE_WIDE: 82 case OPCODE_NOP: 83 case OPCODE_ACONST_NULL: 84 case OPCODE_ICONST_M1: 85 case OPCODE_ICONST_0: 86 case OPCODE_ICONST_1: 87 case OPCODE_ICONST_2: 88 case OPCODE_ICONST_3: 89 case OPCODE_ICONST_4: 90 case OPCODE_ICONST_5: 91 case OPCODE_LCONST_0: 92 case OPCODE_LCONST_1: 93 case OPCODE_FCONST_0: 94 case OPCODE_FCONST_1: 95 case OPCODE_FCONST_2: 96 case OPCODE_DCONST_0: 97 case OPCODE_DCONST_1: 98 case OPCODE_ILOAD_0: 99 case OPCODE_ILOAD_1: 100 case OPCODE_ILOAD_2: 101 case OPCODE_ILOAD_3: 102 case OPCODE_LLOAD_0: 103 case OPCODE_LLOAD_1: 104 case OPCODE_LLOAD_2: 105 case OPCODE_LLOAD_3: 106 case OPCODE_FLOAD_0: 107 case OPCODE_FLOAD_1: 108 case OPCODE_FLOAD_2: 109 case OPCODE_FLOAD_3: 110 case OPCODE_DLOAD_0: 111 case OPCODE_DLOAD_1: 112 case OPCODE_DLOAD_2: 113 case OPCODE_DLOAD_3: 114 case OPCODE_ALOAD_0: 115 case OPCODE_ALOAD_1: 116 case OPCODE_ALOAD_2: 117 case OPCODE_ALOAD_3: 118 case OPCODE_IALOAD: 119 case OPCODE_LALOAD: 120 case OPCODE_FALOAD: 121 case OPCODE_DALOAD: 122 case OPCODE_AALOAD: 123 case OPCODE_BALOAD: 124 case OPCODE_CALOAD: 125 case OPCODE_SALOAD: 126 case OPCODE_ISTORE_0: 127 case OPCODE_ISTORE_1: 128 case OPCODE_ISTORE_2: 129 case OPCODE_ISTORE_3: 130 case OPCODE_LSTORE_0: 131 case OPCODE_LSTORE_1: 132 case OPCODE_LSTORE_2: 133 case OPCODE_LSTORE_3: 134 case OPCODE_FSTORE_0: 135 case OPCODE_FSTORE_1: 136 case OPCODE_FSTORE_2: 137 case OPCODE_FSTORE_3: 138 case OPCODE_DSTORE_0: 139 case OPCODE_DSTORE_1: 140 case OPCODE_DSTORE_2: 141 case OPCODE_DSTORE_3: 142 case OPCODE_ASTORE_0: 143 case OPCODE_ASTORE_1: 144 case OPCODE_ASTORE_2: 145 case OPCODE_ASTORE_3: 146 case OPCODE_IASTORE: 147 case OPCODE_LASTORE: 148 case OPCODE_FASTORE: 149 case OPCODE_DASTORE: 150 case OPCODE_AASTORE: 151 case OPCODE_BASTORE: 152 case OPCODE_CASTORE: 153 case OPCODE_SASTORE: 154 case OPCODE_POP: 155 case OPCODE_POP2: 156 case OPCODE_DUP: 157 case OPCODE_DUP_X1: 158 case OPCODE_DUP_X2: 159 case OPCODE_DUP2: 160 case OPCODE_DUP2_X1: 161 case OPCODE_DUP2_X2: 162 case OPCODE_SWAP: 163 case OPCODE_IADD: 164 case OPCODE_LADD: 165 case OPCODE_FADD: 166 case OPCODE_DADD: 167 case OPCODE_ISUB: 168 case OPCODE_LSUB: 169 case OPCODE_FSUB: 170 case OPCODE_DSUB: 171 case OPCODE_IMUL: 172 case OPCODE_LMUL: 173 case OPCODE_FMUL: 174 case OPCODE_DMUL: 175 case OPCODE_IDIV: 176 case OPCODE_LDIV: 177 case OPCODE_FDIV: 178 case OPCODE_DDIV: 179 case OPCODE_IREM: 180 case OPCODE_LREM: 181 case OPCODE_FREM: 182 case OPCODE_DREM: 183 case OPCODE_INEG: 184 case OPCODE_LNEG: 185 case OPCODE_FNEG: 186 case OPCODE_DNEG: 187 case OPCODE_ISHL: 188 case OPCODE_LSHL: 189 case OPCODE_ISHR: 190 case OPCODE_LSHR: 191 case OPCODE_IUSHR: 192 case OPCODE_LUSHR: 193 case OPCODE_IAND: 194 case OPCODE_LAND: 195 case OPCODE_IOR: 196 case OPCODE_LOR: 197 case OPCODE_IXOR: 198 case OPCODE_LXOR: 199 case OPCODE_I2L: 200 case OPCODE_I2F: 201 case OPCODE_I2D: 202 case OPCODE_L2I: 203 case OPCODE_L2F: 204 case OPCODE_L2D: 205 case OPCODE_F2I: 206 case OPCODE_F2L: 207 case OPCODE_F2D: 208 case OPCODE_D2I: 209 case OPCODE_D2L: 210 case OPCODE_D2F: 211 case OPCODE_I2B: 212 case OPCODE_I2C: 213 case OPCODE_I2S: 214 case OPCODE_LCMP: 215 case OPCODE_FCMPL: 216 case OPCODE_FCMPG: 217 case OPCODE_DCMPL: 218 case OPCODE_DCMPG: 219 case OPCODE_IRETURN: 220 case OPCODE_LRETURN: 221 case OPCODE_FRETURN: 222 case OPCODE_DRETURN: 223 case OPCODE_ARETURN: 224 case OPCODE_RETURN: 225 case OPCODE_XXXUNUSEDXXX: 226 case OPCODE_ARRAYLENGTH: 227 case OPCODE_ATHROW: 228 case OPCODE_MONITORENTER: 229 case OPCODE_MONITOREXIT: 230 case OPCODE_BREAKPOINT: 231 case OPCODE_IMPDEP1: 232 case OPCODE_IMPDEP2: 233 234 instruction = new SimpleInstruction(opcode); 235 break; 236 237 case OPCODE_BIPUSH: 238 case OPCODE_LDC: 239 case OPCODE_ILOAD: case OPCODE_LLOAD: case OPCODE_FLOAD: case OPCODE_DLOAD: case OPCODE_ALOAD: case OPCODE_ISTORE: case OPCODE_LSTORE: case OPCODE_FSTORE: case OPCODE_DSTORE: case OPCODE_ASTORE: case OPCODE_RET: case OPCODE_NEWARRAY: 251 252 instruction = new ImmediateByteInstruction(opcode, wide); 253 break; 254 255 case OPCODE_LDC_W: 256 case OPCODE_LDC2_W: 257 case OPCODE_GETSTATIC: 258 case OPCODE_PUTSTATIC: 259 case OPCODE_GETFIELD: 260 case OPCODE_PUTFIELD: 261 case OPCODE_INVOKEVIRTUAL: 262 case OPCODE_INVOKESPECIAL: 263 case OPCODE_INVOKESTATIC: 264 case OPCODE_NEW: 265 case OPCODE_ANEWARRAY: 266 case OPCODE_CHECKCAST: 267 case OPCODE_INSTANCEOF: 268 case OPCODE_SIPUSH: 271 instruction = new ImmediateShortInstruction(opcode); 272 break; 273 274 case OPCODE_IFEQ: 275 case OPCODE_IFNE: 276 case OPCODE_IFLT: 277 case OPCODE_IFGE: 278 case OPCODE_IFGT: 279 case OPCODE_IFLE: 280 case OPCODE_IF_ICMPEQ: 281 case OPCODE_IF_ICMPNE: 282 case OPCODE_IF_ICMPLT: 283 case OPCODE_IF_ICMPGE: 284 case OPCODE_IF_ICMPGT: 285 case OPCODE_IF_ICMPLE: 286 case OPCODE_IF_ACMPEQ: 287 case OPCODE_IF_ACMPNE: 288 case OPCODE_GOTO: 289 case OPCODE_JSR: 290 case OPCODE_IFNULL: 291 case OPCODE_IFNONNULL: 292 293 instruction = new BranchInstruction(opcode); 294 break; 295 296 case OPCODE_GOTO_W: 297 case OPCODE_JSR_W: 298 299 instruction = new ImmediateIntInstruction(opcode); 300 break; 301 302 case OPCODE_IINC: 304 instruction = new IncrementInstruction(opcode, wide); 305 break; 306 307 case OPCODE_TABLESWITCH: 308 309 instruction = new TableSwitchInstruction(opcode); 310 break; 311 312 case OPCODE_LOOKUPSWITCH: 313 314 instruction = new LookupSwitchInstruction(opcode); 315 break; 316 317 case OPCODE_INVOKEINTERFACE: 318 319 instruction = new InvokeInterfaceInstruction(opcode); 320 break; 321 322 case OPCODE_MULTIANEWARRAY: 323 324 instruction = new MultianewarrayInstruction(opcode); 325 break; 326 327 default: 328 throw new IOException ("invalid opcode 0x" + Integer.toHexString(opcode)); 329 } 330 331 instruction.read(bcis); 332 return instruction; 333 } 334 335 } 336 | Popular Tags |