1 19 20 package edu.umd.cs.findbugs.visitclass; 21 22 import java.io.IOException ; 23 import java.io.Serializable ; 24 import java.util.Comparator ; 25 import java.util.Enumeration ; 26 import java.util.Iterator ; 27 import java.util.TreeSet ; 28 import java.util.zip.ZipEntry ; 29 import java.util.zip.ZipFile ; 30 31 import org.apache.bcel.classfile.ClassParser; 32 import org.apache.bcel.classfile.Code; 33 import org.apache.bcel.classfile.JavaClass; 34 import org.apache.bcel.classfile.Method; 35 36 37 40 public class PrintClass { 41 42 45 static final class ZipEntryComparator implements Comparator <ZipEntry >, Serializable { 46 private static final long serialVersionUID = 1L; 47 public int compare(ZipEntry o1, ZipEntry o2) { 48 ZipEntry e1 = (ZipEntry ) o1; 49 String s1 = e1.getName(); 50 int pos1 = s1.lastIndexOf('/'); 51 String p1 = "-"; 52 if (pos1 >= 0) 53 p1 = s1.substring(0, pos1); 54 55 ZipEntry e2 = (ZipEntry ) o2; 56 String s2 = e2.getName(); 57 int pos2 = s2.lastIndexOf('/'); 58 String p2 = "-"; 59 if (pos2 >= 0) 60 p2 = s2.substring(0, pos2); 61 int r = p1.compareTo(p2); 62 if (r != 0) 63 return r; 64 return s1.compareTo(s2); 65 } 66 } 67 68 static boolean code = false, constants = false; 69 70 public static void main(String argv[]) throws IOException { 71 String [] file_name = new String [argv.length]; 72 int files = 0; 73 ClassParser parser = null; 74 String zip_file = null; 75 76 79 for (int i = 0; i < argv.length; i++) { 80 if (argv[i].charAt(0) == '-') { if (argv[i].equals("-constants")) 82 constants = true; 83 else if (argv[i].equals("-code")) 84 code = true; 85 else if (argv[i].equals("-zip")) 86 zip_file = argv[++i]; 87 } else if (argv[i].endsWith(".zip") || argv[i].endsWith(".jar")) 88 zip_file = argv[i]; 89 else { file_name[files++] = argv[i]; 91 } 92 } 93 94 if (!constants) code = true; 95 if (files == 0 && zip_file == null) { 96 System.err.println("list: No input files specified"); 97 } else { 98 99 if (files == 0 && zip_file != null) { 100 ZipFile z = new ZipFile (zip_file); 101 TreeSet <ZipEntry > zipEntries = new TreeSet <ZipEntry >( 102 new ZipEntryComparator()); 103 for (Enumeration <? extends ZipEntry > e = z 104 .entries(); e.hasMoreElements();) 105 zipEntries.add(e.nextElement()); 106 107 for (ZipEntry ze : zipEntries) { 108 String name = ze.getName(); 109 if (name.endsWith(".class")) 110 printClass(new ClassParser(z.getInputStream(ze), name)); 111 112 113 114 } 115 } else 116 for (int i = 0; i < files; i++) 117 if (file_name[i].endsWith(".class")) { 118 if (zip_file == null) 119 printClass(new ClassParser(file_name[i])); 120 else 121 printClass(new ClassParser(zip_file, file_name[i])); 122 123 } 124 } 125 126 } 127 128 private static void printClass(ClassParser parser) throws IOException { 129 JavaClass java_class; 130 java_class = parser.parse(); 131 132 if (constants || code) 133 System.out.println(java_class); if (constants) System.out.println(java_class.getConstantPool()); 136 137 if (code) printCode(java_class.getMethods()); 139 } 140 141 144 public static void printCode(Method[] methods) { 145 for (Method m : methods) { 146 System.out.println(m); 147 Code code = m.getCode(); 148 if (code != null) 149 System.out.println(code); 150 151 } 152 } 153 } | Popular Tags |