langtools/src/share/classes/com/sun/tools/jdeps/JdepsTask.java
changeset 16290 b0b4f52de7ea
parent 15030 2d8dec41f029
child 16550 f20e2521f3df
--- a/langtools/src/share/classes/com/sun/tools/jdeps/JdepsTask.java	Wed Feb 13 23:05:17 2013 -0800
+++ b/langtools/src/share/classes/com/sun/tools/jdeps/JdepsTask.java	Thu Feb 14 09:43:00 2013 -0800
@@ -29,7 +29,6 @@
 import com.sun.tools.classfile.Dependencies;
 import com.sun.tools.classfile.Dependencies.ClassFileError;
 import com.sun.tools.classfile.Dependency;
-import com.sun.tools.classfile.Dependency.Location;
 import java.io.*;
 import java.text.MessageFormat;
 import java.util.*;
@@ -42,7 +41,7 @@
     class BadArgs extends Exception {
         static final long serialVersionUID = 8765093759964640721L;
         BadArgs(String key, Object... args) {
-            super(JdepsTask.this.getMessage(key, args));
+            super(JdepsTask.getMessage(key, args));
             this.key = key;
             this.args = args;
         }
@@ -105,25 +104,22 @@
         new Option(false, "-s", "--summary") {
             void process(JdepsTask task, String opt, String arg) {
                 task.options.showSummary = true;
-                task.options.verbose = Options.Verbose.SUMMARY;
+                task.options.verbose = Analyzer.Type.SUMMARY;
             }
         },
         new Option(false, "-v", "--verbose") {
             void process(JdepsTask task, String opt, String arg) {
-                task.options.verbose = Options.Verbose.VERBOSE;
+                task.options.verbose = Analyzer.Type.VERBOSE;
             }
         },
         new Option(true, "-V", "--verbose-level") {
             void process(JdepsTask task, String opt, String arg) throws BadArgs {
-                switch (arg) {
-                    case "package":
-                        task.options.verbose = Options.Verbose.PACKAGE;
-                        break;
-                    case "class":
-                        task.options.verbose = Options.Verbose.CLASS;
-                        break;
-                    default:
-                        throw task.new BadArgs("err.invalid.arg.for.option", opt);
+                if ("package".equals(arg)) {
+                    task.options.verbose = Analyzer.Type.PACKAGE;
+                } else if ("class".equals(arg)) {
+                    task.options.verbose = Analyzer.Type.CLASS;
+                } else {
+                    throw task.new BadArgs("err.invalid.arg.for.option", opt);
                 }
             }
         },
@@ -171,7 +167,6 @@
                 task.options.fullVersion = true;
             }
         },
-
     };
 
     private static final String PROGNAME = "jdeps";
@@ -216,7 +211,7 @@
                 showHelp();
                 return EXIT_CMDERR;
             }
-            if (options.showSummary && options.verbose != Options.Verbose.SUMMARY) {
+            if (options.showSummary && options.verbose != Analyzer.Type.SUMMARY) {
                 showHelp();
                 return EXIT_CMDERR;
             }
@@ -236,26 +231,14 @@
     }
 
     private final List<Archive> sourceLocations = new ArrayList<Archive>();
-    private final Archive NOT_FOUND = new Archive(getMessage("artifact.not.found"));
     private boolean run() throws IOException {
         findDependencies();
-        switch (options.verbose) {
-            case VERBOSE:
-            case CLASS:
-                printClassDeps(log);
-                break;
-            case PACKAGE:
-                printPackageDeps(log);
-                break;
-            case SUMMARY:
-                for (Archive origin : sourceLocations) {
-                    for (Archive target : origin.getRequiredArchives()) {
-                        log.format("%-30s -> %s%n", origin, target);
-                    }
-                }
-                break;
-            default:
-                throw new InternalError("Should not reach here");
+        Analyzer analyzer = new Analyzer(options.verbose);
+        analyzer.run(sourceLocations);
+        if (options.verbose == Analyzer.Type.SUMMARY) {
+            printSummary(log, analyzer);
+        } else {
+            printDependencies(log, analyzer);
         }
         return true;
     }
@@ -331,7 +314,7 @@
                 } catch (ConstantPoolException e) {
                     throw new ClassFileError(e);
                 }
