1 30 package org.objectweb.asm.xml; 31 32 import java.io.IOException ; 33 import java.io.OutputStream ; 34 import java.util.ArrayList ; 35 import java.util.HashMap ; 36 import java.util.Iterator ; 37 import java.util.List ; 38 import java.util.Map ; 39 40 import org.objectweb.asm.AnnotationVisitor; 41 import org.objectweb.asm.ClassVisitor; 42 import org.objectweb.asm.ClassWriter; 43 import org.objectweb.asm.FieldVisitor; 44 import org.objectweb.asm.MethodVisitor; 45 import org.objectweb.asm.Opcodes; 46 import org.objectweb.asm.Label; 47 import org.objectweb.asm.Type; 48 49 import org.xml.sax.Attributes ; 50 import org.xml.sax.SAXException ; 51 import org.xml.sax.helpers.DefaultHandler ; 52 53 63 public class ASMContentHandler extends DefaultHandler implements Opcodes { 64 67 private List stack = new ArrayList (); 68 69 72 private String match = ""; 73 74 78 protected boolean computeMax; 79 80 83 protected OutputStream os; 84 85 89 protected ClassWriter cw; 90 91 94 protected Map labels; 95 96 private static final String BASE = "class"; 97 98 private final RuleSet RULES = new RuleSet(); 99 { 100 RULES.add(BASE, new ClassRule()); 101 RULES.add(BASE + "/interfaces/interface", new InterfaceRule()); 102 RULES.add(BASE + "/interfaces", new InterfacesRule()); 103 RULES.add(BASE + "/outerclass", new OuterClassRule()); 104 RULES.add(BASE + "/innerclass", new InnerClassRule()); 105 RULES.add(BASE + "/source", new SourceRule()); 106 RULES.add(BASE + "/field", new FieldRule()); 107 108 RULES.add(BASE + "/method", new MethodRule()); 109 RULES.add(BASE + "/method/exceptions/exception", new ExceptionRule()); 110 RULES.add(BASE + "/method/exceptions", new ExceptionsRule()); 111 112 RULES.add(BASE + "/method/annotationDefault", 113 new AnnotationDefaultRule()); 114 115 RULES.add(BASE + "/method/code/*", new OpcodesRule()); 117 RULES.add(BASE + "/method/code/TABLESWITCH", new TableSwitchRule()); 118 RULES.add(BASE + "/method/code/TABLESWITCH/label", 119 new TableSwitchLabelRule()); 120 RULES.add(BASE + "/method/code/LOOKUPSWITCH", new LookupSwitchRule()); 121 RULES.add(BASE + "/method/code/LOOKUPSWITCH/label", 122 new LookupSwitchLabelRule()); 123 124 RULES.add(BASE + "/method/code/Label", new LabelRule()); 125 RULES.add(BASE + "/method/code/TryCatch", new TryCatchRule()); 126 RULES.add(BASE + "/method/code/LineNumber", new LineNumberRule()); 127 RULES.add(BASE + "/method/code/LocalVar", new LocalVarRule()); 128 RULES.add(BASE + "/method/code/Max", new MaxRule()); 129 130 RULES.add("*/annotation", new AnnotationRule()); 131 RULES.add("*/parameterAnnotation", new AnnotationParameterRule()); 132 RULES.add("*/annotationValue", new AnnotationValueRule()); 133 RULES.add("*/annotationValueAnnotation", 134 new AnnotationValueAnnotationRule()); 135 RULES.add("*/annotationValueEnum", new AnnotationValueEnumRule()); 136 RULES.add("*/annotationValueArray", new AnnotationValueArrayRule()); 137 }; 138 139 private static interface OpcodeGroup { 140 public static final int INSN = 0; 141 public static final int INSN_INT = 1; 142 public static final int INSN_VAR = 2; 143 public static final int INSN_TYPE = 3; 144 public static final int INSN_FIELD = 4; 145 public static final int INSN_METHOD = 5; 146 public static final int INSN_JUMP = 6; 147 public static final int INSN_LDC = 7; 148 public static final int INSN_IINC = 8; 149 public static final int INSN_MULTIANEWARRAY = 9; 150 } 151 152 155 static final Map OPCODES = new HashMap (); 156 static { 157 OPCODES.put("NOP", new Opcode(NOP, OpcodeGroup.INSN)); 158 OPCODES.put("ACONST_NULL", new Opcode(ACONST_NULL, OpcodeGroup.INSN)); 159 OPCODES.put("ICONST_M1", new Opcode(ICONST_M1, OpcodeGroup.INSN)); 160 OPCODES.put("ICONST_0", new Opcode(ICONST_0, OpcodeGroup.INSN)); 161 OPCODES.put("ICONST_1", new Opcode(ICONST_1, OpcodeGroup.INSN)); 162 OPCODES.put("ICONST_2", new Opcode(ICONST_2, OpcodeGroup.INSN)); 163 OPCODES.put("ICONST_3", new Opcode(ICONST_3, OpcodeGroup.INSN)); 164 OPCODES.put("ICONST_4", new Opcode(ICONST_4, OpcodeGroup.INSN)); 165 OPCODES.put("ICONST_5", new Opcode(ICONST_5, OpcodeGroup.INSN)); 166 OPCODES.put("LCONST_0", new Opcode(LCONST_0, OpcodeGroup.INSN)); 167 OPCODES.put("LCONST_1", new Opcode(LCONST_1, OpcodeGroup.INSN)); 168 OPCODES.put("FCONST_0", new Opcode(FCONST_0, OpcodeGroup.INSN)); 169 OPCODES.put("FCONST_1", new Opcode(FCONST_1, OpcodeGroup.INSN)); 170 OPCODES.put("FCONST_2", new Opcode(FCONST_2, OpcodeGroup.INSN)); 171 OPCODES.put("DCONST_0", new Opcode(DCONST_0, OpcodeGroup.INSN)); 172 OPCODES.put("DCONST_1", new Opcode(DCONST_1, OpcodeGroup.INSN)); 173 OPCODES.put("BIPUSH", new Opcode(BIPUSH, OpcodeGroup.INSN_INT)); 174 OPCODES.put("SIPUSH", new Opcode(SIPUSH, OpcodeGroup.INSN_INT)); 175 OPCODES.put("LDC", new Opcode(LDC, OpcodeGroup.INSN_LDC)); 176 OPCODES.put("ILOAD", new Opcode(ILOAD, OpcodeGroup.INSN_VAR)); 177 OPCODES.put("LLOAD", new Opcode(LLOAD, OpcodeGroup.INSN_VAR)); 178 OPCODES.put("FLOAD", new Opcode(FLOAD, OpcodeGroup.INSN_VAR)); 179 OPCODES.put("DLOAD", new Opcode(DLOAD, OpcodeGroup.INSN_VAR)); 180 OPCODES.put("ALOAD", new Opcode(ALOAD, OpcodeGroup.INSN_VAR)); 181 OPCODES.put("IALOAD", new Opcode(IALOAD, OpcodeGroup.INSN)); 182 OPCODES.put("LALOAD", new Opcode(LALOAD, OpcodeGroup.INSN)); 183 OPCODES.put("FALOAD", new Opcode(FALOAD, OpcodeGroup.INSN)); 184 OPCODES.put("DALOAD", new Opcode(DALOAD, OpcodeGroup.INSN)); 185 OPCODES.put("AALOAD", new Opcode(AALOAD, OpcodeGroup.INSN)); 186 OPCODES.put("BALOAD", new Opcode(BALOAD, OpcodeGroup.INSN)); 187 OPCODES.put("CALOAD", new Opcode(CALOAD, OpcodeGroup.INSN)); 188 OPCODES.put("SALOAD", new Opcode(SALOAD, OpcodeGroup.INSN)); 189 OPCODES.put("ISTORE", new Opcode(ISTORE, OpcodeGroup.INSN_VAR)); 190 OPCODES.put("LSTORE", new Opcode(LSTORE, OpcodeGroup.INSN_VAR)); 191 OPCODES.put("FSTORE", new Opcode(FSTORE, OpcodeGroup.INSN_VAR)); 192 OPCODES.put("DSTORE", new Opcode(DSTORE, OpcodeGroup.INSN_VAR)); 193 OPCODES.put("ASTORE", new Opcode(ASTORE, OpcodeGroup.INSN_VAR)); 194 OPCODES.put("IASTORE", new Opcode(IASTORE, OpcodeGroup.INSN)); 195 OPCODES.put("LASTORE", new Opcode(LASTORE, OpcodeGroup.INSN)); 196 OPCODES.put("FASTORE", new Opcode(FASTORE, OpcodeGroup.INSN)); 197 OPCODES.put("DASTORE", new Opcode(DASTORE, OpcodeGroup.INSN)); 198 OPCODES.put("AASTORE", new Opcode(AASTORE, OpcodeGroup.INSN)); 199 OPCODES.put("BASTORE", new Opcode(BASTORE, OpcodeGroup.INSN)); 200 OPCODES.put("CASTORE", new Opcode(CASTORE, OpcodeGroup.INSN)); 201 OPCODES.put("SASTORE", new Opcode(SASTORE, OpcodeGroup.INSN)); 202 OPCODES.put("POP", new Opcode(POP, OpcodeGroup.INSN)); 203 OPCODES.put("POP2", new Opcode(POP2, OpcodeGroup.INSN)); 204 OPCODES.put("DUP", new Opcode(DUP, OpcodeGroup.INSN)); 205 OPCODES.put("DUP_X1", new Opcode(DUP_X1, OpcodeGroup.INSN)); 206 OPCODES.put("DUP_X2", new Opcode(DUP_X2, OpcodeGroup.INSN)); 207 OPCODES.put("DUP2", new Opcode(DUP2, OpcodeGroup.INSN)); 208 OPCODES.put("DUP2_X1", new Opcode(DUP2_X1, OpcodeGroup.INSN)); 209 OPCODES.put("DUP2_X2", new Opcode(DUP2_X2, OpcodeGroup.INSN)); 210 OPCODES.put("SWAP", new Opcode(SWAP, OpcodeGroup.INSN)); 211 OPCODES.put("IADD", new Opcode(IADD, OpcodeGroup.INSN)); 212 OPCODES.put("LADD", new Opcode(LADD, OpcodeGroup.INSN)); 213 OPCODES.put("FADD", new Opcode(FADD, OpcodeGroup.INSN)); 214 OPCODES.put("DADD", new Opcode(DADD, OpcodeGroup.INSN)); 215 OPCODES.put("ISUB", new Opcode(ISUB, OpcodeGroup.INSN)); 216 OPCODES.put("LSUB", new Opcode(LSUB, OpcodeGroup.INSN)); 217 OPCODES.put("FSUB", new Opcode(FSUB, OpcodeGroup.INSN)); 218 OPCODES.put("DSUB", new Opcode(DSUB, OpcodeGroup.INSN)); 219 OPCODES.put("IMUL", new Opcode(IMUL, OpcodeGroup.INSN)); 220 OPCODES.put("LMUL", new Opcode(LMUL, OpcodeGroup.INSN)); 221 OPCODES.put("FMUL", new Opcode(FMUL, OpcodeGroup.INSN)); 222 OPCODES.put("DMUL", new Opcode(DMUL, OpcodeGroup.INSN)); 223 OPCODES.put("IDIV", new Opcode(IDIV, OpcodeGroup.INSN)); 224 OPCODES.put("LDIV", new Opcode(LDIV, OpcodeGroup.INSN)); 225 OPCODES.put("FDIV", new Opcode(FDIV, OpcodeGroup.INSN)); 226 OPCODES.put("DDIV", new Opcode(DDIV, OpcodeGroup.INSN)); 227 OPCODES.put("IREM", new Opcode(IREM, OpcodeGroup.INSN)); 228 OPCODES.put("LREM", new Opcode(LREM, OpcodeGroup.INSN)); 229 OPCODES.put("FREM", new Opcode(FREM, OpcodeGroup.INSN)); 230 OPCODES.put("DREM", new Opcode(DREM, OpcodeGroup.INSN)); 231 OPCODES.put("INEG", new Opcode(INEG, OpcodeGroup.INSN)); 232 OPCODES.put("LNEG", new Opcode(LNEG, OpcodeGroup.INSN)); 233 OPCODES.put("FNEG", new Opcode(FNEG, OpcodeGroup.INSN)); 234 OPCODES.put("DNEG", new Opcode(DNEG, OpcodeGroup.INSN)); 235 OPCODES.put("ISHL", new Opcode(ISHL, OpcodeGroup.INSN)); 236 OPCODES.put("LSHL", new Opcode(LSHL, OpcodeGroup.INSN)); 237 OPCODES.put("ISHR", new Opcode(ISHR, OpcodeGroup.INSN)); 238 OPCODES.put("LSHR", new Opcode(LSHR, OpcodeGroup.INSN)); 239 OPCODES.put("IUSHR", new Opcode(IUSHR, OpcodeGroup.INSN)); 240 OPCODES.put("LUSHR", new Opcode(LUSHR, OpcodeGroup.INSN)); 241 OPCODES.put("IAND", new Opcode(IAND, OpcodeGroup.INSN)); 242 OPCODES.put("LAND", new Opcode(LAND, OpcodeGroup.INSN)); 243 OPCODES.put("IOR", new Opcode(IOR, OpcodeGroup.INSN)); 244 OPCODES.put("LOR", new Opcode(LOR, OpcodeGroup.INSN)); 245 OPCODES.put("IXOR", new Opcode(IXOR, OpcodeGroup.INSN)); 246 OPCODES.put("LXOR", new Opcode(LXOR, OpcodeGroup.INSN)); 247 OPCODES.put("IINC", new Opcode(IINC, OpcodeGroup.INSN_IINC)); 248 OPCODES.put("I2L", new Opcode(I2L, OpcodeGroup.INSN)); 249 OPCODES.put("I2F", new Opcode(I2F, OpcodeGroup.INSN)); 250 OPCODES.put("I2D", new Opcode(I2D, OpcodeGroup.INSN)); 251 OPCODES.put("L2I", new Opcode(L2I, OpcodeGroup.INSN)); 252 OPCODES.put("L2F", new Opcode(L2F, OpcodeGroup.INSN)); 253 OPCODES.put("L2D", new Opcode(L2D, OpcodeGroup.INSN)); 254 OPCODES.put("F2I", new Opcode(F2I, OpcodeGroup.INSN)); 255 OPCODES.put("F2L", new Opcode(F2L, OpcodeGroup.INSN)); 256 OPCODES.put("F2D", new Opcode(F2D, OpcodeGroup.INSN)); 257 OPCODES.put("D2I", new Opcode(D2I, OpcodeGroup.INSN)); 258 OPCODES.put("D2L", new Opcode(D2L, OpcodeGroup.INSN)); 259 OPCODES.put("D2F", new Opcode(D2F, OpcodeGroup.INSN)); 260 OPCODES.put("I2B", new Opcode(I2B, OpcodeGroup.INSN)); 261 OPCODES.put("I2C", new Opcode(I2C, OpcodeGroup.INSN)); 262 OPCODES.put("I2S", new Opcode(I2S, OpcodeGroup.INSN)); 263 OPCODES.put("LCMP", new Opcode(LCMP, OpcodeGroup.INSN)); 264 OPCODES.put("FCMPL", new Opcode(FCMPL, OpcodeGroup.INSN)); 265 OPCODES.put("FCMPG", new Opcode(FCMPG, OpcodeGroup.INSN)); 266 OPCODES.put("DCMPL", new Opcode(DCMPL, OpcodeGroup.INSN)); 267 OPCODES.put("DCMPG", new Opcode(DCMPG, OpcodeGroup.INSN)); 268 OPCODES.put("IFEQ", new Opcode(IFEQ, OpcodeGroup.INSN_JUMP)); 269 OPCODES.put("IFNE", new Opcode(IFNE, OpcodeGroup.INSN_JUMP)); 270 OPCODES.put("IFLT", new Opcode(IFLT, OpcodeGroup.INSN_JUMP)); 271 OPCODES.put("IFGE", new Opcode(IFGE, OpcodeGroup.INSN_JUMP)); 272 OPCODES.put("IFGT", new Opcode(IFGT, OpcodeGroup.INSN_JUMP)); 273 OPCODES.put("IFLE", new Opcode(IFLE, OpcodeGroup.INSN_JUMP)); 274 OPCODES.put("IF_ICMPEQ", new Opcode(IF_ICMPEQ, OpcodeGroup.INSN_JUMP)); 275 OPCODES.put("IF_ICMPNE", new Opcode(IF_ICMPNE, OpcodeGroup.INSN_JUMP)); 276 OPCODES.put("IF_ICMPLT", new Opcode(IF_ICMPLT, OpcodeGroup.INSN_JUMP)); 277 OPCODES.put("IF_ICMPGE", new Opcode(IF_ICMPGE, OpcodeGroup.INSN_JUMP)); 278 OPCODES.put("IF_ICMPGT", new Opcode(IF_ICMPGT, OpcodeGroup.INSN_JUMP)); 279 OPCODES.put("IF_ICMPLE", new Opcode(IF_ICMPLE, OpcodeGroup.INSN_JUMP)); 280 OPCODES.put("IF_ACMPEQ", new Opcode(IF_ACMPEQ, OpcodeGroup.INSN_JUMP)); 281 OPCODES.put("IF_ACMPNE", new Opcode(IF_ACMPNE, OpcodeGroup.INSN_JUMP)); 282 OPCODES.put("GOTO", new Opcode(GOTO, OpcodeGroup.INSN_JUMP)); 283 OPCODES.put("JSR", new Opcode(JSR, OpcodeGroup.INSN_JUMP)); 284 OPCODES.put("RET", new Opcode(RET, OpcodeGroup.INSN_VAR)); 285 OPCODES.put("IRETURN", new Opcode(IRETURN, OpcodeGroup.INSN)); 290 OPCODES.put("LRETURN", new Opcode(LRETURN, OpcodeGroup.INSN)); 291 OPCODES.put("FRETURN", new Opcode(FRETURN, OpcodeGroup.INSN)); 292 OPCODES.put("DRETURN", new Opcode(DRETURN, OpcodeGroup.INSN)); 293 OPCODES.put("ARETURN", new Opcode(ARETURN, OpcodeGroup.INSN)); 294 OPCODES.put("RETURN", new Opcode(RETURN, OpcodeGroup.INSN)); 295 OPCODES.put("GETSTATIC", new Opcode(GETSTATIC, OpcodeGroup.INSN_FIELD)); 296 OPCODES.put("PUTSTATIC", new Opcode(PUTSTATIC, OpcodeGroup.INSN_FIELD)); 297 OPCODES.put("GETFIELD", new Opcode(GETFIELD, OpcodeGroup.INSN_FIELD)); 298 OPCODES.put("PUTFIELD", new Opcode(PUTFIELD, OpcodeGroup.INSN_FIELD)); 299 OPCODES.put("INVOKEVIRTUAL", new Opcode(INVOKEVIRTUAL, 300 OpcodeGroup.INSN_METHOD)); 301 OPCODES.put("INVOKESPECIAL", new Opcode(INVOKESPECIAL, 302 OpcodeGroup.INSN_METHOD)); 303 OPCODES.put("INVOKESTATIC", new Opcode(INVOKESTATIC, 304 OpcodeGroup.INSN_METHOD)); 305 OPCODES.put("INVOKEINTERFACE", new Opcode(INVOKEINTERFACE, 306 OpcodeGroup.INSN_METHOD)); 307 OPCODES.put("NEW", new Opcode(NEW, OpcodeGroup.INSN_TYPE)); 308 OPCODES.put("NEWARRAY", new Opcode(NEWARRAY, OpcodeGroup.INSN_INT)); 309 OPCODES.put("ANEWARRAY", new Opcode(ANEWARRAY, OpcodeGroup.INSN_TYPE)); 310 OPCODES.put("ARRAYLENGTH", new Opcode(ARRAYLENGTH, OpcodeGroup.INSN)); 311 OPCODES.put("ATHROW", new Opcode(ATHROW, OpcodeGroup.INSN)); 312 OPCODES.put("CHECKCAST", new Opcode(CHECKCAST, OpcodeGroup.INSN_TYPE)); 313 OPCODES.put("INSTANCEOF", new Opcode(INSTANCEOF, OpcodeGroup.INSN_TYPE)); 314 OPCODES.put("MONITORENTER", new Opcode(MONITORENTER, OpcodeGroup.INSN)); 315 OPCODES.put("MONITOREXIT", new Opcode(MONITOREXIT, OpcodeGroup.INSN)); 316 OPCODES.put("MULTIANEWARRAY", new Opcode(MULTIANEWARRAY, 317 OpcodeGroup.INSN_MULTIANEWARRAY)); 318 OPCODES.put("IFNULL", new Opcode(IFNULL, OpcodeGroup.INSN_JUMP)); 319 OPCODES.put("IFNONNULL", new Opcode(IFNONNULL, OpcodeGroup.INSN_JUMP)); 320 } 321 322 330 public ASMContentHandler(OutputStream os, boolean computeMax) { 331 this.os = os; 332 this.computeMax = computeMax; 333 } 334 335 342 public byte[] toByteArray() { 343 return cw == null ? null : cw.toByteArray(); 344 } 345 346 359 public final void startElement( 360 String ns, 361 String localName, 362 String qName, 363 Attributes list) throws SAXException 364 { 365 String name = localName; 368 if (name == null || name.length() < 1) { 369 name = qName; 370 } 371 372 StringBuffer sb = new StringBuffer (match); 374 if (match.length() > 0) { 375 sb.append('/'); 376 } 377 sb.append(name); 378 match = sb.toString(); 379 380 Rule r = (Rule) RULES.match(match); 382 if (r != null) 383 r.begin(name, list); 384 } 385 386 398 public final void endElement(String ns, String localName, String qName) 399 throws SAXException 400 { 401 String name = localName; 404 if (name == null || name.length() < 1) { 405 name = qName; 406 } 407 408 Rule r = (Rule) RULES.match(match); 410 if (r != null) 411 r.end(name); 412 413 int slash = match.lastIndexOf('/'); 415 if (slash >= 0) { 416 match = match.substring(0, slash); 417 } else { 418 match = ""; 419 } 420 } 421 422 428 public final void endDocument() throws SAXException { 429 try { 430 os.write(cw.toByteArray()); 431 } catch (IOException ex) { 432 throw new SAXException (ex.toString(), ex); 433 } 434 } 435 436 440 final Object peek() { 441 return stack.size() == 0 ? null : stack.get(stack.size() - 1); 442 } 443 444 452 final Object peek(int n) { 453 return stack.size() < (n + 1) ? null : stack.get(n); 454 } 455 456 460 final Object pop() { 461 return stack.size() == 0 ? null : stack.remove(stack.size() - 1); 462 } 463 464 469 final void push(Object object) { 470 stack.add(object); 471 } 472 473 private static final class RuleSet { 474 private Map rules = new HashMap (); 475 476 private List lpatterns = new ArrayList (); 477 478 private List rpatterns = new ArrayList (); 479 480 public void add(String path, Object rule) { 481 String pattern = path; 482 if (path.startsWith("*/")) { 483 pattern = path.substring(1); 484 lpatterns.add(pattern); 485 } else if (path.endsWith("/*")) { 486 pattern = path.substring(0, path.length() - 1); 487 rpatterns.add(pattern); 488 } 489 rules.put(pattern, rule); 490 } 491 492 public Object match(String path) { 493 if (rules.containsKey(path)) { 494 return rules.get(path); 495 } 496 497 int n = path.lastIndexOf('/'); 498 for (Iterator it = lpatterns.iterator(); it.hasNext();) { 499 String pattern = (String ) it.next(); 500 if (path.substring(n).endsWith(pattern)) { 501 return rules.get(pattern); 502 } 503 } 504 505 for (Iterator it = rpatterns.iterator(); it.hasNext();) { 506 String pattern = (String ) it.next(); 507 if (path.startsWith(pattern)) { 508 return rules.get(pattern); 509 } 510 } 511 512 return null; 513 } 514 515 } 516 517 520 protected abstract class Rule { 521 522 public void begin(String name, Attributes attrs) { 523 } 524 525 public void end(String name) { 526 } 527 528 protected final Object getValue(String desc, String val) { 529 Object value = null; 530 if (val != null) { 531 if (desc.equals("Ljava/lang/String;")) { 532 value = decode(val); 533 } else if ("Ljava/lang/Integer;".equals(desc) 534 || "I".equals(desc) || "S".equals(desc) 535 || "B".equals(desc) || "C".equals(desc) 536 || desc.equals("Z")) 537 { 538 value = new Integer (val); 539 540 } else if ("Ljava/lang/Short;".equals(desc)) { 541 value = new Short (val); 542 543 } else if ("Ljava/lang/Byte;".equals(desc)) { 544 value = new Byte (val); 545 546 } else if ("Ljava/lang/Character;".equals(desc)) { 547 value = new Character (decode(val).charAt(0)); 548 549 } else if ("Ljava/lang/Boolean;".equals(desc)) { 550 value = Boolean.valueOf(val); 551 552 569 } else if ("Ljava/lang/Long;".equals(desc) || desc.equals("J")) 570 { 571 value = new Long (val); 572 } else if ("Ljava/lang/Float;".equals(desc) || desc.equals("F")) 573 { 574 value = new Float (val); 575 } else if ("Ljava/lang/Double;".equals(desc) 576 || desc.equals("D")) 577 { 578 value = new Double (val); 579 } else if (Type.getDescriptor(Type.class).equals(desc)) { 580 value = Type.getType(val); 581 582 599 } else { 600 throw new RuntimeException ("Invalid value:" + val 601 + " desc:" + desc + " ctx:" + this); 602 } 603 } 604 return value; 605 } 606 607 private final String decode(String val) { 608 StringBuffer sb = new StringBuffer (val.length()); 609 try { 610 int n = 0; 611 while (n < val.length()) { 612 char c = val.charAt(n); 613 if (c == '\\') { 614 n++; 615 c = val.charAt(n); 616 if (c == '\\') { 617 sb.append('\\'); 618 } else { 619 n++; sb.append((char) Integer.parseInt(val.substring(n, 621 n + 4), 16)); 622 n += 3; 623 } 624 } else { 625 sb.append(c); 626 } 627 n++; 628 } 629 630 } catch (RuntimeException ex) { 631 System.err.println(val + "\n" + ex.toString()); 632 ex.printStackTrace(); 633 throw ex; 634 } 635 return sb.toString(); 636 } 637 638 protected final Label getLabel(Object label) { 639 Label lbl = (Label) labels.get(label); 640 if (lbl == null) { 641 lbl = new Label(); 642 labels.put(label, lbl); 643 } 644 return lbl; 645 } 646 647 protected final MethodVisitor getCodeVisitor() { 649 return (MethodVisitor) peek(); 650 } 651 652 protected final int getAccess(String s) { 653 int access = 0; 654 if (s.indexOf("public") != -1) 655 access |= Opcodes.ACC_PUBLIC; 656 if (s.indexOf("private") != -1) 657 access |= Opcodes.ACC_PRIVATE; 658 if (s.indexOf("protected") != -1) 659 access |= Opcodes.ACC_PROTECTED; 660 if (s.indexOf("static") != -1) 661 access |= Opcodes.ACC_STATIC; 662 if (s.indexOf("final") != -1) 663 access |= Opcodes.ACC_FINAL; 664 if (s.indexOf("super") != -1) 665 access |= Opcodes.ACC_SUPER; 666 if (s.indexOf("synchronized") != -1) 667 access |= Opcodes.ACC_SYNCHRONIZED; 668 if (s.indexOf("volatile") != -1) 669 access |= Opcodes.ACC_VOLATILE; 670 if (s.indexOf("bridge") != -1) 671 access |= Opcodes.ACC_BRIDGE; 672 if (s.indexOf("varargs") != -1) 673 access |= Opcodes.ACC_VARARGS; 674 if (s.indexOf("transient") != -1) 675 access |= Opcodes.ACC_TRANSIENT; 676 if (s.indexOf("native") != -1) 677 access |= Opcodes.ACC_NATIVE; 678 if (s.indexOf("interface") != -1) 679 access |= Opcodes.ACC_INTERFACE; 680 if (s.indexOf("abstract") != -1) 681 access |= Opcodes.ACC_ABSTRACT; 682 if (s.indexOf("strict") != -1) 683 access |= Opcodes.ACC_STRICT; 684 if (s.indexOf("synthetic") != -1) 685 access |= Opcodes.ACC_SYNTHETIC; 686 if (s.indexOf("annotation") != -1) 687 access |= Opcodes.ACC_ANNOTATION; 688 if (s.indexOf("enum") != -1) 689 access |= Opcodes.ACC_ENUM; 690 if (s.indexOf("deprecated") != -1) 691 access |= Opcodes.ACC_DEPRECATED; 692 return access; 693 } 694 695 } 696 697 700 private final class ClassRule extends Rule { 701 702 public final void begin(String name, Attributes attrs) { 703 int major = Integer.parseInt(attrs.getValue("major")); 704 int minor = Integer.parseInt(attrs.getValue("minor")); 705 cw = new ClassWriter(computeMax); 706 Map vals = new HashMap (); 707 vals.put("version", new Integer (minor << 16 | major)); 708 vals.put("access", attrs.getValue("access")); 709 vals.put("name", attrs.getValue("name")); 710 vals.put("parent", attrs.getValue("parent")); 711 vals.put("source", attrs.getValue("source")); 712 vals.put("signature", attrs.getValue("signature")); 713 vals.put("interfaces", new ArrayList ()); 714 push(vals); 715 } 717 718 } 719 720 private final class SourceRule extends Rule { 721 722 public void begin(String name, Attributes attrs) { 723 String file = attrs.getValue("file"); 724 String debug = attrs.getValue("debug"); 725 cw.visitSource(file, debug); 726 } 727 728 } 729 730 733 private final class InterfaceRule extends Rule { 734 735 public final void begin(String name, Attributes attrs) { 736 ((List ) ((Map ) peek()).get("interfaces")).add(attrs.getValue("name")); 737 } 738 739 } 740 741 744 private final class InterfacesRule extends Rule { 745 746 public final void end(String element) { 747 Map vals = (Map ) pop(); 748 int version = ((Integer ) vals.get("version")).intValue(); 749 int access = getAccess((String ) vals.get("access")); 750 String name = (String ) vals.get("name"); 751 String signature = (String ) vals.get("signature"); 752 String parent = (String ) vals.get("parent"); 753 String [] interfaces = (String []) ((List ) vals.get("interfaces")).toArray(new String [0]); 754 cw.visit(version, access, name, signature, parent, interfaces); 755 push(cw); 756 } 757 758 } 759 760 763 private final class OuterClassRule extends Rule { 764 765 public final void begin(String element, Attributes attrs) { 766 String owner = attrs.getValue("owner"); 767 String name = attrs.getValue("name"); 768 String desc = attrs.getValue("desc"); 769 cw.visitOuterClass(owner, name, desc); 770 } 771 772 } 773 774 777 private final class InnerClassRule extends Rule { 778 779 public final void begin(String element, Attributes attrs) { 780 int access = getAccess(attrs.getValue("access")); 781 String name = attrs.getValue("name"); 782 String outerName = attrs.getValue("outerName"); 783 String innerName = attrs.getValue("innerName"); 784 cw.visitInnerClass(name, outerName, innerName, access); 785 } 786 787 } 788 789 792 private final class FieldRule extends Rule { 793 794 public final void begin(String element, Attributes attrs) { 795 int access = getAccess(attrs.getValue("access")); 796 String name = attrs.getValue("name"); 797 String signature = attrs.getValue("signature"); 798 String desc = attrs.getValue("desc"); 799 Object value = getValue(desc, attrs.getValue("value")); 800 push(cw.visitField(access, name, desc, signature, value)); 801 } 802 803 public void end(String name) { 804 ((FieldVisitor) pop()).visitEnd(); 805 } 806 807 } 808 809 812 private final class MethodRule extends Rule { 813 814 public final void begin(String name, Attributes attrs) { 815 labels = new HashMap (); 816 Map vals = new HashMap (); 817 vals.put("access", attrs.getValue("access")); 818 vals.put("name", attrs.getValue("name")); 819 vals.put("desc", attrs.getValue("desc")); 820 vals.put("signature", attrs.getValue("signature")); 821 vals.put("exceptions", new ArrayList ()); 822 push(vals); 823 } 825 826 public final void end(String name) { 827 ((MethodVisitor) pop()).visitEnd(); 828 labels = null; 829 } 830 831 } 832 833 836 private final class ExceptionRule extends Rule { 837 838 public final void begin(String name, Attributes attrs) { 839 ((List ) ((Map ) peek()).get("exceptions")).add(attrs.getValue("name")); 840 } 841 842 } 843 844 847 private final class ExceptionsRule extends Rule { 848 849 public final void end(String element) { 850 Map vals = (Map ) pop(); 851 int access = getAccess((String ) vals.get("access")); 852 String name = (String ) vals.get("name"); 853 String desc = (String ) vals.get("desc"); 854 String signature = (String ) vals.get("signature"); 855 String [] exceptions = (String []) ((List ) vals.get("exceptions")).toArray(new String [0]); 856 857 push(cw.visitMethod(access, name, desc, signature, exceptions)); 858 } 859 860 } 861 862 865 private class TableSwitchRule extends Rule { 866 867 public final void begin(String name, Attributes attrs) { 868 Map vals = new HashMap (); 869 vals.put("min", attrs.getValue("min")); 870 vals.put("max", attrs.getValue("max")); 871 vals.put("dflt", attrs.getValue("dflt")); 872 vals.put("labels", new ArrayList ()); 873 push(vals); 874 } 875 876 public final void end(String name) { 877 Map vals = (Map ) pop(); 878 int min = Integer.parseInt((String ) vals.get("min")); 879 int max = Integer.parseInt((String ) vals.get("max")); 880 Label dflt = getLabel(vals.get("dflt")); 881 Label[] lbls = (Label[]) ((List ) vals.get("labels")).toArray(new Label[0]); 882 getCodeVisitor().visitTableSwitchInsn(min, max, dflt, lbls); 883 } 884 885 } 886 887 890 private final class TableSwitchLabelRule extends Rule { 891 892 public final void begin(String name, Attributes attrs) { 893 ((List ) ((Map ) peek()).get("labels")).add(getLabel(attrs.getValue("name"))); 894 } 895 896 } 897 898 901 private final class LookupSwitchRule extends Rule { 902 903 public final void begin(String name, Attributes attrs) { 904 Map vals = new HashMap (); 905 vals.put("dflt", attrs.getValue("dflt")); 906 vals.put("labels", new ArrayList ()); 907 vals.put("keys", new ArrayList ()); 908 push(vals); 909 } 910 911 public final void end(String name) { 912 Map vals = (Map ) pop(); 913 Label dflt = getLabel(vals.get("dflt")); 914 List keyList = (List ) vals.get("keys"); 915 Label[] lbls = (Label[]) ((List ) vals.get("labels")).toArray(new Label[0]); 916 int[] keys = new int[keyList.size()]; 917 for (int i = 0; i < keys.length; i++) { 918 keys[i] = Integer.parseInt((String ) keyList.get(i)); 919 } 920 getCodeVisitor().visitLookupSwitchInsn(dflt, keys, lbls); 921 } 922 923 } 924 925 928 private final class LookupSwitchLabelRule extends Rule { 929 930 public final void begin(String name, Attributes attrs) { 931 Map vals = (Map ) peek(); 932 ((List ) vals.get("labels")).add(getLabel(attrs.getValue("name"))); 933 ((List ) vals.get("keys")).add(attrs.getValue("key")); 934 } 935 936 } 937 938 941 private final class LabelRule extends Rule { 942 943 public final void begin(String name, Attributes attrs) { 944 getCodeVisitor().visitLabel(getLabel(attrs.getValue("name"))); 945 } 946 947 } 948 949 952 private final class TryCatchRule extends Rule { 953 954 public final void begin(String name, Attributes attrs) { 955 Label start = getLabel(attrs.getValue("start")); 956 Label end = getLabel(attrs.getValue("end")); 957 Label handler = getLabel(attrs.getValue("handler")); 958 String type = attrs.getValue("type"); 959 getCodeVisitor().visitTryCatchBlock(start, end, handler, type); 960 } 961 962 } 963 964 967 private final class LineNumberRule extends Rule { 968 969 public final void begin(String name, Attributes attrs) { 970 int line = Integer.parseInt(attrs.getValue("line")); 971 Label start = getLabel(attrs.getValue("start")); 972 getCodeVisitor().visitLineNumber(line, start); 973 } 974 975 } 976 977 980 private final class LocalVarRule extends Rule { 981 982 public final void begin(String element, Attributes attrs) { 983 String name = attrs.getValue("name"); 984 String desc = attrs.getValue("desc"); 985 String signature = attrs.getValue("signature"); 986 Label start = getLabel(attrs.getValue("start")); 987 Label end = getLabel(attrs.getValue("end")); 988 int var = Integer.parseInt(attrs.getValue("var")); 989 getCodeVisitor().visitLocalVariable(name, 990 desc, 991 signature, 992 start, 993 end, 994 var); 995 } 996 997 } 998 999 1002 private final class OpcodesRule extends Rule { 1003 1004 1008 public final void begin(String element, Attributes attrs) { 1009 Opcode o = ((Opcode) OPCODES.get(element)); 1010 if (o == null) 1011 return; 1012 1013 switch (o.type) { 1014 case OpcodeGroup.INSN: 1015 getCodeVisitor().visitInsn(o.opcode); 1016 break; 1017 1018 case OpcodeGroup.INSN_FIELD: 1019 getCodeVisitor().visitFieldInsn(o.opcode, 1020 attrs.getValue("owner"), 1021 attrs.getValue("name"), 1022 attrs.getValue("desc")); 1023 break; 1024 1025 case OpcodeGroup.INSN_INT: 1026 getCodeVisitor().visitIntInsn(o.opcode, 1027 Integer.parseInt(attrs.getValue("value"))); 1028 break; 1029 1030 case OpcodeGroup.INSN_JUMP: 1031 getCodeVisitor().visitJumpInsn(o.opcode, 1032 getLabel(attrs.getValue("label"))); 1033 break; 1034 1035 case OpcodeGroup.INSN_METHOD: 1036 getCodeVisitor().visitMethodInsn(o.opcode, 1037 attrs.getValue("owner"), 1038 attrs.getValue("name"), 1039 attrs.getValue("desc")); 1040 break; 1041 1042 case OpcodeGroup.INSN_TYPE: 1043 getCodeVisitor().visitTypeInsn(o.opcode, 1044 attrs.getValue("desc")); 1045 break; 1046 1047 case OpcodeGroup.INSN_VAR: 1048 getCodeVisitor().visitVarInsn(o.opcode, 1049 Integer.parseInt(attrs.getValue("var"))); 1050 break; 1051 1052 case OpcodeGroup.INSN_IINC: 1053 getCodeVisitor().visitIincInsn(Integer.parseInt(attrs.getValue("var")), 1054 Integer.parseInt(attrs.getValue("inc"))); 1055 break; 1056 1057 case OpcodeGroup.INSN_LDC: 1058 getCodeVisitor().visitLdcInsn(getValue(attrs.getValue("desc"), 1059 attrs.getValue("cst"))); 1060 break; 1061 1062 case OpcodeGroup.INSN_MULTIANEWARRAY: 1063 getCodeVisitor().visitMultiANewArrayInsn(attrs.getValue("desc"), 1064 Integer.parseInt(attrs.getValue("dims"))); 1065 break; 1066 1067 default: 1068 throw new RuntimeException ("Invalid element: " + element 1069 + " at " + match); 1070 1071 } 1072 } 1073 } 1074 1075 1078 private final class MaxRule extends Rule { 1079 1080 public final void begin(String element, Attributes attrs) { 1081 int maxStack = Integer.parseInt(attrs.getValue("maxStack")); 1082 int maxLocals = Integer.parseInt(attrs.getValue("maxLocals")); 1083 getCodeVisitor().visitMaxs(maxStack, maxLocals); 1084 } 1085 1086 } 1087 1088 private final class AnnotationRule extends Rule { 1089 1090 public void begin(String name, Attributes attrs) { 1091 String desc = attrs.getValue("desc"); 1092 boolean visible = Boolean.valueOf(attrs.getValue("visible")) 1093 .booleanValue(); 1094 1095 Object v = peek(); 1096 if (v instanceof ClassVisitor) { 1097 push(((ClassVisitor) v).visitAnnotation(desc, visible)); 1098 } else if (v instanceof FieldVisitor) { 1099 push(((FieldVisitor) v).visitAnnotation(desc, visible)); 1100 } else if (v instanceof MethodVisitor) { 1101 push(((MethodVisitor) v).visitAnnotation(desc, visible)); 1102 } 1103 } 1104 1105 public void end(String name) { 1106 ((AnnotationVisitor) pop()).visitEnd(); 1107 } 1108 1109 } 1110 1111 private final class AnnotationParameterRule extends Rule { 1112 1113 public void begin(String name, Attributes attrs) { 1114 int parameter = Integer.parseInt(attrs.getValue("parameter")); 1115 String desc = attrs.getValue("desc"); 1116 boolean visible = Boolean.valueOf(attrs.getValue("visible")) 1117 .booleanValue(); 1118 1119 push(((MethodVisitor) peek()).visitParameterAnnotation(parameter, 1120 desc, 1121 visible)); 1122 } 1123 1124 public void end(String name) { 1125 ((AnnotationVisitor) pop()).visitEnd(); 1126 } 1127 1128 } 1129 1130 private final class AnnotationValueRule extends Rule { 1131 1132 public void begin(String nm, Attributes attrs) { 1133 String name = attrs.getValue("name"); 1134 String desc = attrs.getValue("desc"); 1135 String value = attrs.getValue("value"); 1136 ((AnnotationVisitor) peek()).visit(name, getValue(desc, value)); 1137 } 1138 1139 } 1140 1141 private final class AnnotationValueEnumRule extends Rule { 1142 1143 public void begin(String nm, Attributes attrs) { 1144 String name = attrs.getValue("name"); 1145 String desc = attrs.getValue("desc"); 1146 String value = attrs.getValue("value"); 1147 ((AnnotationVisitor) peek()).visitEnum(name, desc, value); 1148 } 1149 1150 } 1151 1152 private final class AnnotationValueAnnotationRule extends Rule { 1153 1154 public void begin(String nm, Attributes attrs) { 1155 String name = attrs.getValue("name"); 1156 String desc = attrs.getValue("desc"); 1157 push(((AnnotationVisitor) peek()).visitAnnotation(name, desc)); 1158 } 1159 1160 public void end(String name) { 1161 ((AnnotationVisitor) pop()).visitEnd(); 1162 } 1163 1164 } 1165 1166 private final class AnnotationValueArrayRule extends Rule { 1167 1168 public void begin(String nm, Attributes attrs) { 1169 String name = attrs.getValue("name"); 1170 push(((AnnotationVisitor) peek()).visitArray(name)); 1171 } 1172 1173 public void end(String name) { 1174 ((AnnotationVisitor) pop()).visitEnd(); 1175 } 1176 1177 } 1178 1179 private final class AnnotationDefaultRule extends Rule { 1180 1181 public void begin(String nm, Attributes attrs) { 1182 push(((MethodVisitor) peek()).visitAnnotationDefault()); 1183 } 1184 1185 public void end(String name) { 1186 ((AnnotationVisitor) pop()).visitEnd(); 1187 } 1188 1189 } 1190 1191 1194 private final static class Opcode { 1195 public int opcode; 1196 1197 public int type; 1198 1199 public Opcode(int opcode, int type) { 1200 this.opcode = opcode; 1201 this.type = type; 1202 } 1203 1204 } 1205 1206} 1207 | Popular Tags |