jdk/make/modules/tools/src/com/sun/classanalyzer/ShowDeps.java
changeset 8852 c228cf346138
parent 8851 e630c590eb10
parent 8717 f75a1efb1412
child 8853 6aa795396cc8
child 9067 c0b85430843d
equal deleted inserted replaced
8851:e630c590eb10 8852:c228cf346138
     1 /*
       
     2  * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     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 }