1 25 26 import org.netbeans.modules.classfile.*; 27 import java.io.*; 28 import java.util.*; 29 30 35 public class PrintClass { 36 String thisClass; 37 38 PrintClass(String spec) { 39 thisClass = spec; 40 } 41 42 void print(PrintStream out) throws IOException { 43 ClassName cn = ClassName.getClassName(thisClass.replace('.', '/')); 44 InputStream is = findClassStream(cn.getType()); 45 if (is == null) { 46 System.err.println("couldn't find class: " + 47 cn.getExternalName()); 48 return; 49 } 50 ClassFile cfile = new ClassFile(is); 51 out.println(cfile); 52 } 53 54 private InputStream findClassStream(String className) { 55 InputStream is = 56 ClassLoader.getSystemClassLoader().getResourceAsStream(className + ".class"); 57 return is; 58 } 59 60 64 public static void usage() { 65 System.err.println( 66 "usage: java PrintClass <class> [ <class> ...]"); 67 System.exit(1); 68 } 69 70 public static void main(String [] args) { 71 if (args.length == 0) 72 usage(); 73 74 for (int i = 0; i < args.length; i++) { 75 if (args[i].charAt(0) == '-') 76 usage(); 77 else { 78 try { 79 PrintClass pc = new PrintClass(args[i]); 80 pc.print(System.out); 81 } catch (IOException e) { 82 System.err.println("error accessing \"" + args[i] + 83 "\": " + e.toString()); 84 } 85 } 86 } 87 } 88 } 89 | Popular Tags |