Path: blob/master/src/jdk.jdeps/share/classes/com/sun/tools/jdeps/JdepsTask.java
41161 views
/*1* Copyright (c) 2012, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.tools.jdeps;2627import com.sun.tools.jdeps.Analyzer.Type;28import static com.sun.tools.jdeps.Analyzer.Type.*;29import static com.sun.tools.jdeps.JdepsWriter.*;30import static java.util.stream.Collectors.*;3132import java.io.IOException;33import java.io.PrintWriter;34import java.lang.module.ResolutionException;35import java.nio.file.Files;36import java.nio.file.Path;37import java.nio.file.Paths;38import java.text.MessageFormat;39import java.util.*;40import java.util.jar.JarFile;41import java.util.regex.Pattern;4243/**44* Implementation for the jdeps tool for static class dependency analysis.45*/46class JdepsTask {47static interface BadArguments {48String getKey();49Object[] getArgs();50boolean showUsage();51}52static class BadArgs extends Exception implements BadArguments {53static final long serialVersionUID = 8765093759964640721L;54BadArgs(String key, Object... args) {55super(JdepsTask.getMessage(key, args));56this.key = key;57this.args = args;58}5960BadArgs showUsage(boolean b) {61showUsage = b;62return this;63}64final String key;65final Object[] args;66boolean showUsage;6768@Override69public String getKey() {70return key;71}7273@Override74public Object[] getArgs() {75return args;76}7778@Override79public boolean showUsage() {80return showUsage;81}82}8384static class UncheckedBadArgs extends RuntimeException implements BadArguments {85static final long serialVersionUID = -1L;86final BadArgs cause;87UncheckedBadArgs(BadArgs cause) {88super(cause);89this.cause = cause;90}91@Override92public String getKey() {93return cause.key;94}9596@Override97public Object[] getArgs() {98return cause.args;99}100101@Override102public boolean showUsage() {103return cause.showUsage;104}105}106107static abstract class Option {108Option(boolean hasArg, String... aliases) {109this.hasArg = hasArg;110this.aliases = aliases;111}112113Option(boolean hasArg, CommandOption cmd) {114this(hasArg, cmd.names());115}116117boolean isHidden() {118return false;119}120121boolean matches(String opt) {122for (String a : aliases) {123if (a.equals(opt))124return true;125if (hasArg && opt.startsWith(a + "="))126return true;127}128return false;129}130131boolean ignoreRest() {132return false;133}134135abstract void process(JdepsTask task, String opt, String arg) throws BadArgs;136final boolean hasArg;137final String[] aliases;138}139140static abstract class HiddenOption extends Option {141HiddenOption(boolean hasArg, String... aliases) {142super(hasArg, aliases);143}144145boolean isHidden() {146return true;147}148}149150enum CommandOption {151ANALYZE_DEPS(""),152GENERATE_DOT_FILE("-dotoutput", "--dot-output"),153GENERATE_MODULE_INFO("--generate-module-info"),154GENERATE_OPEN_MODULE("--generate-open-module"),155LIST_DEPS("--list-deps"),156LIST_REDUCED_DEPS("--list-reduced-deps"),157PRINT_MODULE_DEPS("--print-module-deps"),158CHECK_MODULES("--check");159160private final String[] names;161CommandOption(String... names) {162this.names = names;163}164165String[] names() {166return names;167}168169@Override170public String toString() {171return names[0];172}173}174175static Option[] recognizedOptions = {176new Option(false, "-h", "-?", "-help", "--help") {177void process(JdepsTask task, String opt, String arg) {178task.options.help = true;179}180},181new Option(true, CommandOption.GENERATE_DOT_FILE) {182void process(JdepsTask task, String opt, String arg) throws BadArgs {183if (task.command != null) {184throw new BadArgs("err.command.set", task.command, opt);185}186task.command = task.genDotFile(Paths.get(arg));187}188},189new Option(false, "-s", "-summary") {190void process(JdepsTask task, String opt, String arg) {191task.options.showSummary = true;192}193},194new Option(false, "-v", "-verbose",195"-verbose:module",196"-verbose:package",197"-verbose:class") {198void process(JdepsTask task, String opt, String arg) throws BadArgs {199switch (opt) {200case "-v":201case "-verbose":202task.options.verbose = VERBOSE;203task.options.filterSameArchive = false;204task.options.filterSamePackage = false;205break;206case "-verbose:module":207task.options.verbose = MODULE;208break;209case "-verbose:package":210task.options.verbose = PACKAGE;211break;212case "-verbose:class":213task.options.verbose = CLASS;214break;215default:216throw new BadArgs("err.invalid.arg.for.option", opt);217}218}219},220new Option(false, "-apionly", "--api-only") {221void process(JdepsTask task, String opt, String arg) {222task.options.apiOnly = true;223}224},225226new Option(false, "-jdkinternals", "--jdk-internals") {227void process(JdepsTask task, String opt, String arg) {228task.options.findJDKInternals = true;229if (task.options.includePattern == null) {230task.options.includePattern = Pattern.compile(".*");231}232}233},234235// ---- paths option ----236new Option(true, "-cp", "-classpath", "--class-path") {237void process(JdepsTask task, String opt, String arg) {238task.options.classpath = arg;239}240},241new Option(true, "--module-path") {242void process(JdepsTask task, String opt, String arg) throws BadArgs {243task.options.modulePath = arg;244}245},246new Option(true, "--upgrade-module-path") {247void process(JdepsTask task, String opt, String arg) throws BadArgs {248task.options.upgradeModulePath = arg;249}250},251new Option(true, "--system") {252void process(JdepsTask task, String opt, String arg) throws BadArgs {253if (arg.equals("none")) {254task.options.systemModulePath = null;255} else {256Path path = Paths.get(arg);257if (Files.isRegularFile(path.resolve("lib").resolve("modules")))258task.options.systemModulePath = arg;259else260throw new BadArgs("err.invalid.path", arg);261}262}263},264new Option(true, "--add-modules") {265void process(JdepsTask task, String opt, String arg) throws BadArgs {266Set<String> mods = Set.of(arg.split(","));267task.options.addmods.addAll(mods);268}269},270new Option(true, "--multi-release") {271void process(JdepsTask task, String opt, String arg) throws BadArgs {272if (arg.equalsIgnoreCase("base")) {273task.options.multiRelease = JarFile.baseVersion();274} else {275try {276int v = Integer.parseInt(arg);277if (v < 9) {278throw new BadArgs("err.invalid.arg.for.option", arg);279}280} catch (NumberFormatException x) {281throw new BadArgs("err.invalid.arg.for.option", arg);282}283task.options.multiRelease = Runtime.Version.parse(arg);284}285}286},287new Option(false, "-q", "-quiet") {288void process(JdepsTask task, String opt, String arg) {289task.options.nowarning = true;290}291},292new Option(false, "-version", "--version") {293void process(JdepsTask task, String opt, String arg) {294task.options.version = true;295}296},297298// ---- module-specific options ----299300new Option(true, "-m", "--module") {301void process(JdepsTask task, String opt, String arg) throws BadArgs {302if (!task.options.rootModules.isEmpty()) {303throw new BadArgs("err.option.already.specified", opt);304}305task.options.rootModules.add(arg);306task.options.addmods.add(arg);307}308},309new Option(true, CommandOption.GENERATE_MODULE_INFO) {310void process(JdepsTask task, String opt, String arg) throws BadArgs {311if (task.command != null) {312throw new BadArgs("err.command.set", task.command, opt);313}314task.command = task.genModuleInfo(Paths.get(arg), false);315}316},317new Option(true, CommandOption.GENERATE_OPEN_MODULE) {318void process(JdepsTask task, String opt, String arg) throws BadArgs {319if (task.command != null) {320throw new BadArgs("err.command.set", task.command, opt);321}322task.command = task.genModuleInfo(Paths.get(arg), true);323}324},325new Option(true, CommandOption.CHECK_MODULES) {326void process(JdepsTask task, String opt, String arg) throws BadArgs {327if (task.command != null) {328throw new BadArgs("err.command.set", task.command, opt);329}330Set<String> mods = Set.of(arg.split(","));331task.options.addmods.addAll(mods);332task.command = task.checkModuleDeps(mods);333}334},335new Option(false, CommandOption.LIST_DEPS) {336void process(JdepsTask task, String opt, String arg) throws BadArgs {337if (task.command != null) {338throw new BadArgs("err.command.set", task.command, opt);339}340task.command = task.listModuleDeps(CommandOption.LIST_DEPS);341}342},343new Option(false, CommandOption.LIST_REDUCED_DEPS) {344void process(JdepsTask task, String opt, String arg) throws BadArgs {345if (task.command != null) {346throw new BadArgs("err.command.set", task.command, opt);347}348task.command = task.listModuleDeps(CommandOption.LIST_REDUCED_DEPS);349}350},351new Option(false, CommandOption.PRINT_MODULE_DEPS) {352void process(JdepsTask task, String opt, String arg) throws BadArgs {353if (task.command != null) {354throw new BadArgs("err.command.set", task.command, opt);355}356task.command = task.listModuleDeps(CommandOption.PRINT_MODULE_DEPS);357}358},359new Option(false, "--ignore-missing-deps") {360void process(JdepsTask task, String opt, String arg) {361task.options.ignoreMissingDeps = true;362}363},364365// ---- Target filtering options ----366new Option(true, "-p", "-package", "--package") {367void process(JdepsTask task, String opt, String arg) {368task.options.packageNames.add(arg);369}370},371new Option(true, "-e", "-regex", "--regex") {372void process(JdepsTask task, String opt, String arg) {373task.options.regex = Pattern.compile(arg);374}375},376new Option(true, "--require") {377void process(JdepsTask task, String opt, String arg) {378task.options.requires.add(arg);379task.options.addmods.add(arg);380}381},382new Option(true, "-f", "-filter") {383void process(JdepsTask task, String opt, String arg) {384task.options.filterRegex = Pattern.compile(arg);385}386},387new Option(false, "-filter:package",388"-filter:archive", "-filter:module",389"-filter:none") {390void process(JdepsTask task, String opt, String arg) {391switch (opt) {392case "-filter:package":393task.options.filterSamePackage = true;394task.options.filterSameArchive = false;395break;396case "-filter:archive":397case "-filter:module":398task.options.filterSameArchive = true;399task.options.filterSamePackage = false;400break;401case "-filter:none":402task.options.filterSameArchive = false;403task.options.filterSamePackage = false;404break;405}406}407},408new Option(false, "--missing-deps") {409void process(JdepsTask task, String opt, String arg) {410task.options.findMissingDeps = true;411}412},413414// ---- Source filtering options ----415new Option(true, "-include") {416void process(JdepsTask task, String opt, String arg) throws BadArgs {417task.options.includePattern = Pattern.compile(arg);418}419},420421new Option(false, "-P", "-profile") {422void process(JdepsTask task, String opt, String arg) throws BadArgs {423task.options.showProfile = true;424}425},426427new Option(false, "-R", "-recursive", "--recursive") {428void process(JdepsTask task, String opt, String arg) throws BadArgs {429task.options.recursive = Options.RECURSIVE;430// turn off filtering431task.options.filterSameArchive = false;432task.options.filterSamePackage = false;433}434},435new Option(false, "--no-recursive") {436void process(JdepsTask task, String opt, String arg) throws BadArgs {437task.options.recursive = Options.NO_RECURSIVE;438}439},440new Option(false, "-I", "--inverse") {441void process(JdepsTask task, String opt, String arg) {442task.options.inverse = true;443// equivalent to the inverse of compile-time view analysis444task.options.compileTimeView = true;445task.options.filterSamePackage = true;446task.options.filterSameArchive = true;447}448},449450new Option(false, "--compile-time") {451void process(JdepsTask task, String opt, String arg) {452task.options.compileTimeView = true;453task.options.recursive = Options.RECURSIVE;454task.options.filterSamePackage = true;455task.options.filterSameArchive = true;456}457},458459new HiddenOption(false, "-fullversion") {460void process(JdepsTask task, String opt, String arg) {461task.options.fullVersion = true;462}463},464new HiddenOption(false, "-showlabel") {465void process(JdepsTask task, String opt, String arg) {466task.options.showLabel = true;467}468},469new HiddenOption(false, "--hide-show-module") {470void process(JdepsTask task, String opt, String arg) {471task.options.showModule = false;472}473},474new HiddenOption(true, "-depth") {475void process(JdepsTask task, String opt, String arg) throws BadArgs {476try {477task.options.depth = Integer.parseInt(arg);478} catch (NumberFormatException e) {479throw new BadArgs("err.invalid.arg.for.option", opt);480}481}482},483};484485private static final String PROGNAME = "jdeps";486private final Options options = new Options();487private final List<String> inputArgs = new ArrayList<>();488489private Command command;490private PrintWriter log;491void setLog(PrintWriter out) {492log = out;493}494495/**496* Result codes.497*/498static final int EXIT_OK = 0, // Completed with no errors.499EXIT_ERROR = 1, // Completed but reported errors.500EXIT_CMDERR = 2, // Bad command-line arguments501EXIT_SYSERR = 3, // System error or resource exhaustion.502EXIT_ABNORMAL = 4; // terminated abnormally503504int run(String... args) {505if (log == null) {506log = new PrintWriter(System.out);507}508try {509handleOptions(args);510if (options.help) {511showHelp();512}513if (options.version || options.fullVersion) {514showVersion(options.fullVersion);515}516if (options.help || options.version || options.fullVersion) {517return EXIT_OK;518}519if (options.numFilters() > 1) {520reportError("err.invalid.filters");521return EXIT_CMDERR;522}523524// default command to analyze dependences525if (command == null) {526command = analyzeDeps();527}528if (!command.checkOptions()) {529return EXIT_CMDERR;530}531532boolean ok = run();533return ok ? EXIT_OK : EXIT_ERROR;534535} catch (BadArgs|UncheckedBadArgs e) {536reportError(e.getKey(), e.getArgs());537if (e.showUsage()) {538log.println(getMessage("main.usage.summary", PROGNAME));539}540return EXIT_CMDERR;541} catch (ResolutionException e) {542reportError("err.exception.message", e.getMessage());543return EXIT_CMDERR;544} catch (IOException e) {545e.printStackTrace();546return EXIT_CMDERR;547} catch (MultiReleaseException e) {548reportError(e.getKey(), e.getParams());549return EXIT_CMDERR; // could be EXIT_ABNORMAL sometimes550} finally {551log.flush();552}553}554555boolean run() throws IOException {556try (JdepsConfiguration config = buildConfig()) {557if (!options.nowarning) {558// detect split packages559config.splitPackages().entrySet()560.stream()561.sorted(Map.Entry.comparingByKey())562.forEach(e -> warning("warn.split.package",563e.getKey(),564e.getValue().stream().collect(joining(" "))));565}566567// check if any module specified in --add-modules, --require, and -m is missing568options.addmods.stream()569.filter(mn -> !JdepsConfiguration.isToken(mn))570.forEach(mn -> config.findModule(mn).orElseThrow(() ->571new UncheckedBadArgs(new BadArgs("err.module.not.found", mn))));572573return command.run(config);574}575}576577private JdepsConfiguration buildConfig() throws IOException {578JdepsConfiguration.Builder builder =579new JdepsConfiguration.Builder(options.systemModulePath);580581builder.upgradeModulePath(options.upgradeModulePath)582.appModulePath(options.modulePath)583.addmods(options.addmods)584.addmods(command.addModules());585586if (options.classpath != null)587builder.addClassPath(options.classpath);588589if (options.multiRelease != null)590builder.multiRelease(options.multiRelease);591592// build the root set of archives to be analyzed593for (String s : inputArgs) {594Path p = Paths.get(s);595if (Files.exists(p)) {596builder.addRoot(p);597} else {598warning("warn.invalid.arg", s);599}600}601602return builder.build();603}604605// ---- factory methods to create a Command606607private AnalyzeDeps analyzeDeps() throws BadArgs {608return options.inverse ? new InverseAnalyzeDeps()609: new AnalyzeDeps();610}611612private GenDotFile genDotFile(Path dir) throws BadArgs {613if (Files.exists(dir) && (!Files.isDirectory(dir) || !Files.isWritable(dir))) {614throw new BadArgs("err.invalid.path", dir.toString());615}616return new GenDotFile(dir);617}618619private GenModuleInfo genModuleInfo(Path dir, boolean openModule) throws BadArgs {620if (Files.exists(dir) && (!Files.isDirectory(dir) || !Files.isWritable(dir))) {621throw new BadArgs("err.invalid.path", dir.toString());622}623return new GenModuleInfo(dir, openModule);624}625626private ListModuleDeps listModuleDeps(CommandOption option) throws BadArgs {627// do transitive dependence analysis unless --no-recursive is set628if (options.recursive != Options.NO_RECURSIVE) {629options.recursive = Options.RECURSIVE;630}631// no need to record the dependences on the same archive or same package632options.filterSameArchive = true;633options.filterSamePackage = true;634switch (option) {635case LIST_DEPS:636return new ListModuleDeps(option, true, false);637case LIST_REDUCED_DEPS:638return new ListModuleDeps(option, true, true);639case PRINT_MODULE_DEPS:640return new ListModuleDeps(option, false, true, ",");641default:642throw new IllegalArgumentException(option.toString());643}644}645646private CheckModuleDeps checkModuleDeps(Set<String> mods) throws BadArgs {647return new CheckModuleDeps(mods);648}649650abstract class Command {651final CommandOption option;652protected Command(CommandOption option) {653this.option = option;654}655656/**657* Returns true if the command-line options are all valid;658* otherwise, returns false.659*/660abstract boolean checkOptions();661662/**663* Do analysis664*/665abstract boolean run(JdepsConfiguration config) throws IOException;666667/**668* Includes all modules on system module path and application module path669*670* When a named module is analyzed, it will analyze the dependences671* only. The method should be overridden when this command should672* analyze all modules instead.673*/674Set<String> addModules() {675return Set.of();676}677678@Override679public String toString() {680return option.toString();681}682}683684685/**686* Analyze dependences687*/688class AnalyzeDeps extends Command {689JdepsWriter writer;690AnalyzeDeps() {691this(CommandOption.ANALYZE_DEPS);692}693694AnalyzeDeps(CommandOption option) {695super(option);696}697698@Override699boolean checkOptions() {700if (options.findJDKInternals || options.findMissingDeps) {701// cannot set any filter, -verbose and -summary option702if (options.showSummary || options.verbose != null) {703reportError("err.invalid.options", "-summary or -verbose",704options.findJDKInternals ? "-jdkinternals" : "--missing-deps");705return false;706}707if (options.hasFilter()) {708reportError("err.invalid.options", "--package, --regex, --require",709options.findJDKInternals ? "-jdkinternals" : "--missing-deps");710return false;711}712}713if (options.showSummary) {714// -summary cannot use with -verbose option715if (options.verbose != null) {716reportError("err.invalid.options", "-v, -verbose", "-s, -summary");717return false;718}719}720721if (!inputArgs.isEmpty() && !options.rootModules.isEmpty()) {722reportError("err.invalid.arg.for.option", "-m");723}724if (inputArgs.isEmpty() && !options.hasSourcePath()) {725showHelp();726return false;727}728return true;729}730731/*732* Default is to show package-level dependencies733*/734Type getAnalyzerType() {735if (options.showSummary)736return Type.SUMMARY;737738if (options.findJDKInternals || options.findMissingDeps)739return Type.CLASS;740741// default to package-level verbose742return options.verbose != null ? options.verbose : PACKAGE;743}744745@Override746boolean run(JdepsConfiguration config) throws IOException {747Type type = getAnalyzerType();748// default to package-level verbose749JdepsWriter writer = new SimpleWriter(log,750type,751options.showProfile,752options.showModule);753754return run(config, writer, type);755}756757boolean run(JdepsConfiguration config, JdepsWriter writer, Type type)758throws IOException759{760// analyze the dependencies761DepsAnalyzer analyzer = new DepsAnalyzer(config,762dependencyFilter(config),763writer,764type,765options.apiOnly);766767boolean ok = analyzer.run(options.compileTimeView, options.depth());768769// print skipped entries, if any770if (!options.nowarning) {771analyzer.archives()772.forEach(archive -> archive.reader()773.skippedEntries().stream()774.forEach(name -> warning("warn.skipped.entry", name)));775}776777if (options.findJDKInternals && !options.nowarning) {778Map<String, String> jdkInternals = new TreeMap<>();779Set<String> deps = analyzer.dependences();780// find the ones with replacement781deps.forEach(cn -> replacementFor(cn).ifPresent(782repl -> jdkInternals.put(cn, repl))783);784785if (!deps.isEmpty()) {786log.println();787warning("warn.replace.useJDKInternals", getMessage("jdeps.wiki.url"));788}789790if (!jdkInternals.isEmpty()) {791log.println();792String internalApiTitle = getMessage("internal.api.column.header");793String replacementApiTitle = getMessage("public.api.replacement.column.header");794log.format("%-40s %s%n", internalApiTitle, replacementApiTitle);795log.format("%-40s %s%n",796internalApiTitle.replaceAll(".", "-"),797replacementApiTitle.replaceAll(".", "-"));798jdkInternals.entrySet().stream()799.forEach(e -> {800String key = e.getKey();801String[] lines = e.getValue().split("\\n");802for (String s : lines) {803log.format("%-40s %s%n", key, s);804key = "";805}806});807}808}809return ok;810}811}812813814class InverseAnalyzeDeps extends AnalyzeDeps {815InverseAnalyzeDeps() {816}817818@Override819boolean checkOptions() {820if (options.recursive != -1 || options.depth != -1) {821reportError("err.invalid.options", "--recursive and --no-recursive", "--inverse");822return false;823}824825if (options.numFilters() == 0) {826reportError("err.filter.not.specified");827return false;828}829830if (!super.checkOptions()) {831return false;832}833834return true;835}836837@Override838boolean run(JdepsConfiguration config) throws IOException {839Type type = getAnalyzerType();840841InverseDepsAnalyzer analyzer =842new InverseDepsAnalyzer(config,843dependencyFilter(config),844writer,845type,846options.apiOnly);847boolean ok = analyzer.run();848849log.println();850if (!options.requires.isEmpty())851log.println(getMessage("inverse.transitive.dependencies.on",852options.requires));853else854log.println(getMessage("inverse.transitive.dependencies.matching",855options.regex != null856? options.regex.toString()857: "packages " + options.packageNames));858859analyzer.inverseDependences()860.stream()861.sorted(comparator())862.map(this::toInversePath)863.forEach(log::println);864return ok;865}866867private String toInversePath(Deque<Archive> path) {868return path.stream()869.map(Archive::getName)870.collect(joining(" <- "));871}872873/*874* Returns a comparator for sorting the inversed path, grouped by875* the first module name, then the shortest path and then sort by876* the module names of each path877*/878private Comparator<Deque<Archive>> comparator() {879return Comparator.<Deque<Archive>, String>880comparing(deque -> deque.peekFirst().getName())881.thenComparingInt(Deque::size)882.thenComparing(this::toInversePath);883}884885/*886* Returns true if --require is specified so that all modules are887* analyzed to find all modules that depend on the modules specified in the888* --require option directly and indirectly889*/890Set<String> addModules() {891return options.requires.size() > 0 ? Set.of("ALL-SYSTEM") : Set.of();892}893}894895896class GenModuleInfo extends Command {897final Path dir;898final boolean openModule;899GenModuleInfo(Path dir, boolean openModule) {900super(CommandOption.GENERATE_MODULE_INFO);901this.dir = dir;902this.openModule = openModule;903}904905@Override906boolean checkOptions() {907if (options.classpath != null) {908reportError("err.invalid.options", "-classpath",909option);910return false;911}912if (options.hasFilter()) {913reportError("err.invalid.options", "--package, --regex, --require",914option);915return false;916}917if (!options.rootModules.isEmpty()) {918reportError("err.invalid.options", "-m or --module",919option);920return false;921}922return true;923}924925@Override926boolean run(JdepsConfiguration config) throws IOException {927// check if any JAR file contains unnamed package928for (String arg : inputArgs) {929try (ClassFileReader reader = ClassFileReader.newInstance(Paths.get(arg), config.getVersion())) {930Optional<String> classInUnnamedPackage =931reader.entries().stream()932.filter(n -> n.endsWith(".class"))933.filter(cn -> toPackageName(cn).isEmpty())934.findFirst();935936if (classInUnnamedPackage.isPresent()) {937if (classInUnnamedPackage.get().equals("module-info.class")) {938reportError("err.genmoduleinfo.not.jarfile", arg);939} else {940reportError("err.genmoduleinfo.unnamed.package", arg);941}942return false;943}944}945}946947ModuleInfoBuilder builder948= new ModuleInfoBuilder(config, inputArgs, dir, openModule);949boolean ok = builder.run(options.ignoreMissingDeps, log, options.nowarning);950if (!ok) {951reportError("err.missing.dependences");952log.println();953builder.visitMissingDeps(new SimpleDepVisitor());954}955return ok;956}957958private String toPackageName(String name) {959int i = name.lastIndexOf('/');960return i > 0 ? name.replace('/', '.').substring(0, i) : "";961}962}963964class CheckModuleDeps extends Command {965final Set<String> modules;966CheckModuleDeps(Set<String> mods) {967super(CommandOption.CHECK_MODULES);968this.modules = mods;969}970971@Override972boolean checkOptions() {973if (!inputArgs.isEmpty()) {974reportError("err.invalid.options", inputArgs, "--check");975return false;976}977return true;978}979980@Override981boolean run(JdepsConfiguration config) throws IOException {982if (!config.initialArchives().isEmpty()) {983String list = config.initialArchives().stream()984.map(Archive::getPathName).collect(joining(" "));985throw new UncheckedBadArgs(new BadArgs("err.invalid.options",986list, "--check"));987}988return new ModuleAnalyzer(config, log, modules).run(options.ignoreMissingDeps);989}990991/*992* Returns true to analyze all modules993*/994Set<String> addModules() {995return Set.of("ALL-SYSTEM", "ALL-MODULE-PATH");996}997}998999class ListModuleDeps extends Command {1000final boolean jdkinternals;1001final boolean reduced;1002final String separator;1003ListModuleDeps(CommandOption option, boolean jdkinternals, boolean reduced) {1004this(option, jdkinternals, reduced, System.getProperty("line.separator"));1005}1006ListModuleDeps(CommandOption option, boolean jdkinternals, boolean reduced, String sep) {1007super(option);1008this.jdkinternals = jdkinternals;1009this.reduced = reduced;1010this.separator = sep;1011}10121013@Override1014boolean checkOptions() {1015if (options.showSummary || options.verbose != null) {1016reportError("err.invalid.options", "-summary or -verbose", option);1017return false;1018}1019if (options.findJDKInternals) {1020reportError("err.invalid.options", "-jdkinternals", option);1021return false;1022}1023if (options.findMissingDeps) {1024reportError("err.invalid.options", "--missing-deps", option);1025return false;1026}10271028if (!inputArgs.isEmpty() && !options.rootModules.isEmpty()) {1029reportError("err.invalid.arg.for.option", "-m");1030}1031if (inputArgs.isEmpty() && !options.hasSourcePath()) {1032showHelp();1033return false;1034}1035return true;1036}10371038@Override1039boolean run(JdepsConfiguration config) throws IOException {1040ModuleExportsAnalyzer analyzer = new ModuleExportsAnalyzer(config,1041dependencyFilter(config),1042jdkinternals,1043reduced,1044log,1045separator);1046boolean ok = analyzer.run(options.depth(), options.ignoreMissingDeps);1047if (!ok) {1048reportError("err.missing.dependences");1049log.println();1050analyzer.visitMissingDeps(new SimpleDepVisitor());1051}1052return ok;1053}1054}10551056class GenDotFile extends AnalyzeDeps {1057final Path dotOutputDir;1058GenDotFile(Path dotOutputDir) {1059super(CommandOption.GENERATE_DOT_FILE);10601061this.dotOutputDir = dotOutputDir;1062}10631064@Override1065boolean run(JdepsConfiguration config) throws IOException {1066if ((options.showSummary || options.verbose == MODULE) &&1067!options.addmods.isEmpty() && inputArgs.isEmpty()) {1068// generate dot graph from the resolved graph from module1069// resolution. No class dependency analysis is performed.1070return new ModuleDotGraph(config, options.apiOnly)1071.genDotFiles(dotOutputDir);1072}10731074Type type = getAnalyzerType();1075JdepsWriter writer = new DotFileWriter(dotOutputDir,1076type,1077options.showProfile,1078options.showModule,1079options.showLabel);1080return run(config, writer, type);1081}1082}10831084class SimpleDepVisitor implements Analyzer.Visitor {1085private Archive source;1086@Override1087public void visitDependence(String origin, Archive originArchive, String target, Archive targetArchive) {1088if (source != originArchive) {1089source = originArchive;1090log.format("%s%n", originArchive);1091}1092log.format(" %-50s -> %-50s %s%n", origin, target, targetArchive.getName());1093}1094}10951096/**1097* Returns a filter used during dependency analysis1098*/1099private JdepsFilter dependencyFilter(JdepsConfiguration config) {1100// Filter specified by -filter, -package, -regex, and --require options1101JdepsFilter.Builder builder = new JdepsFilter.Builder();11021103// source filters1104builder.includePattern(options.includePattern);11051106// target filters1107builder.filter(options.filterSamePackage, options.filterSameArchive);1108builder.findJDKInternals(options.findJDKInternals);1109builder.findMissingDeps(options.findMissingDeps);11101111// --require1112if (!options.requires.isEmpty()) {1113options.requires.stream()1114.forEach(mn -> {1115Module m = config.findModule(mn).get();1116builder.requires(mn, m.packages());1117});1118}1119// -regex1120if (options.regex != null)1121builder.regex(options.regex);1122// -package1123if (!options.packageNames.isEmpty())1124builder.packages(options.packageNames);1125// -filter1126if (options.filterRegex != null)1127builder.filter(options.filterRegex);11281129return builder.build();1130}11311132public void handleOptions(String[] args) throws BadArgs {1133// process options1134for (int i=0; i < args.length; i++) {1135if (args[i].charAt(0) == '-') {1136String name = args[i];1137Option option = getOption(name);1138String param = null;1139if (option.hasArg) {1140if (name.startsWith("-") && name.indexOf('=') > 0) {1141param = name.substring(name.indexOf('=') + 1, name.length());1142} else if (i + 1 < args.length) {1143param = args[++i];1144}1145if (param == null || param.isEmpty() || param.charAt(0) == '-') {1146throw new BadArgs("err.missing.arg", name).showUsage(true);1147}1148}1149option.process(this, name, param);1150if (option.ignoreRest()) {1151i = args.length;1152}1153} else {1154// process rest of the input arguments1155for (; i < args.length; i++) {1156String name = args[i];1157if (name.charAt(0) == '-') {1158throw new BadArgs("err.option.after.class", name).showUsage(true);1159}1160inputArgs.add(name);1161}1162}1163}1164}11651166private Option getOption(String name) throws BadArgs {1167for (Option o : recognizedOptions) {1168if (o.matches(name)) {1169return o;1170}1171}1172throw new BadArgs("err.unknown.option", name).showUsage(true);1173}11741175private void reportError(String key, Object... args) {1176log.println(getMessage("error.prefix") + " " + getMessage(key, args));1177}11781179void warning(String key, Object... args) {1180log.println(getMessage("warn.prefix") + " " + getMessage(key, args));1181}11821183private void showHelp() {1184log.println(getMessage("main.usage", PROGNAME));1185for (Option o : recognizedOptions) {1186String name = o.aliases[0].substring(1); // there must always be at least one name1187name = name.charAt(0) == '-' ? name.substring(1) : name;1188if (o.isHidden() || name.startsWith("filter:")) {1189continue;1190}1191log.println(getMessage("main.opt." + name));1192}1193}11941195private void showVersion(boolean full) {1196log.println(version(full ? "full" : "release"));1197}11981199private String version(String key) {1200// key=version: mm.nn.oo[-milestone]1201// key=full: mm.mm.oo[-milestone]-build1202try {1203return ResourceBundleHelper.getVersion(key);1204} catch (MissingResourceException e) {1205return getMessage("version.unknown", System.getProperty("java.version"));1206}1207}12081209static String getMessage(String key, Object... args) {1210try {1211return MessageFormat.format(ResourceBundleHelper.getMessage(key), args);1212} catch (MissingResourceException e) {1213throw new InternalError("Missing message: " + key);1214}1215}12161217private static class Options {1218static final int NO_RECURSIVE = 0;1219static final int RECURSIVE = 1;1220boolean help;1221boolean version;1222boolean fullVersion;1223boolean showProfile;1224boolean showModule = true;1225boolean showSummary;1226boolean apiOnly;1227boolean showLabel;1228boolean findJDKInternals;1229boolean findMissingDeps;1230boolean ignoreMissingDeps;1231boolean nowarning = false;1232Analyzer.Type verbose;1233// default filter references from same package1234boolean filterSamePackage = true;1235boolean filterSameArchive = false;1236Pattern filterRegex;1237String classpath;1238int recursive = -1; // 0: --no-recursive, 1: --recursive1239int depth = -1;1240Set<String> requires = new HashSet<>();1241Set<String> packageNames = new HashSet<>();1242Pattern regex; // apply to the dependences1243Pattern includePattern;1244boolean inverse = false;1245boolean compileTimeView = false;1246String systemModulePath = System.getProperty("java.home");1247String upgradeModulePath;1248String modulePath;1249Set<String> rootModules = new HashSet<>();1250Set<String> addmods = new HashSet<>();1251Runtime.Version multiRelease;12521253boolean hasSourcePath() {1254return !addmods.isEmpty() || includePattern != null;1255}12561257boolean hasFilter() {1258return numFilters() > 0;1259}12601261int numFilters() {1262int count = 0;1263if (requires.size() > 0) count++;1264if (regex != null) count++;1265if (packageNames.size() > 0) count++;1266return count;1267}12681269int depth() {1270// ignore -depth if --no-recursive is set1271if (recursive == NO_RECURSIVE)1272return 1;12731274// depth == 0 if recursive1275if (recursive == RECURSIVE && depth == -1)1276return 0;12771278// default depth is 1 unless specified via -depth option1279return depth == -1 ? 1 : depth;1280}1281}12821283private static class ResourceBundleHelper {1284static final String LS = System.lineSeparator();1285static final ResourceBundle versionRB;1286static final ResourceBundle bundle;1287static final ResourceBundle jdkinternals;12881289static {1290Locale locale = Locale.getDefault();1291try {1292bundle = ResourceBundle.getBundle("com.sun.tools.jdeps.resources.jdeps", locale);1293} catch (MissingResourceException e) {1294throw new InternalError("Cannot find jdeps resource bundle for locale " + locale);1295}1296try {1297versionRB = ResourceBundle.getBundle("com.sun.tools.jdeps.resources.version");1298} catch (MissingResourceException e) {1299throw new InternalError("version.resource.missing");1300}1301try {1302jdkinternals = ResourceBundle.getBundle("com.sun.tools.jdeps.resources.jdkinternals");1303} catch (MissingResourceException e) {1304throw new InternalError("Cannot find jdkinternals resource bundle");1305}1306}13071308static String getMessage(String key) {1309return bundle.getString(key).replace("\n", LS);1310}13111312static String getVersion(String key) {1313if (ResourceBundleHelper.versionRB == null) {1314return System.getProperty("java.version");1315}1316return versionRB.getString(key).replace("\n", LS);1317}13181319static String getSuggestedReplacement(String key) {1320return ResourceBundleHelper.jdkinternals.getString(key).replace("\n", LS);1321}1322}13231324/**1325* Returns the recommended replacement API for the given classname;1326* or return null if replacement API is not known.1327*/1328private Optional<String> replacementFor(String cn) {1329String name = cn;1330String value = null;1331while (value == null && name != null) {1332try {1333value = ResourceBundleHelper.getSuggestedReplacement(name);1334} catch (MissingResourceException e) {1335// go up one subpackage level1336int i = name.lastIndexOf('.');1337name = i > 0 ? name.substring(0, i) : null;1338}1339}1340return Optional.ofNullable(value);1341}1342}134313441345