-                a.addClass(classFileName);
+
                 if (!doneClasses.contains(classFileName)) {
                     doneClasses.add(classFileName);
                 }
@@ -341,7 +324,7 @@
                         if (!doneClasses.contains(cn) && !deque.contains(cn)) {
                             deque.add(cn);
                         }
-                        a.addDependency(d);
+                        a.addClass(d.getOrigin(), d.getTarget());
                     }
                 }
             }
@@ -367,19 +350,20 @@
                         } catch (ConstantPoolException e) {
                             throw new ClassFileError(e);
                         }
-                        a.addClass(classFileName);
                         if (!doneClasses.contains(classFileName)) {
                             // if name is a fully-qualified class name specified
                             // from command-line, this class might already be parsed
                             doneClasses.add(classFileName);
-                            if (depth > 0) {
-                                for (Dependency d : finder.findDependencies(cf)) {
-                                    if (filter.accepts(d)) {
-                                        String cn = d.getTarget().getName();
-                                        if (!doneClasses.contains(cn) && !deque.contains(cn)) {
-                                            deque.add(cn);
-                                        }
-                                        a.addDependency(d);
+                            for (Dependency d : finder.findDependencies(cf)) {
+                                if (depth == 0) {
+                                    // ignore the dependency
+                                    a.addClass(d.getOrigin());
+                                    break;
+                                } else if (filter.accepts(d)) {
+                                    a.addClass(d.getOrigin(), d.getTarget());
+                                    String cn = d.getTarget().getName();
+                                    if (!doneClasses.contains(cn) && !deque.contains(cn)) {
+                                        deque.add(cn);
                                     }
                                 }
                             }
@@ -388,7 +372,7 @@
                     }
                 }
                 if (cf == null) {
-                    NOT_FOUND.addClass(name);
+                    doneClasses.add(name);
                 }
             }
             unresolved = deque;
@@ -396,96 +380,44 @@
         } while (!unresolved.isEmpty() && depth-- > 0);
     }
 
