1 16 17 package org.cojen.classfile; 18 19 import java.io.BufferedInputStream ; 20 import java.io.File ; 21 import java.io.FileInputStream ; 22 import java.io.FileNotFoundException ; 23 import java.io.InputStream ; 24 import java.io.IOException ; 25 import java.io.PrintWriter ; 26 import java.util.ArrayList ; 27 import java.util.Arrays ; 28 import java.util.Comparator ; 29 import java.util.Date ; 30 import java.util.HashMap ; 31 import java.util.List ; 32 import java.util.Map ; 33 34 import org.cojen.classfile.attribute.CodeAttr; 35 import org.cojen.classfile.attribute.SignatureAttr; 36 37 53 public class DisassemblyTool { 54 63 public static void main(String [] args) throws Exception { 64 if (args.length == 0) { 65 System.out.println("DisassemblyTool [-f <format style>] <file or class name>"); 66 System.out.println(); 67 System.out.println("The format style may be \"assembly\" (the default) or \"builder\""); 68 return; 69 } 70 71 String style; 72 String name; 73 74 if ("-f".equals(args[0])) { 75 style = args[1]; 76 name = args[2]; 77 } else { 78 style = "assembly"; 79 name = args[0]; 80 } 81 82 ClassFileDataLoader loader; 83 InputStream in; 84 85 try { 86 final File file = new File (name); 87 in = new FileInputStream (file); 88 loader = new ClassFileDataLoader() { 89 public InputStream getClassData(String name) 90 throws IOException 91 { 92 name = name.substring(name.lastIndexOf('.') + 1); 93 File f = new File (file.getParentFile(), name + ".class"); 94 95 if (f.exists()) { 96 return new FileInputStream (f); 97 } 98 99 return null; 100 } 101 }; 102 } catch (FileNotFoundException e) { 103 if (name.endsWith(".class")) { 104 System.err.println(e); 105 return; 106 } 107 108 loader = new ResourceClassFileDataLoader(); 109 in = loader.getClassData(name); 110 111 if (in == null) { 112 System.err.println(e); 113 return; 114 } 115 } 116 117 in = new BufferedInputStream (in); 118 ClassFile cf = ClassFile.readFrom(in, loader, null); 119 120 PrintWriter out = new PrintWriter (System.out); 121 122 Printer p; 123 if (style == null || style.equals("assembly")) { 124 p = new AssemblyStylePrinter(); 125 } else if (style.equals("builder")) { 126 p = new BuilderStylePrinter(); 127 } else { 128 System.err.println("Unknown format style: " + style); 129 return; 130 } 131 132 p.disassemble(cf, out); 133 134 out.flush(); 135 } 136 137 public static interface Printer { 138 void disassemble(ClassFile cf, PrintWriter out); 139 } 140 } 141 | Popular Tags |