1 /*
   2  * Copyright (c) 2012, 2014, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package com.sun.tools.jdeps;
  26 
  27 import com.sun.tools.classfile.AccessFlags;
  28 import com.sun.tools.classfile.ClassFile;
  29 import com.sun.tools.classfile.ConstantPoolException;
  30 import com.sun.tools.classfile.Dependencies;
  31 import com.sun.tools.classfile.Dependencies.ClassFileError;
  32 import com.sun.tools.classfile.Dependency;
  33 import com.sun.tools.classfile.Dependency.Location;
  34 import static com.sun.tools.jdeps.Analyzer.Type.*;
  35 import java.io.*;
  36 import java.nio.file.DirectoryStream;
  37 import java.nio.file.Files;
  38 import java.nio.file.Path;
  39 import java.nio.file.Paths;
  40 import java.text.MessageFormat;
  41 import java.util.*;
  42 import java.util.regex.Pattern;
  43 
  44 /**
  45  * Implementation for the jdeps tool for static class dependency analysis.
  46  */
  47 class JdepsTask {
  48     static class BadArgs extends Exception {
  49         static final long serialVersionUID = 8765093759964640721L;
  50         BadArgs(String key, Object... args) {
  51             super(JdepsTask.getMessage(key, args));
  52             this.key = key;
  53             this.args = args;
  54         }
  55 
  56         BadArgs showUsage(boolean b) {
  57             showUsage = b;
  58             return this;
  59         }
  60         final String key;
  61         final Object[] args;
  62         boolean showUsage;
  63     }
  64 
  65     static abstract class Option {
  66         Option(boolean hasArg, String... aliases) {
  67             this.hasArg = hasArg;
  68             this.aliases = aliases;
  69         }
  70 
  71         boolean isHidden() {
  72             return false;
  73         }
  74 
  75         boolean matches(String opt) {
  76             for (String a : aliases) {
  77                 if (a.equals(opt))
  78                     return true;
  79                 if (hasArg && opt.startsWith(a + "="))
  80                     return true;
  81             }
  82             return false;
  83         }
  84 
  85         boolean ignoreRest() {
  86             return false;
  87         }
  88 
  89         abstract void process(JdepsTask task, String opt, String arg) throws BadArgs;
  90         final boolean hasArg;
  91         final String[] aliases;
  92     }
  93 
  94     static abstract class HiddenOption extends Option {
  95         HiddenOption(boolean hasArg, String... aliases) {
  96             super(hasArg, aliases);
  97         }
  98 
  99         boolean isHidden() {
 100             return true;
 101         }
 102     }
 103 
 104     static Option[] recognizedOptions = {
 105         new Option(false, "-h", "-?", "-help") {
 106             void process(JdepsTask task, String opt, String arg) {
 107                 task.options.help = true;
 108             }
 109         },
 110         new Option(true, "-dotoutput") {
 111             void process(JdepsTask task, String opt, String arg) throws BadArgs {
 112                 Path p = Paths.get(arg);
 113                 if (Files.exists(p) && (!Files.isDirectory(p) || !Files.isWritable(p))) {
 114                     throw new BadArgs("err.invalid.path", arg);
 115                 }
 116                 task.options.dotOutputDir = arg;
 117             }
 118         },
 119         new Option(false, "-s", "-summary") {
 120             void process(JdepsTask task, String opt, String arg) {
 121                 task.options.showSummary = true;
 122                 task.options.verbose = SUMMARY;
 123             }
 124         },
 125         new Option(false, "-v", "-verbose",
 126                           "-verbose:package",
 127                           "-verbose:class") {
 128             void process(JdepsTask task, String opt, String arg) throws BadArgs {
 129                 switch (opt) {
 130                     case "-v":
 131                     case "-verbose":
 132                         task.options.verbose = VERBOSE;
 133                         task.options.filterSameArchive = false;
 134                         task.options.filterSamePackage = false;
 135                         break;
 136                     case "-verbose:package":
 137                         task.options.verbose = PACKAGE;
 138                         break;
 139                     case "-verbose:class":
 140                         task.options.verbose = CLASS;
 141                         break;
 142                     default:
 143                         throw new BadArgs("err.invalid.arg.for.option", opt);
 144                 }
 145             }
 146         },
 147         new Option(true, "-cp", "-classpath") {
 148             void process(JdepsTask task, String opt, String arg) {
 149                 task.options.classpath = arg;
 150             }
 151         },
 152         new Option(true, "-p", "-package") {
 153             void process(JdepsTask task, String opt, String arg) {
 154                 task.options.packageNames.add(arg);
 155             }
 156         },
 157         new Option(true, "-e", "-regex") {
 158             void process(JdepsTask task, String opt, String arg) {
 159                 task.options.regex = arg;
 160             }
 161         },
 162 
 163         new Option(true, "-f", "-filter") {
 164             void process(JdepsTask task, String opt, String arg) {
 165                 task.options.filterRegex = arg;
 166             }
 167         },
 168         new Option(false, "-filter:package",
 169                           "-filter:archive",
 170                           "-filter:none") {
 171             void process(JdepsTask task, String opt, String arg) {
 172                 switch (opt) {
 173                     case "-filter:package":
 174                         task.options.filterSamePackage = true;
 175                         task.options.filterSameArchive = false;
 176                         break;
 177                     case "-filter:archive":
 178                         task.options.filterSameArchive = true;
 179                         task.options.filterSamePackage = false;
 180                         break;
 181                     case "-filter:none":
 182                         task.options.filterSameArchive = false;
 183                         task.options.filterSamePackage = false;
 184                         break;
 185                 }
 186             }
 187         },
 188         new Option(true, "-include") {
 189             void process(JdepsTask task, String opt, String arg) throws BadArgs {
 190                 task.options.includePattern = Pattern.compile(arg);
 191             }
 192         },
 193         new Option(false, "-P", "-profile") {
 194             void process(JdepsTask task, String opt, String arg) throws BadArgs {
 195                 task.options.showProfile = true;
 196                 task.options.showModule = false;
 197             }
 198         },
 199         new Option(false, "-M", "-module") {
 200             void process(JdepsTask task, String opt, String arg) throws BadArgs {
 201                 task.options.showModule = true;
 202                 task.options.showProfile = false;
 203             }
 204         },
 205         new Option(false, "-apionly") {
 206             void process(JdepsTask task, String opt, String arg) {
 207                 task.options.apiOnly = true;
 208             }
 209         },
 210         new Option(false, "-R", "-recursive") {
 211             void process(JdepsTask task, String opt, String arg) {
 212                 task.options.depth = 0;
 213                 // turn off filtering
 214                 task.options.filterSameArchive = false;
 215                 task.options.filterSamePackage = false;
 216             }
 217         },
 218         new Option(false, "-jdkinternals") {
 219             void process(JdepsTask task, String opt, String arg) {
 220                 task.options.findJDKInternals = true;
 221                 task.options.verbose = CLASS;
 222                 if (task.options.includePattern == null) {
 223                     task.options.includePattern = Pattern.compile(".*");
 224                 }
 225             }
 226         },
 227             new HiddenOption(false, "-verify:access") {
 228                 void process(JdepsTask task, String opt, String arg) {
 229                     task.options.verifyAccess = true;
 230                     task.options.verbose = VERBOSE;
 231                     task.options.filterSameArchive = false;
 232                     task.options.filterSamePackage = false;
 233                 }
 234             },
 235             new HiddenOption(true, "-mp") {
 236                 void process(JdepsTask task, String opt, String arg) throws BadArgs {
 237                     task.options.mpath = Paths.get(arg);
 238                     if (!Files.isDirectory(task.options.mpath)) {
 239                         throw new BadArgs("err.invalid.path", arg);
 240                     }
 241                     if (task.options.includePattern == null) {
 242                         task.options.includePattern = Pattern.compile(".*");
 243                     }
 244                 }
 245             },
 246         new Option(false, "-version") {
 247             void process(JdepsTask task, String opt, String arg) {
 248                 task.options.version = true;
 249             }
 250         },
 251         new HiddenOption(false, "-fullversion") {
 252             void process(JdepsTask task, String opt, String arg) {
 253                 task.options.fullVersion = true;
 254             }
 255         },
 256         new HiddenOption(false, "-showlabel") {
 257             void process(JdepsTask task, String opt, String arg) {
 258                 task.options.showLabel = true;
 259             }
 260         },
 261         new HiddenOption(false, "-q", "-quiet") {
 262             void process(JdepsTask task, String opt, String arg) {
 263                 task.options.nowarning = true;
 264             }
 265         },
 266         new HiddenOption(true, "-depth") {
 267             void process(JdepsTask task, String opt, String arg) throws BadArgs {
 268                 try {
 269                     task.options.depth = Integer.parseInt(arg);
 270                 } catch (NumberFormatException e) {
 271                     throw new BadArgs("err.invalid.arg.for.option", opt);
 272                 }
 273             }
 274         },
 275     };
 276 
 277     private static final String PROGNAME = "jdeps";
 278     private final Options options = new Options();
 279     private final List<String> classes = new ArrayList<>();
 280 
 281     private PrintWriter log;
 282     void setLog(PrintWriter out) {
 283         log = out;
 284     }
 285 
 286     /**
 287      * Result codes.
 288      */
 289     static final int EXIT_OK = 0, // Completed with no errors.
 290                      EXIT_ERROR = 1, // Completed but reported errors.
 291                      EXIT_CMDERR = 2, // Bad command-line arguments
 292                      EXIT_SYSERR = 3, // System error or resource exhaustion.
 293                      EXIT_ABNORMAL = 4;// terminated abnormally
 294 
 295     int run(String[] args) {
 296         if (log == null) {
 297             log = new PrintWriter(System.out);
 298         }
 299         try {
 300             handleOptions(args);
 301             if (options.help) {
 302                 showHelp();
 303             }
 304             if (options.version || options.fullVersion) {
 305                 showVersion(options.fullVersion);
 306             }
 307             if (classes.isEmpty() && options.includePattern == null) {
 308                 if (options.help || options.version || options.fullVersion) {
 309                     return EXIT_OK;
 310                 } else {
 311                     showHelp();
 312                     return EXIT_CMDERR;
 313                 }
 314             }
 315             if (options.regex != null && options.packageNames.size() > 0) {
 316                 showHelp();
 317                 return EXIT_CMDERR;
 318             }
 319             if ((options.findJDKInternals || options.verifyAccess) &&
 320                    (options.regex != null || options.packageNames.size() > 0 || options.showSummary)) {
 321                 showHelp();
 322                 return EXIT_CMDERR;
 323             }
 324             if (options.showSummary && options.verbose != SUMMARY) {
 325                 showHelp();
 326                 return EXIT_CMDERR;
 327             }
 328 
 329             boolean ok = run();
 330             return ok ? EXIT_OK : EXIT_ERROR;
 331         } catch (BadArgs e) {
 332             reportError(e.key, e.args);
 333             if (e.showUsage) {
 334                 log.println(getMessage("main.usage.summary", PROGNAME));
 335             }
 336             return EXIT_CMDERR;
 337         } catch (IOException e) {
 338             return EXIT_ABNORMAL;
 339         } finally {
 340             log.flush();
 341         }
 342     }
 343 
 344     private final List<Archive> sourceLocations = new ArrayList<>();
 345     private final List<Archive> classpaths = new ArrayList<>();
 346     private final List<Archive> initialArchives = new ArrayList<>();
 347     private boolean run() throws IOException {
 348         buildArchives();
 349 
 350         if (options.verifyAccess) {
 351             return verifyModuleAccess();
 352         } else {
 353             return analyzeDeps();
 354         }
 355     }
 356 
 357     private boolean analyzeDeps() throws IOException {
 358         Analyzer analyzer = new Analyzer(options.verbose, new Analyzer.Filter() {
 359             @Override
 360             public boolean accepts(Location origin, Archive originArchive,
 361                                    Location target, Archive targetArchive)
 362             {
 363                 if (options.findJDKInternals) {
 364                     // accepts target that is JDK class but not exported
 365                     return isJDKModule(targetArchive) &&
 366                               !((Module) targetArchive).isExported(target.getClassName());
 367                 } else if (options.filterSameArchive) {
 368                     // accepts origin and target that from different archive
 369                     return originArchive != targetArchive;
 370                 }
 371                 return true;
 372             }
 373         });
 374 
 375         // parse classfiles and find all dependencies
 376         findDependencies(options.apiOnly);
 377 
 378         // analyze the dependencies
 379         analyzer.run(sourceLocations);
 380 
 381         // output result
 382         if (options.dotOutputDir != null) {
 383             Path dir = Paths.get(options.dotOutputDir);
 384             Files.createDirectories(dir);
 385             generateDotFiles(dir, analyzer);
 386         } else {
 387             printRawOutput(log, analyzer);
 388         }
 389 
 390         if (options.findJDKInternals && !options.nowarning) {
 391             showReplacements(analyzer);
 392         }
 393         return true;
 394     }
 395 
 396     private boolean verifyModuleAccess() throws IOException {
 397         // two passes
 398         // 1. check API dependences where the types of dependences must be re-exported
 399         // 2. check all dependences where types must be accessible
 400 
 401         // pass 1
 402         findDependencies(true /* api only */);
 403         Analyzer analyzer = Analyzer.getExportedAPIsAnalyzer();
 404         boolean pass1 = analyzer.run(sourceLocations);
 405         if (!pass1) {
 406             System.out.println("ERROR: Failed API access verification");
 407         }
 408         // pass 2
 409         findDependencies(false);
 410         analyzer =  Analyzer.getModuleAccessAnalyzer();
 411         boolean pass2 = analyzer.run(sourceLocations);
 412         if (!pass2) {
 413             System.out.println("ERROR: Failed module access verification");
 414         }
 415         if (pass1 & pass2) {
 416             System.out.println("Access verification succeeded.");
 417         }
 418         return pass1 & pass2;
 419     }
 420 
 421     private void generateSummaryDotFile(Path dir, Analyzer analyzer) throws IOException {
 422         // If verbose mode (-v or -verbose option),
 423         // the summary.dot file shows package-level dependencies.
 424         Analyzer.Type summaryType =
 425             (options.verbose == PACKAGE || options.verbose == SUMMARY) ? SUMMARY : PACKAGE;
 426         Path summary = dir.resolve("summary.dot");
 427         try (PrintWriter sw = new PrintWriter(Files.newOutputStream(summary));
 428              SummaryDotFile dotfile = new SummaryDotFile(sw, summaryType)) {
 429             for (Archive archive : sourceLocations) {
 430                 if (!archive.isEmpty()) {
 431                     if (options.verbose == PACKAGE || options.verbose == SUMMARY) {
 432                         if (options.showLabel) {
 433                             // build labels listing package-level dependencies
 434                             analyzer.visitDependences(archive, dotfile.labelBuilder(), PACKAGE);
 435                         }
 436                     }
 437                     analyzer.visitDependences(archive, dotfile, summaryType);
 438                 }
 439             }
 440         }
 441     }
 442 
 443     private void generateDotFiles(Path dir, Analyzer analyzer) throws IOException {
 444         // output individual .dot file for each archive
 445         if (options.verbose != SUMMARY) {
 446             for (Archive archive : sourceLocations) {
 447                 if (analyzer.hasDependences(archive)) {
 448                     Path dotfile = dir.resolve(archive.getName() + ".dot");
 449                     try (PrintWriter pw = new PrintWriter(Files.newOutputStream(dotfile));
 450                          DotFileFormatter formatter = new DotFileFormatter(pw, archive)) {
 451                         analyzer.visitDependences(archive, formatter);
 452                     }
 453                 }
 454             }
 455         }
 456         // generate summary dot file
 457         generateSummaryDotFile(dir, analyzer);
 458     }
 459 
 460     private void printRawOutput(PrintWriter writer, Analyzer analyzer) {
 461         RawOutputFormatter depFormatter = new RawOutputFormatter(writer);
 462         RawSummaryFormatter summaryFormatter = new RawSummaryFormatter(writer);
 463         for (Archive archive : sourceLocations) {
 464             if (!archive.isEmpty()) {
 465                 analyzer.visitDependences(archive, summaryFormatter, SUMMARY);
 466                 if (analyzer.hasDependences(archive) && options.verbose != SUMMARY) {
 467                     analyzer.visitDependences(archive, depFormatter);
 468                 }
 469             }
 470         }
 471     }
 472 
 473     private boolean isValidClassName(String name) {
 474         if (!Character.isJavaIdentifierStart(name.charAt(0))) {
 475             return false;
 476         }
 477         for (int i=1; i < name.length(); i++) {
 478             char c = name.charAt(i);
 479             if (c != '.'  && !Character.isJavaIdentifierPart(c)) {
 480                 return false;
 481             }
 482         }
 483         return true;
 484     }
 485 
 486     /*
 487      * Dep Filter configured based on the input jdeps option
 488      * 1. -p and -regex to match target dependencies
 489      * 2. -filter:package to filter out same-package dependencies
 490      *
 491      * This filter is applied when jdeps parses the class files
 492      * and filtered dependencies are not stored in the Analyzer.
 493      *
 494      * -filter:archive is applied later in the Analyzer as the
 495      * containing archive of a target class may not be known until
 496      * the entire archive
 497      */
 498     class DependencyFilter implements Dependency.Filter {
 499         final Dependency.Filter filter;
 500         final Pattern filterPattern;
 501         DependencyFilter() {
 502             if (options.regex != null) {
 503                 this.filter = Dependencies.getRegexFilter(Pattern.compile(options.regex));
 504             } else if (options.packageNames.size() > 0) {
 505                 this.filter = Dependencies.getPackageFilter(options.packageNames, false);
 506             } else {
 507                 this.filter = null;
 508             }
 509 
 510             this.filterPattern =
 511                 options.filterRegex != null ? Pattern.compile(options.filterRegex) : null;
 512         }
 513         @Override
 514         public boolean accepts(Dependency d) {
 515             if (d.getOrigin().equals(d.getTarget())) {
 516                 return false;
 517             }
 518             String pn = d.getTarget().getPackageName();
 519             if (options.filterSamePackage && d.getOrigin().getPackageName().equals(pn)) {
 520                 return false;
 521             }
 522 
 523             if (filterPattern != null && filterPattern.matcher(pn).matches()) {
 524                 return false;
 525             }
 526             return filter != null ? filter.accepts(d) : true;
 527         }
 528     }
 529 
 530     /**
 531      * Tests if the given class matches the pattern given in the -include option
 532      */
 533     private boolean matches(String classname) {
 534         if (options.includePattern != null) {
 535             return options.includePattern.matcher(classname.replace('/', '.')).matches();
 536         } else {
 537             return true;
 538         }
 539     }
 540 
 541     private void buildArchives() throws IOException {
 542         for (String s : classes) {
 543             Path p = Paths.get(s);
 544             if (Files.exists(p)) {
 545                 initialArchives.add(Archive.getInstance(p));
 546             }
 547         }
 548         sourceLocations.addAll(initialArchives);
 549 
 550         classpaths.addAll(getClassPathArchives(options.classpath));
 551         if (options.includePattern != null) {
 552             initialArchives.addAll(classpaths);
 553         }
 554         classpaths.addAll(PlatformClassPath.getModules(options.mpath));
 555         if (options.mpath != null) {
 556             initialArchives.addAll(PlatformClassPath.getModules(options.mpath));
 557         } else {
 558             classpaths.addAll(PlatformClassPath.getJarFiles());
 559         }
 560         // add all classpath archives to the source locations for reporting
 561         sourceLocations.addAll(classpaths);
 562     }
 563 
 564     private void findDependencies(boolean apiOnly) throws IOException {
 565         Dependency.Finder finder =
 566             apiOnly ? Dependencies.getAPIFinder(AccessFlags.ACC_PROTECTED)
 567                     : Dependencies.getClassDependencyFinder();
 568         Dependency.Filter filter = new DependencyFilter();
 569 
 570         Deque<String> roots = new LinkedList<>();
 571         for (String s : classes) {
 572             Path p = Paths.get(s);
 573             if (!Files.exists(p)) {
 574                 if (isValidClassName(s)) {
 575                     roots.add(s);
 576                 } else {
 577                     warning("warn.invalid.arg", s);
 578                 }
 579             }
 580         }
 581 
 582         // Work queue of names of classfiles to be searched.
 583         // Entries will be unique, and for classes that do not yet have
 584         // dependencies in the results map.
 585         Deque<String> deque = new LinkedList<>();
 586         Set<String> doneClasses = new HashSet<>();
 587 
 588         // get the immediate dependencies of the input files
 589         for (Archive a : initialArchives) {
 590             for (ClassFile cf : a.reader().getClassFiles()) {
 591                 String classFileName;
 592                 try {
 593                     classFileName = cf.getName();
 594                 } catch (ConstantPoolException e) {
 595                     throw new ClassFileError(e);
 596                 }
 597 
 598                 // tests if this class matches the -include or -apiOnly option if specified
 599                 if (!matches(classFileName) || (apiOnly && !cf.access_flags.is(AccessFlags.ACC_PUBLIC))) {
 600                     continue;
 601                 }
 602 
 603                 if (!doneClasses.contains(classFileName)) {
 604                     doneClasses.add(classFileName);
 605                 }
 606 
 607                 for (Dependency d : finder.findDependencies(cf)) {
 608                     if (filter.accepts(d)) {
 609                         String cn = d.getTarget().getName();
 610                         if (!doneClasses.contains(cn) && !deque.contains(cn)) {
 611                             deque.add(cn);
 612                         }
 613                         a.addClass(d.getOrigin(), d.getTarget());
 614                     }
 615                 }
 616                 for (String name : a.reader().skippedEntries()) {
 617                     warning("warn.skipped.entry", name, a.getPathName());
 618                 }
 619             }
 620         }
 621 
 622         // add Archive for looking up classes from the classpath
 623         // for transitive dependency analysis
 624         Deque<String> unresolved = roots;
 625         int depth = options.depth > 0 ? options.depth : Integer.MAX_VALUE;
 626         do {
 627             String name;
 628             while ((name = unresolved.poll()) != null) {
 629                 if (doneClasses.contains(name)) {
 630                     continue;
 631                 }
 632                 ClassFile cf = null;
 633                 for (Archive a : classpaths) {
 634                     cf = a.reader().getClassFile(name);
 635                     if (cf != null) {
 636                         String classFileName;
 637                         try {
 638                             classFileName = cf.getName();
 639                         } catch (ConstantPoolException e) {
 640                             throw new ClassFileError(e);
 641                         }
 642                         if (!doneClasses.contains(classFileName)) {
 643                             // if name is a fully-qualified class name specified
 644                             // from command-line, this class might already be parsed
 645                             doneClasses.add(classFileName);
 646                             for (Dependency d : finder.findDependencies(cf)) {
 647                                 if (depth == 0) {
 648                                     // ignore the dependency
 649                                     a.addClass(d.getOrigin());
 650                                     break;
 651                                 } else if (filter.accepts(d)) {
 652                                     a.addClass(d.getOrigin(), d.getTarget());
 653                                     String cn = d.getTarget().getName();
 654                                     if (!doneClasses.contains(cn) && !deque.contains(cn)) {
 655                                         deque.add(cn);
 656                                     }
 657                                 }
 658                             }
 659                         }
 660                         break;
 661                     }
 662                 }
 663                 if (cf == null) {
 664                     doneClasses.add(name);
 665                 }
 666             }
 667             unresolved = deque;
 668             deque = new LinkedList<>();
 669         } while (!unresolved.isEmpty() && depth-- > 0);
 670     }
 671 
 672     public void handleOptions(String[] args) throws BadArgs {
 673         // process options
 674         for (int i=0; i < args.length; i++) {
 675             if (args[i].charAt(0) == '-') {
 676                 String name = args[i];
 677                 Option option = getOption(name);
 678                 String param = null;
 679                 if (option.hasArg) {
 680                     if (name.startsWith("-") && name.indexOf('=') > 0) {
 681                         param = name.substring(name.indexOf('=') + 1, name.length());
 682                     } else if (i + 1 < args.length) {
 683                         param = args[++i];
 684                     }
 685                     if (param == null || param.isEmpty() || param.charAt(0) == '-') {
 686                         throw new BadArgs("err.missing.arg", name).showUsage(true);
 687                     }
 688                 }
 689                 option.process(this, name, param);
 690                 if (option.ignoreRest()) {
 691                     i = args.length;
 692                 }
 693             } else {
 694                 // process rest of the input arguments
 695                 for (; i < args.length; i++) {
 696                     String name = args[i];
 697                     if (name.charAt(0) == '-') {
 698                         throw new BadArgs("err.option.after.class", name).showUsage(true);
 699                     }
 700                     classes.add(name);
 701                 }
 702             }
 703         }
 704     }
 705 
 706     private Option getOption(String name) throws BadArgs {
 707         for (Option o : recognizedOptions) {
 708             if (o.matches(name)) {
 709                 return o;
 710             }
 711         }
 712         throw new BadArgs("err.unknown.option", name).showUsage(true);
 713     }
 714 
 715     private void reportError(String key, Object... args) {
 716         log.println(getMessage("error.prefix") + " " + getMessage(key, args));
 717     }
 718 
 719     private void warning(String key, Object... args) {
 720         log.println(getMessage("warn.prefix") + " " + getMessage(key, args));
 721     }
 722 
 723     private void showHelp() {
 724         log.println(getMessage("main.usage", PROGNAME));
 725         for (Option o : recognizedOptions) {
 726             String name = o.aliases[0].substring(1); // there must always be at least one name
 727             name = name.charAt(0) == '-' ? name.substring(1) : name;
 728             if (o.isHidden() || name.equals("h") || name.startsWith("filter:")) {
 729                 continue;
 730             }
 731             log.println(getMessage("main.opt." + name));
 732         }
 733     }
 734 
 735     private void showVersion(boolean full) {
 736         log.println(version(full ? "full" : "release"));
 737     }
 738 
 739     private String version(String key) {
 740         // key=version:  mm.nn.oo[-milestone]
 741         // key=full:     mm.mm.oo[-milestone]-build
 742         if (ResourceBundleHelper.versionRB == null) {
 743             return System.getProperty("java.version");
 744         }
 745         try {
 746             return ResourceBundleHelper.versionRB.getString(key);
 747         } catch (MissingResourceException e) {
 748             return getMessage("version.unknown", System.getProperty("java.version"));
 749         }
 750     }
 751 
 752     static String getMessage(String key, Object... args) {
 753         try {
 754             return MessageFormat.format(ResourceBundleHelper.bundle.getString(key), args);
 755         } catch (MissingResourceException e) {
 756             throw new InternalError("Missing message: " + key);
 757         }
 758     }
 759 
 760     private static class Options {
 761         boolean help;
 762         boolean version;
 763         boolean fullVersion;
 764         boolean showProfile;
 765         boolean showModule;
 766         boolean showSummary;
 767         boolean apiOnly;
 768         boolean showLabel;
 769         boolean findJDKInternals;
 770         boolean nowarning;
 771         // default is to show package-level dependencies
 772         // and filter references from same package
 773         Analyzer.Type verbose = PACKAGE;
 774         boolean filterSamePackage = true;
 775         boolean filterSameArchive = false;
 776         String filterRegex;
 777         String dotOutputDir;
 778         String classpath = "";
 779         int depth = 1;
 780         Set<String> packageNames = new HashSet<>();
 781         String regex;             // apply to the dependences
 782         Pattern includePattern;   // apply to classes
 783         // module boundary access check
 784         boolean verifyAccess;
 785         Path mpath;
 786     }
 787     private static class ResourceBundleHelper {
 788         static final ResourceBundle versionRB;
 789         static final ResourceBundle bundle;
 790         static final ResourceBundle jdkinternals;
 791 
 792         static {
 793             Locale locale = Locale.getDefault();
 794             try {
 795                 bundle = ResourceBundle.getBundle("com.sun.tools.jdeps.resources.jdeps", locale);
 796             } catch (MissingResourceException e) {
 797                 throw new InternalError("Cannot find jdeps resource bundle for locale " + locale);
 798             }
 799             try {
 800                 versionRB = ResourceBundle.getBundle("com.sun.tools.jdeps.resources.version");
 801             } catch (MissingResourceException e) {
 802                 throw new InternalError("version.resource.missing");
 803             }
 804             try {
 805                 jdkinternals = ResourceBundle.getBundle("com.sun.tools.jdeps.resources.jdkinternals");
 806             } catch (MissingResourceException e) {
 807                 throw new InternalError("Cannot find jdkinternals resource bundle");
 808             }
 809         }
 810     }
 811 
 812     private List<Archive> getClassPathArchives(String paths) throws IOException {
 813         List<Archive> result = new ArrayList<>();
 814         if (paths.isEmpty()) {
 815             return result;
 816         }
 817         for (String p : paths.split(File.pathSeparator)) {
 818             if (p.length() > 0) {
 819                 List<Path> files = new ArrayList<>();
 820                 // wildcard to parse all JAR files e.g. -classpath dir/*
 821                 int i = p.lastIndexOf(".*");
 822                 if (i > 0) {
 823                     Path dir = Paths.get(p.substring(0, i));
 824                     try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir, "*.jar")) {
 825                         for (Path entry : stream) {
 826                             files.add(entry);
 827                         }
 828                     }
 829                 } else {
 830                     files.add(Paths.get(p));
 831                 }
 832                 for (Path f : files) {
 833                     if (Files.exists(f)) {
 834                         result.add(Archive.getInstance(f));
 835                     }
 836                 }
 837             }
 838         }
 839         return result;
 840     }
 841 
 842     class RawOutputFormatter implements Analyzer.Visitor {
 843         private final PrintWriter writer;
 844         private String pkg = "";
 845         RawOutputFormatter(PrintWriter writer) {
 846             this.writer = writer;
 847         }
 848         @Override
 849         public void visitDependence(String origin, Archive originArchive,
 850                                     String target, Archive targetArchive) {
 851             String tag = toTag(target, targetArchive);
 852             if (options.verbose == VERBOSE) {
 853                 writer.format("   %-50s -> %-50s %s%n", origin, target, tag);
 854             } else {
 855                 if (!origin.equals(pkg)) {
 856                     pkg = origin;
 857                     writer.format("   %s (%s)%n", origin, originArchive.getName());
 858                 }
 859                 writer.format("      -> %-50s %s%n", target, tag);
 860             }
 861         }
 862     }
 863 
 864     class RawSummaryFormatter implements Analyzer.Visitor {
 865         private final PrintWriter writer;
 866         RawSummaryFormatter(PrintWriter writer) {
 867             this.writer = writer;
 868         }
 869         @Override
 870         public void visitDependence(String origin, Archive originArchive,
 871                                     String target, Archive targetArchive) {
 872             String targetName =  targetArchive.getPathName();
 873             if (options.showModule && isJDKModule(targetArchive)) {
 874                 targetName = ((Module)targetArchive).name();
 875             }
 876             writer.format("%s -> %s", originArchive.getName(), targetName);
 877             if (options.showProfile && isJDKModule(targetArchive)) {
 878                 writer.format(" (%s)", target);
 879             }
 880             writer.format("%n");
 881         }
 882     }
 883 
 884     class DotFileFormatter implements Analyzer.Visitor, AutoCloseable {
 885         private final PrintWriter writer;
 886         private final String name;
 887         DotFileFormatter(PrintWriter writer, Archive archive) {
 888             this.writer = writer;
 889             this.name = archive.getName();
 890             writer.format("digraph \"%s\" {%n", name);
 891             writer.format("    // Path: %s%n", archive.getPathName());
 892         }
 893 
 894         @Override
 895         public void close() {
 896             writer.println("}");
 897         }
 898 
 899         @Override
 900         public void visitDependence(String origin, Archive originArchive,
 901                                     String target, Archive targetArchive) {
 902             String tag = toTag(target, targetArchive);
 903             writer.format("   %-50s -> \"%s\";%n",
 904                           String.format("\"%s\"", origin),
 905                           tag.isEmpty() ? target
 906                                         : String.format("%s (%s)", target, tag));
 907         }
 908     }
 909 
 910     class SummaryDotFile implements Analyzer.Visitor, AutoCloseable {
 911         private final PrintWriter writer;
 912         private final Analyzer.Type type;
 913         private final Map<Archive, Map<Archive,StringBuilder>> edges = new HashMap<>();
 914         SummaryDotFile(PrintWriter writer, Analyzer.Type type) {
 915             this.writer = writer;
 916             this.type = type;
 917             writer.format("digraph \"summary\" {%n");
 918         }
 919 
 920         @Override
 921         public void close() {
 922             writer.println("}");
 923         }
 924 
 925         @Override
 926         public void visitDependence(String origin, Archive originArchive,
 927                                     String target, Archive targetArchive) {
 928             String targetName = type == PACKAGE ? target : targetArchive.getName();
 929             if (isJDKModule(targetArchive)) {
 930                 Module m = (Module)targetArchive;
 931                 String n = showProfileOrModule(m);
 932                 if (!n.isEmpty()) {
 933                     targetName += " (" + n + ")";
 934                 }
 935             } else if (type == PACKAGE) {
 936                 targetName += " (" + targetArchive.getName() + ")";
 937             }
 938             String label = getLabel(originArchive, targetArchive);
 939             writer.format("  %-50s -> \"%s\"%s;%n",
 940                           String.format("\"%s\"", origin), targetName, label);
 941         }
 942 
 943         String getLabel(Archive origin, Archive target) {
 944             if (edges.isEmpty())
 945                 return "";
 946 
 947             StringBuilder label = edges.get(origin).get(target);
 948             return label == null ? "" : String.format(" [label=\"%s\",fontsize=9]", label.toString());
 949         }
 950 
 951         Analyzer.Visitor labelBuilder() {
 952             // show the package-level dependencies as labels in the dot graph
 953             return new Analyzer.Visitor() {
 954                 @Override
 955                 public void visitDependence(String origin, Archive originArchive, String target, Archive targetArchive) {
 956                     edges.putIfAbsent(originArchive, new HashMap<>());
 957                     edges.get(originArchive).putIfAbsent(targetArchive, new StringBuilder());
 958                     StringBuilder sb = edges.get(originArchive).get(targetArchive);
 959                     String tag = toTag(target, targetArchive);
 960                     addLabel(sb, origin, target, tag);
 961                 }
 962 
 963                 void addLabel(StringBuilder label, String origin, String target, String tag) {
 964                     label.append(origin).append(" -> ").append(target);
 965                     if (!tag.isEmpty()) {
 966                         label.append(" (" + tag + ")");
 967                     }
 968                     label.append("\\n");
 969                 }
 970             };
 971         }
 972     }
 973 
 974     /**
 975      * Test if the given archive is part of the JDK
 976      */
 977     private boolean isJDKModule(Archive archive) {
 978         return Module.class.isInstance(archive);
 979     }
 980 
 981     /**
 982      * If the given archive is JDK archive, this method returns the profile name
 983      * only if -profile option is specified; it accesses a private JDK API and
 984      * the returned value will have "JDK internal API" prefix
 985      *
 986      * For non-JDK archives, this method returns the file name of the archive.
 987      */
 988     private String toTag(String name, Archive source) {
 989         if (!isJDKModule(source)) {
 990             return source.getName();
 991         }
 992 
 993         Module module = (Module)source;
 994         boolean isExported = false;
 995         if (options.verbose == CLASS || options.verbose == VERBOSE) {
 996             isExported = module.isExported(name);
 997         } else {
 998             isExported = module.isExportedPackage(name);
 999         }
1000         if (isExported) {
1001             // exported API
1002             return showProfileOrModule(module);
1003         } else {
1004             return "JDK internal API (" + source.getName() + ")";
1005         }
1006     }
1007 
1008     private String showProfileOrModule(Module m) {
1009         String tag = "";
1010         if (options.showProfile) {
1011             Profile p = Profile.getProfile(m);
1012             if (p != null) {
1013                 tag = p.profileName();
1014             }
1015         } else if (options.showModule) {
1016             tag = m.name();
1017         }
1018         return tag;
1019     }
1020 
1021     private Profile getProfile(String name) {
1022         String pn = name;
1023         if (options.verbose == CLASS || options.verbose == VERBOSE) {
1024             int i = name.lastIndexOf('.');
1025             pn = i > 0 ? name.substring(0, i) : "";
1026         }
1027         return Profile.getProfile(pn);
1028     }
1029 
1030     /**
1031      * Returns the recommended replacement API for the given classname;
1032      * or return null if replacement API is not known.
1033      */
1034     private String replacementFor(String cn) {
1035         String name = cn;
1036         String value = null;
1037         while (value == null && name != null) {
1038             try {
1039                 value = ResourceBundleHelper.jdkinternals.getString(name);
1040             } catch (MissingResourceException e) {
1041                 // go up one subpackage level
1042                 int i = name.lastIndexOf('.');
1043                 name = i > 0 ? name.substring(0, i) : null;
1044             }
1045         }
1046         return value;
1047     };
1048 
1049     private void showReplacements(Analyzer analyzer) {
1050         Map<String,String> jdkinternals = new TreeMap<>();
1051         boolean useInternals = false;
1052         for (Archive source : sourceLocations) {
1053             useInternals = useInternals || analyzer.hasDependences(source);
1054             for (String cn : analyzer.dependences(source)) {
1055                 String repl = replacementFor(cn);
1056                 if (repl != null) {
1057                     jdkinternals.putIfAbsent(cn, repl);
1058                 }
1059             }
1060         }
1061         if (useInternals) {
1062             log.println();
1063             warning("warn.replace.useJDKInternals", getMessage("jdeps.wiki.url"));
1064         }
1065         if (!jdkinternals.isEmpty()) {
1066             log.println();
1067             log.format("%-40s %s%n", "JDK Internal API", "Suggested Replacement");
1068             log.format("%-40s %s%n", "----------------", "---------------------");
1069             for (Map.Entry<String,String> e : jdkinternals.entrySet()) {
1070                 log.format("%-40s %s%n", e.getKey(), e.getValue());
1071             }
1072         }
1073 
1074     }
1075 }