-    private void printPackageDeps(PrintWriter out) {
-        for (Archive source : sourceLocations) {
-            SortedMap<Location, SortedSet<Location>> deps = source.getDependencies();
-            if (deps.isEmpty())
-                continue;
-
-            for (Archive target : source.getRequiredArchives()) {
-                out.format("%s -> %s%n", source, target);
-            }
-
-            Map<String, Archive> pkgs = new TreeMap<String, Archive>();
-            SortedMap<String, Archive> targets = new TreeMap<String, Archive>();
-            String pkg = "";
-            for (Map.Entry<Location, SortedSet<Location>> e : deps.entrySet()) {
-                String cn = e.getKey().getClassName();
-                String p = packageOf(e.getKey());
-                Archive origin = Archive.find(e.getKey());
-                assert origin != null;
-                if (!pkgs.containsKey(p)) {
-                    pkgs.put(p, origin);
-                } else if (pkgs.get(p) != origin) {
-                    warning("warn.split.package", p, origin, pkgs.get(p));
-                }
-
-                if (!p.equals(pkg)) {
-                    printTargets(out, targets);
-                    pkg = p;
-                    targets.clear();
-                    out.format("   %s (%s)%n", p, origin.getFileName());
-                }
-
-                for (Location t : e.getValue()) {
-                    p = packageOf(t);
-                    Archive target = Archive.find(t);
-                    if (!targets.containsKey(p)) {
-                        targets.put(p, target);
-                    }
+    private void printSummary(final PrintWriter out, final Analyzer analyzer) {
+        Analyzer.Visitor visitor = new Analyzer.Visitor() {
+            public void visit(String origin, String profile) {
+                if (options.showProfile) {
+                    out.format("%-30s -> %s%n", origin, profile);
                 }
             }
-            printTargets(out, targets);
-            out.println();
-        }
+            public void visit(Archive origin, Archive target) {
+                if (!options.showProfile) {
+                    out.format("%-30s -> %s%n", origin, target);
+                }
+            }
+        };
+        analyzer.visitSummary(visitor);
     }
 
-    private void printTargets(PrintWriter out, Map<String, Archive> targets) {
-        for (Map.Entry<String, Archive> t : targets.entrySet()) {
-            String pn = t.getKey();
-            out.format("      -> %-40s %s%n", pn, getPackageInfo(pn, t.getValue()));
-        }
-    }
-
-    private String getPackageInfo(String pn, Archive source) {
-        if (PlatformClassPath.contains(source)) {
-            String name = PlatformClassPath.getProfileName(pn);
-            if (name.isEmpty()) {
-                return "JDK internal API (" + source.getFileName() + ")";
+    private void printDependencies(final PrintWriter out, final Analyzer analyzer) {
+        Analyzer.Visitor visitor = new Analyzer.Visitor() {
+            private String pkg = "";
+            public void visit(String origin, String target) {
+                if (!origin.equals(pkg)) {
+                    pkg = origin;
+                    out.format("   %s (%s)%n", origin, analyzer.getArchiveName(origin));
+                }
+                Archive source = analyzer.getArchive(target);
+                String profile = options.showProfile ? analyzer.getProfile(target) : "";
+                out.format("      -> %-50s %s%n", target,
+                           PlatformClassPath.contains(source)
+                               ? profile
+                               : analyzer.getArchiveName(target));
             }
-            return options.showProfile ? name : "";
-        }
-        return source.getFileName();
-    }
-
-    private static String packageOf(Location loc) {
-        String pkg = loc.getPackageName();
-        return pkg.isEmpty() ? "<unnamed>" : pkg;
+            public void visit(Archive origin, Archive target) {
+                out.format("%s -> %s%n", origin, target);
+            }
+        };
+        analyzer.visit(visitor);
     }
 
-    private void printClassDeps(PrintWriter out) {
-        for (Archive source : sourceLocations) {
-            SortedMap<Location, SortedSet<Location>> deps = source.getDependencies();
-            if (deps.isEmpty())
-                continue;
-
-            for (Archive target : source.getRequiredArchives()) {
-                out.format("%s -> %s%n", source, target);
-            }
-            out.format("%s%n", source);
-            for (Map.Entry<Location, SortedSet<Location>> e : deps.entrySet()) {
-                String cn = e.getKey().getClassName();
-                Archive origin = Archive.find(e.getKey());
-                out.format("   %s (%s)%n", cn, origin.getFileName());
-                for (Location t : e.getValue()) {
-                    cn = t.getClassName();
-                    Archive target = Archive.find(t);
-                    out.format("      -> %-60s %s%n", cn, getPackageInfo(t.getPackageName(), target));
-                }
-            }
-            out.println();
-        }
-    }
     public void handleOptions(String[] args) throws BadArgs {
         // process options
         for (int i=0; i < args.length; i++) {
@@ -570,7 +502,7 @@
         }
     }
 
-    public String getMessage(String key, Object... args) {
+    static String getMessage(String key, Object... args) {
         try {
             return MessageFormat.format(ResourceBundleHelper.bundle.getString(key), args);
         } catch (MissingResourceException e) {
@@ -579,13 +511,6 @@
     }
 
     private static class Options {
-        enum Verbose {
-            CLASS,
-            PACKAGE,
-            SUMMARY,
-            VERBOSE
-        };
-
         boolean help;
         boolean version;
         boolean fullVersion;
@@ -596,7 +521,7 @@
         String regex;
         String classpath = "";
         int depth = 1;
-        Verbose verbose = Verbose.PACKAGE;
+        Analyzer.Type verbose = Analyzer.Type.PACKAGE;
         Set<String> packageNames = new HashSet<String>();
     }