1 18 19 package alt.jiapi.reflect; 20 21 import java.util.HashMap ; 22 import alt.jiapi.reflect.instruction.Opcodes; 23 24 32 public class Instruction implements Cloneable { 33 37 private HashMap attributes; 38 41 private byte[] bytes; 42 45 private short offset; 46 47 private static final short UNKNOWN_STACK_USAGE = 99; 48 55 private static short[] stackUsages = new short[] { 56 0, 1,1,1,1,1,1,1,1, 58 2,2, 1,1,1, 60 2,2, 1,1,1,1, 62 2, 1, 64 2, 1, 66 2, 1,1,1,1,1, 68 2,2,2,2, 1,1,1,1, 70 1,1,1,1, 1,1,1,1, 73 -1, 74 0, -1, 76 0, -1,-1,-1,-1,-1, 78 -2, -1, 80 -2, -1,-1,-1,-1,-1, 82 -2,-2,-2,-2, -1,-1,-1,-1, 84 -2,-2,-2,-2, -1,-1,-1,-1, 87 -3, 88 -4, -3, 90 -4, -3,-3,-3,-3, -1, -2, 1,1,1,2,2,2, 0, 97 -1, 98 -2, -1, 100 -2, -1, 102 -2, -1, 104 -2, -1, 106 -2, -1, 108 -2, -1, 110 -2, -1, 112 -2, -1, 114 -2, -1, 116 -2, 0,0,0,0, 119 -1,-1,-1,-1,-1,-1,-1, 121 -2, -1, 123 -2, -1, 125 -2, 0, 128 1, 0, 130 1, -1, -1, 0,0, 134 1, 1, -1, 0, 138 -1, 0,0,0, 142 -3, -1,-1, 144 -3, -3, -1,-1,-1,-1,-1,-1, -2,-2,-2,-2,-2,-2,-2,-2, 0,1,0, 150 -1,-1, -1, 152 -2, -1, 154 -2, -1, 157 158 0,1,-1, 0,-2, 163 164 UNKNOWN_STACK_USAGE, UNKNOWN_STACK_USAGE, UNKNOWN_STACK_USAGE, UNKNOWN_STACK_USAGE, 171 0, 1, 172 0,0,0,0,0,0, -1,-1, 175 0, UNKNOWN_STACK_USAGE, 178 -1,-1, 179 0,1, 0, 0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, 0,0,0, 0,0 }; 189 190 191 198 private static short[] stackConsumptions = new short[] { 199 0, 0,0,0,0,0,0,0,0, 201 0,0, 0,0,0, 203 0,0, 0,0,0,0, 205 0, 0, 207 0, 0, 209 0, 0,0,0,0,0, 211 0,0,0,0, 0,0,0,0, 213 0,0,0,0, 0,0,0,0, 216 2, 217 2, 2, 219 2, 2,2,2,2, 221 1, 222 2, 1, 224 2, 1,1,1,1,1, 226 2,2,2,2, 1,1,1,1, 228 2,2,2,2, 1,1,1,1, 231 3, 232 4, 3, 234 4, 3,3,3,3, 1, 2, 1,2,2,2,2,2, 2, 241 2, 242 4, 2, 244 4, 2, 246 4, 2, 248 4, 2, 250 4, 2, 252 4, 2, 254 4, 2, 256 4, 2, 258 4, 2, 260 4, 1,2,1,2, 263 2,3,2,3,2,3,2, 265 4, 2, 267 4, 2, 269 4, 0, 272 1, 1, 274 1, 2, 2, 2,2, 278 1, 1, 2, 2, 282 2, 1,1,1, 286 4, 2,2, 288 4, 4, 1,1,1,1,1,1, 2,2,2,2,2,2,2,2, 0,0,0, 294 1,1, 1, 296 2, 1, 298 2, 1, 301 0,0,1,1,2, 303 UNKNOWN_STACK_USAGE, UNKNOWN_STACK_USAGE, UNKNOWN_STACK_USAGE, UNKNOWN_STACK_USAGE, 306 0, 0, 1,1,1,1,1,1, 1,1, 310 0, UNKNOWN_STACK_USAGE, 313 1,1, 0,0, 0, 0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, 0,0,0, 0,0 }; 324 325 329 330 331 335 Instruction() { 336 } 337 338 339 349 public Instruction(byte[] bytes) { 350 if (bytes.length == 0) { throw new IllegalArgumentException ("bytes.length of Instruction must be greater than 0"); 352 } 353 354 this.bytes = bytes; 355 } 356 357 358 363 protected void setBytes(byte[] bytes) { 364 this.bytes = bytes; 366 } 367 368 374 public byte[] getBytes() { 375 return bytes; 376 } 377 378 382 public byte getOpcode() { 383 return bytes[0]; 384 } 385 386 390 public int length() { 391 return bytes.length; 392 } 393 394 public String toString() { 395 int opCode = getOpcode(); 396 opCode = opCode & 0xff; 397 StringBuffer sb = new StringBuffer (Opcodes.opcodeStrings[opCode]); 398 399 for (int i = 1; i < bytes.length; i++) { 400 sb.append(" " + (0xff & bytes[i])); 401 } 402 403 return sb.toString(); 404 } 405 406 407 408 419 public short stackUsage() { 420 short opcode = getOpcode(); 421 422 423 if (opcode == Opcodes.INVOKEVIRTUAL) { 426 byte[] bytes = getBytes(); 427 429 } 432 435 return stackUsages[0xff & opcode]; 436 } 437 438 439 446 public short stackConsumption() { 447 short opcode = getOpcode(); 448 449 if (opcode == Opcodes.MULTIANEWARRAY) { 452 byte[] bytes = getBytes(); 453 return bytes[3]; 454 } 455 456 return stackConsumptions[0xff & opcode]; 457 } 458 459 460 461 465 protected void setOffset(short offset) { 466 this.offset = offset; 467 } 468 469 473 protected short getOffset() { 474 return offset; 475 } 476 477 480 public void setAttribute(String name) { 481 setAttribute(name, ""); 482 } 483 484 485 488 public void setAttribute(String name, Object value) { 489 if (attributes == null) { 490 attributes = new HashMap (); 491 } 492 493 attributes.put(name, value); 494 } 495 496 497 501 public Object getAttribute(String name) { 502 if (attributes == null) { 503 return null; 504 } 505 506 return attributes.get(name); 507 } 508 } 509 510 511 512 | Popular Tags |