5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 package com.sun.classanalyzer; 24 25 import java.io.File; 26 import java.io.IOException; 27 import java.util.*; 28 29 /** 30 * A simple tool to print out the static dependencies for a given set of JAR, 31 * class files, or combinations of. The tools supports an -ignore option to 32 * ignore references to classes listed in the file (including .classlists 33 * created by the ClassAnalyzer tool). 34 */ 35 36 public class ShowDeps { 37 38 static void usage() { 39 System.out.println("java ShowDeps [-ignore <classlist>] file..."); 40 System.out.println(" where <file> is a class or JAR file, or a directory"); 41 System.out.println(); 42 System.out.println("Example usages:"); 43 System.out.println(" java ShowDeps Foo.jar"); 44 System.out.println(" java ShowDeps -ignore base.classlist Foo.jar"); 45 System.out.println(" java ShowDeps -ignore base.classlist -ignore " + 46 "jaxp-parsers.classlist <dir>"); 47 System.exit(-1); 48 } 49 50 public static void main(String[] args) throws IOException { 51 // process -ignore options 52 int argi = 0; 53 Set<String> ignore = new HashSet<String>(); 54 while (argi < args.length && args[argi].equals("-ignore")) { 55 argi++; 56 Scanner s = new Scanner(new File(args[argi++])); 57 try { 58 while (s.hasNextLine()) { 59 String line = s.nextLine(); 60 if (!line.endsWith(".class")) 61 continue; 62 int len = line.length(); 63 // convert to class names 64 String clazz = line.replace('\\', '.').replace('/', '.') 65 .substring(0, len-6); 66 ignore.add(clazz); 67 } 68 } finally { 69 s.close(); 70 } 71 } 72 73 if (argi >= args.length) 74 usage(); 75 76 // parse all classes 77 while (argi < args.length) 78 ClassPath.setClassPath(args[argi++]); 79 ClassPath.parseAllClassFiles(); 80 81 // find the classes that don't exist 82 Set<Klass> unresolved = new TreeSet<Klass>(); 83 for (Klass k : Klass.getAllClasses()) { 84 if (k.getFileSize() == 0) 85 unresolved.add(k); 86 } 87 88 // print references to classes that don't exist 89 for (Klass k: Klass.getAllClasses()) { 90 for (Klass other : k.getReferencedClasses()) { 91 if (unresolved.contains(other)) { 92 String name = other.toString(); 93 if (!ignore.contains(name)) { 94 System.out.format("%s -> %s\n", k, other); 95 } 96 } 97 } 98 } 99 } 100 } | 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 package com.sun.classanalyzer; 24 25 import java.io.*; 26 import java.util.*; 27 28 /** 29 * A simple tool to print out the static dependencies for a given set of JAR, 30 * class files, or combinations of. The tools supports an -ignore option to 31 * ignore references to classes listed in the file (including .classlists 32 * created by the ClassAnalyzer tool). 33 */ 34 public class ShowDeps { 35 36 private final ClassPaths cpaths; 37 private final Set<String> classes = new TreeSet<String>(); 38 39 public ShowDeps(ClassPaths cpaths) { 40 this.cpaths = cpaths; 41 } 42 43 public void run() throws IOException { 44 cpaths.parse(); 45 46 // find the classes that don't exist 47 Set<Klass> unresolved = new TreeSet<Klass>(); 48 for (Klass k : Klass.getAllClasses()) { 49 if (k.getFileSize() == 0) { 50 unresolved.add(k); 51 } 52 } 53 54 // print references to classes that don't exist 55 for (Klass k : Klass.getAllClasses()) { 56 for (Klass other : k.getReferencedClasses()) { 57 if (unresolved.contains(other)) { 58 String name = other.toString(); 59 if (!ignore.contains(name)) { 60 System.out.format("%s -> %s\n", k, other); 61 } 62 } 63 } 64 } 65 } 66 67 static void usage() { 68 System.out.println("java ShowDeps [-ignore <classlist>] file..."); 69 System.out.println(" where <file> is a class or JAR file, or a directory"); 70 System.out.println(); 71 System.out.println("Example usages:"); 72 System.out.println(" java ShowDeps Foo.jar"); 73 System.out.println(" java ShowDeps -ignore base.classlist Foo.jar"); 74 System.out.println(" java ShowDeps -ignore base.classlist -ignore " + 75 "jaxp-parsers.classlist <dir>"); 76 System.exit(-1); 77 } 78 private static Set<String> ignore = new HashSet<String>(); 79 80 public static void main(String[] args) throws IOException { 81 // process -ignore options 82 int argi = 0; 83 while (argi < args.length && args[argi].equals("-ignore")) { 84 argi++; 85 Scanner s = new Scanner(new File(args[argi++])); 86 try { 87 while (s.hasNextLine()) { 88 String line = s.nextLine(); 89 if (!line.endsWith(".class")) { 90 continue; 91 } 92 int len = line.length(); 93 // convert to class names 94 String clazz = line.replace('\\', '.').replace('/', '.') 95 .substring(0, len - 6); 96 ignore.add(clazz); 97 } 98 } finally { 99 s.close(); 100 } 101 } 102 103 if (argi >= args.length) { 104 usage(); 105 } 106 107 // parse all classes 108 ClassPaths cpaths = new ClassPaths(Arrays.copyOfRange(args, argi, args.length)); 109 ShowDeps instance = new ShowDeps(cpaths); 110 instance.run(); 111 } 112 } |