1 25 26 import org.netbeans.modules.classfile.*; 27 import java.io.*; 28 import java.util.*; 29 import java.util.zip.ZipFile ; 30 import java.util.zip.ZipEntry ; 31 32 38 public class ScanJar { 39 String jarName; 40 boolean includeCode; 41 boolean toString; 42 43 public static void main(String [] args) { 44 boolean includeCode = false; 45 boolean toString = false; 46 if (args.length == 0) 47 usage(); 48 for (int i = 0; i < args.length; i++) { 49 if (args[i].equals("-includeCode")) 50 includeCode = true; 51 else if (args[i].equals("-toString")) 52 toString = true; 53 else if (args[i].charAt(0) == '-') 54 usage(); 55 else { 56 try { 57 ScanJar sj = new ScanJar(args[i], includeCode, toString); 58 System.out.print("scanning " + args[i]); 59 if (includeCode || toString) { 60 System.out.print(": "); 61 if (includeCode) 62 System.out.print("includeCode "); 63 if (toString) 64 System.out.print("toString"); 65 } 66 System.out.println(); 67 ElapsedTimer timer = new ElapsedTimer(); 68 int n = sj.scan(); 69 System.out.println("scanned " + n + " files in " + 70 timer.toString()); 71 } catch (IOException e) { 72 System.err.println("error accessing \"" + args[i] + 73 "\": " + e.toString()); 74 } 75 } 76 } 77 } 78 79 ScanJar(String name, boolean incl, boolean tos) { 80 jarName = name; 81 includeCode = incl; 82 toString = tos; 83 } 84 85 89 public int scan() throws IOException { 90 int n = 0; 91 ZipFile zf = new ZipFile (jarName); 92 Enumeration files = zf.entries(); 93 while (files.hasMoreElements()) { 94 ZipEntry entry = (ZipEntry )files.nextElement(); 95 String name = entry.getName(); 96 if (name.endsWith(".class")) { 97 InputStream in = zf.getInputStream(entry); 98 ClassFile cf = new ClassFile(in, includeCode); 99 if (toString) 100 try { 101 cf.toString(); } catch (InvalidClassFileAttributeException ex) { 103 System.out.println("error accessing " + name); 104 throw ex; 105 } 106 in.close(); 107 n++; 108 } 109 } 110 zf.close(); 111 return n; 112 } 113 114 public static void usage() { 115 System.err.println("usage: java ScanJar [-includeCode] " + 116 "[-toString] <jar file> [ <jar file> ...]"); 117 System.exit(1); 118 } 119 } 120 121
| Popular Tags
|