1 30 package org.objectweb.asm.commons; 31 32 import java.io.FileInputStream ; 33 import java.io.PrintWriter ; 34 35 import org.objectweb.asm.ClassReader; 36 import org.objectweb.asm.MethodVisitor; 37 import org.objectweb.asm.util.ASMifierClassVisitor; 38 39 public class GASMifierClassVisitor extends ASMifierClassVisitor { 40 41 51 public static void main(final String [] args) throws Exception { 52 int i = 0; 53 boolean skipDebug = true; 54 55 boolean ok = true; 56 if (args.length < 1 || args.length > 2) { 57 ok = false; 58 } 59 if (ok && args[0].equals("-debug")) { 60 i = 1; 61 skipDebug = false; 62 if (args.length != 2) { 63 ok = false; 64 } 65 } 66 if (!ok) { 67 System.err.println("Prints the ASM code to generate the given class."); 68 System.err.println("Usage: GASMifierClassVisitor [-debug] " 69 + "<fully qualified class name or class file name>"); 70 System.exit(-1); 71 } 72 ClassReader cr; 73 if (args[i].endsWith(".class")) { 74 cr = new ClassReader(new FileInputStream (args[i])); 75 } else { 76 cr = new ClassReader(args[i]); 77 } 78 cr.accept(new GASMifierClassVisitor(new PrintWriter (System.out)), 79 getDefaultAttributes(), 80 skipDebug); 81 } 82 83 public GASMifierClassVisitor(PrintWriter pw) { 84 super(pw); 85 } 86 87 public void visit( 88 final int version, 89 final int access, 90 final String name, 91 final String signature, 92 final String superName, 93 final String [] interfaces) 94 { 95 super.visit(version, access, name, signature, superName, interfaces); 96 int n; 97 if (name.lastIndexOf('/') != -1) { 98 n = 1; 99 } else { 100 n = 0; 101 } 102 text.set(n + 5, "ClassWriter cw = new ClassWriter(true);\n"); 103 text.set(n + 7, "GeneratorAdapter mg;\n"); 104 text.add(n + 1, "import org.objectweb.asm.commons.*;\n"); 105 } 106 107 public MethodVisitor visitMethod( 108 final int access, 109 final String name, 110 final String desc, 111 final String signature, 112 final String [] exceptions) 113 { 114 buf.setLength(0); 115 buf.append("{\n"); 116 buf.append("mg = new GeneratorAdapter("); 117 buf.append(access); 118 buf.append(", "); 119 buf.append(GASMifierMethodVisitor.getMethod(name, desc)); 120 buf.append(", "); 121 if (signature == null) { 122 buf.append("null"); 123 } else { 124 buf.append('"').append(signature).append('"'); 125 } 126 buf.append(", "); 127 if (exceptions != null && exceptions.length > 0) { 128 buf.append("new Type[] {"); 129 for (int i = 0; i < exceptions.length; ++i) { 130 buf.append(i == 0 ? " " : ", "); 131 buf.append(GASMifierMethodVisitor.getType(exceptions[i])); 132 } 133 buf.append(" }"); 134 } else { 135 buf.append("null"); 136 } 137 buf.append(", cw);\n"); 138 text.add(buf.toString()); 139 GASMifierMethodVisitor acv = new GASMifierMethodVisitor(access, desc); 140 text.add(acv.getText()); 141 text.add("}\n"); 142 return new LocalVariablesSorter(access, desc, acv); 143 } 144 } 145 | Popular Tags |