36 import java.nio.file.Files; |
36 import java.nio.file.Files; |
37 import java.nio.file.Path; |
37 import java.nio.file.Path; |
38 import java.nio.file.Paths; |
38 import java.nio.file.Paths; |
39 import java.text.MessageFormat; |
39 import java.text.MessageFormat; |
40 import java.util.*; |
40 import java.util.*; |
41 import java.util.function.Function; |
|
42 import java.util.function.ToIntFunction; |
|
43 import java.util.jar.JarFile; |
41 import java.util.jar.JarFile; |
44 import java.util.regex.Pattern; |
42 import java.util.regex.Pattern; |
45 |
43 |
46 /** |
44 /** |
47 * Implementation for the jdeps tool for static class dependency analysis. |
45 * Implementation for the jdeps tool for static class dependency analysis. |
155 GENERATE_DOT_FILE("-dotoutput", "--dot-output"), |
153 GENERATE_DOT_FILE("-dotoutput", "--dot-output"), |
156 GENERATE_MODULE_INFO("--generate-module-info"), |
154 GENERATE_MODULE_INFO("--generate-module-info"), |
157 GENERATE_OPEN_MODULE("--generate-open-module"), |
155 GENERATE_OPEN_MODULE("--generate-open-module"), |
158 LIST_DEPS("--list-deps"), |
156 LIST_DEPS("--list-deps"), |
159 LIST_REDUCED_DEPS("--list-reduced-deps"), |
157 LIST_REDUCED_DEPS("--list-reduced-deps"), |
|
158 PRINT_MODULE_DEPS("--print-module-deps"), |
160 CHECK_MODULES("--check"); |
159 CHECK_MODULES("--check"); |
161 |
160 |
162 private final String[] names; |
161 private final String[] names; |
163 CommandOption(String... names) { |
162 CommandOption(String... names) { |
164 this.names = names; |
163 this.names = names; |
337 new Option(false, CommandOption.LIST_DEPS) { |
336 new Option(false, CommandOption.LIST_DEPS) { |
338 void process(JdepsTask task, String opt, String arg) throws BadArgs { |
337 void process(JdepsTask task, String opt, String arg) throws BadArgs { |
339 if (task.command != null) { |
338 if (task.command != null) { |
340 throw new BadArgs("err.command.set", task.command, opt); |
339 throw new BadArgs("err.command.set", task.command, opt); |
341 } |
340 } |
342 task.command = task.listModuleDeps(false); |
341 task.command = task.listModuleDeps(CommandOption.LIST_DEPS); |
343 } |
342 } |
344 }, |
343 }, |
345 new Option(false, CommandOption.LIST_REDUCED_DEPS) { |
344 new Option(false, CommandOption.LIST_REDUCED_DEPS) { |
346 void process(JdepsTask task, String opt, String arg) throws BadArgs { |
345 void process(JdepsTask task, String opt, String arg) throws BadArgs { |
347 if (task.command != null) { |
346 if (task.command != null) { |
348 throw new BadArgs("err.command.set", task.command, opt); |
347 throw new BadArgs("err.command.set", task.command, opt); |
349 } |
348 } |
350 task.command = task.listModuleDeps(true); |
349 task.command = task.listModuleDeps(CommandOption.LIST_REDUCED_DEPS); |
|
350 } |
|
351 }, |
|
352 new Option(false, CommandOption.PRINT_MODULE_DEPS) { |
|
353 void process(JdepsTask task, String opt, String arg) throws BadArgs { |
|
354 if (task.command != null) { |
|
355 throw new BadArgs("err.command.set", task.command, opt); |
|
356 } |
|
357 task.command = task.listModuleDeps(CommandOption.PRINT_MODULE_DEPS); |
351 } |
358 } |
352 }, |
359 }, |
353 |
360 |
354 // ---- Target filtering options ---- |
361 // ---- Target filtering options ---- |
355 new Option(true, "-p", "-package", "--package") { |
362 new Option(true, "-p", "-package", "--package") { |
532 } |
539 } |
533 } |
540 } |
534 |
541 |
535 boolean run() throws IOException { |
542 boolean run() throws IOException { |
536 try (JdepsConfiguration config = buildConfig(command.allModules())) { |
543 try (JdepsConfiguration config = buildConfig(command.allModules())) { |
537 |
544 if (!options.nowarning) { |
538 // detect split packages |
545 // detect split packages |
539 config.splitPackages().entrySet() |
546 config.splitPackages().entrySet() |
540 .stream() |
547 .stream() |
541 .sorted(Map.Entry.comparingByKey()) |
548 .sorted(Map.Entry.comparingByKey()) |
542 .forEach(e -> log.println(getMessage("split.package", |
549 .forEach(e -> warning("warn.split.package", |
543 e.getKey(), |
550 e.getKey(), |
544 e.getValue().toString()))); |
551 e.getValue().stream().collect(joining(" ")))); |
|
552 } |
545 |
553 |
546 // check if any module specified in --add-modules, --require, and -m is missing |
554 // check if any module specified in --add-modules, --require, and -m is missing |
547 options.addmods.stream() |
555 options.addmods.stream() |
548 .filter(mn -> !config.isValidToken(mn)) |
556 .filter(mn -> !config.isValidToken(mn)) |
549 .forEach(mn -> config.findModule(mn).orElseThrow(() -> |
557 .forEach(mn -> config.findModule(mn).orElseThrow(() -> |
604 throw new BadArgs("err.invalid.path", dir.toString()); |
612 throw new BadArgs("err.invalid.path", dir.toString()); |
605 } |
613 } |
606 return new GenModuleInfo(dir, openModule); |
614 return new GenModuleInfo(dir, openModule); |
607 } |
615 } |
608 |
616 |
609 private ListModuleDeps listModuleDeps(boolean reduced) throws BadArgs { |
617 private ListModuleDeps listModuleDeps(CommandOption option) throws BadArgs { |
610 return reduced ? new ListReducedDeps() |
618 switch (option) { |
611 : new ListModuleDeps(); |
619 case LIST_DEPS: |
|
620 return new ListModuleDeps(option, true, false); |
|
621 case LIST_REDUCED_DEPS: |
|
622 return new ListModuleDeps(option, true, true); |
|
623 case PRINT_MODULE_DEPS: |
|
624 return new ListModuleDeps(option, false, true, ","); |
|
625 default: |
|
626 throw new IllegalArgumentException(option.toString()); |
|
627 } |
612 } |
628 } |
613 |
629 |
614 private CheckModuleDeps checkModuleDeps(Set<String> mods) throws BadArgs { |
630 private CheckModuleDeps checkModuleDeps(Set<String> mods) throws BadArgs { |
615 return new CheckModuleDeps(mods); |
631 return new CheckModuleDeps(mods); |
616 } |
632 } |
962 public boolean allModules() { |
978 public boolean allModules() { |
963 return true; |
979 return true; |
964 } |
980 } |
965 } |
981 } |
966 |
982 |
967 class ListReducedDeps extends ListModuleDeps { |
|
968 ListReducedDeps() { |
|
969 super(CommandOption.LIST_REDUCED_DEPS, true); |
|
970 } |
|
971 } |
|
972 |
|
973 class ListModuleDeps extends Command { |
983 class ListModuleDeps extends Command { |
|
984 final boolean jdkinternals; |
974 final boolean reduced; |
985 final boolean reduced; |
975 ListModuleDeps() { |
986 final String separator; |
976 this(CommandOption.LIST_DEPS, false); |
987 ListModuleDeps(CommandOption option, boolean jdkinternals, boolean reduced) { |
977 } |
988 this(option, jdkinternals, reduced, System.getProperty("line.separator")); |
978 ListModuleDeps(CommandOption option, boolean reduced) { |
989 } |
|
990 ListModuleDeps(CommandOption option, boolean jdkinternals, boolean reduced, String sep) { |
979 super(option); |
991 super(option); |
|
992 this.jdkinternals = jdkinternals; |
980 this.reduced = reduced; |
993 this.reduced = reduced; |
|
994 this.separator = sep; |
981 } |
995 } |
982 |
996 |
983 @Override |
997 @Override |
984 boolean checkOptions() { |
998 boolean checkOptions() { |
985 if (options.showSummary || options.verbose != null) { |
999 if (options.showSummary || options.verbose != null) { |