langtools/src/jdk.jdeps/share/classes/com/sun/tools/jdeps/JdepsTask.java
author mchung
Thu, 29 Sep 2016 21:31:09 -0700
changeset 41251 6112540cd0c1
parent 41164 69167c89e68f
child 41442 14db641d4a9f
permissions -rw-r--r--
8166238: Update jdeps for GNU-style long form options Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
     1
/*
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
     2
 * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved.
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
     4
 *
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    10
 *
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    15
 * accompanied this code).
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    16
 *
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    20
 *
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    23
 * questions.
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    24
 */
34752
9c262a013456 8145342: Some copyright notices are inconsistently and ill formatted
vasya
parents: 30846
diff changeset
    25
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    26
package com.sun.tools.jdeps;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    27
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
    28
import static com.sun.tools.jdeps.Analyzer.Type.*;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
    29
import static com.sun.tools.jdeps.JdepsWriter.*;
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
    30
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
    31
import java.io.IOException;
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
    32
import java.io.PrintWriter;
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
    33
import java.lang.module.ResolutionException;
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
    34
import java.nio.file.Files;
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
    35
import java.nio.file.Path;
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
    36
import java.nio.file.Paths;
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    37
import java.text.MessageFormat;
39101
fd8a6392b7ea 8159524: jdeps -jdkinternals throws NPE when no replacement is known
mchung
parents: 38532
diff changeset
    38
import java.util.*;
41164
69167c89e68f 8153654: Update jdeps to be multi-release jar aware
sdrach
parents: 40762
diff changeset
    39
import java.util.jar.JarFile;
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    40
import java.util.regex.Pattern;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
    41
import java.util.stream.Collectors;
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
    42
import java.util.stream.Stream;
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    43
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    44
/**
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    45
 * Implementation for the jdeps tool for static class dependency analysis.
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    46
 */
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    47
class JdepsTask {
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    48
    static interface BadArguments {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    49
        String getKey();
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    50
        Object[] getArgs();
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    51
        boolean showUsage();
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    52
    }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    53
    static class BadArgs extends Exception implements BadArguments {
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    54
        static final long serialVersionUID = 8765093759964640721L;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    55
        BadArgs(String key, Object... args) {
16290
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents: 15030
diff changeset
    56
            super(JdepsTask.getMessage(key, args));
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    57
            this.key = key;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    58
            this.args = args;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    59
        }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    60
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    61
        BadArgs showUsage(boolean b) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    62
            showUsage = b;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    63
            return this;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    64
        }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    65
        final String key;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    66
        final Object[] args;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    67
        boolean showUsage;
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    68
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    69
        @Override
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    70
        public String getKey() {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    71
            return key;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    72
        }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    73
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    74
        @Override
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    75
        public Object[] getArgs() {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    76
            return args;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    77
        }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    78
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    79
        @Override
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    80
        public boolean showUsage() {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    81
            return showUsage;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    82
        }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    83
    }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    84
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    85
    static class UncheckedBadArgs extends RuntimeException implements BadArguments {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    86
        static final long serialVersionUID = -1L;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    87
        final BadArgs cause;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    88
        UncheckedBadArgs(BadArgs cause) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    89
            super(cause);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    90
            this.cause = cause;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    91
        }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    92
        @Override
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    93
        public String getKey() {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    94
            return cause.key;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    95
        }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    96
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    97
        @Override
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    98
        public Object[] getArgs() {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    99
            return cause.args;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   100
        }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   101
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   102
        @Override
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   103
        public boolean showUsage() {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   104
            return cause.showUsage;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   105
        }
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   106
    }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   107
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   108
    static abstract class Option {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   109
        Option(boolean hasArg, String... aliases) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   110
            this.hasArg = hasArg;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   111
            this.aliases = aliases;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   112
        }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   113
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   114
        boolean isHidden() {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   115
            return false;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   116
        }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   117
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   118
        boolean matches(String opt) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   119
            for (String a : aliases) {
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   120
                if (a.equals(opt))
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   121
                    return true;
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   122
                if (hasArg && opt.startsWith(a + "="))
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   123
                    return true;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   124
            }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   125
            return false;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   126
        }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   127
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   128
        boolean ignoreRest() {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   129
            return false;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   130
        }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   131
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   132
        abstract void process(JdepsTask task, String opt, String arg) throws BadArgs;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   133
        final boolean hasArg;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   134
        final String[] aliases;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   135
    }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   136
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   137
    static abstract class HiddenOption extends Option {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   138
        HiddenOption(boolean hasArg, String... aliases) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   139
            super(hasArg, aliases);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   140
        }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   141
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   142
        boolean isHidden() {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   143
            return true;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   144
        }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   145
    }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   146
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   147
    static Option[] recognizedOptions = {
40308
274367a99f98 8136930: Simplify use of module-system options by custom launchers
jjg
parents: 39101
diff changeset
   148
        new Option(false, "-h", "-?", "-help", "--help") {
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   149
            void process(JdepsTask task, String opt, String arg) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   150
                task.options.help = true;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   151
            }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   152
        },
41251
6112540cd0c1 8166238: Update jdeps for GNU-style long form options
mchung
parents: 41164
diff changeset
   153
        new Option(true, "-dotoutput", "--dot-output") {
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   154
            void process(JdepsTask task, String opt, String arg) throws BadArgs {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   155
                Path p = Paths.get(arg);
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   156
                if (Files.exists(p) && (!Files.isDirectory(p) || !Files.isWritable(p))) {
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   157
                    throw new BadArgs("err.invalid.path", arg);
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   158
                }
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   159
                task.options.dotOutputDir = Paths.get(arg);;
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   160
            }
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   161
        },
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   162
        new Option(false, "-s", "-summary") {
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   163
            void process(JdepsTask task, String opt, String arg) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   164
                task.options.showSummary = true;
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   165
                task.options.verbose = SUMMARY;
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   166
            }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   167
        },
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   168
        new Option(false, "-v", "-verbose",
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   169
                                "-verbose:module",
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   170
                                "-verbose:package",
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   171
                                "-verbose:class") {
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   172
            void process(JdepsTask task, String opt, String arg) throws BadArgs {
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   173
                switch (opt) {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   174
                    case "-v":
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   175
                    case "-verbose":
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   176
                        task.options.verbose = VERBOSE;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   177
                        task.options.filterSameArchive = false;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   178
                        task.options.filterSamePackage = false;
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   179
                        break;
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   180
                    case "-verbose:module":
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   181
                        task.options.verbose = MODULE;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   182
                        break;
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   183
                    case "-verbose:package":
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   184
                        task.options.verbose = PACKAGE;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   185
                        break;
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   186
                    case "-verbose:class":
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   187
                        task.options.verbose = CLASS;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   188
                        break;
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   189
                    default:
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   190
                        throw new BadArgs("err.invalid.arg.for.option", opt);
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   191
                }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   192
            }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   193
        },
41251
6112540cd0c1 8166238: Update jdeps for GNU-style long form options
mchung
parents: 41164
diff changeset
   194
        new Option(false, "-apionly", "--api-only") {
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   195
            void process(JdepsTask task, String opt, String arg) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   196
                task.options.apiOnly = true;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   197
            }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   198
        },
40308
274367a99f98 8136930: Simplify use of module-system options by custom launchers
jjg
parents: 39101
diff changeset
   199
        new Option(true, "--check") {
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   200
            void process(JdepsTask task, String opt, String arg) throws BadArgs {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   201
                Set<String> mods =  Set.of(arg.split(","));
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   202
                task.options.checkModuleDeps = mods;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   203
                task.options.addmods.addAll(mods);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   204
            }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   205
        },
41251
6112540cd0c1 8166238: Update jdeps for GNU-style long form options
mchung
parents: 41164
diff changeset
   206
        new Option(true, "--generate-module-info") {
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   207
            void process(JdepsTask task, String opt, String arg) throws BadArgs {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   208
                Path p = Paths.get(arg);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   209
                if (Files.exists(p) && (!Files.isDirectory(p) || !Files.isWritable(p))) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   210
                    throw new BadArgs("err.invalid.path", arg);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   211
                }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   212
                task.options.genModuleInfo = Paths.get(arg);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   213
            }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   214
        },
41251
6112540cd0c1 8166238: Update jdeps for GNU-style long form options
mchung
parents: 41164
diff changeset
   215
        new Option(false, "-jdkinternals", "--jdk-internals") {
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   216
            void process(JdepsTask task, String opt, String arg) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   217
                task.options.findJDKInternals = true;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   218
                task.options.verbose = CLASS;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   219
                if (task.options.includePattern == null) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   220
                    task.options.includePattern = Pattern.compile(".*");
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   221
                }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   222
            }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   223
        },
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   224
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   225
        // ---- paths option ----
40308
274367a99f98 8136930: Simplify use of module-system options by custom launchers
jjg
parents: 39101
diff changeset
   226
        new Option(true, "-cp", "-classpath", "--class-path") {
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   227
            void process(JdepsTask task, String opt, String arg) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   228
                task.options.classpath = arg;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   229
            }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   230
        },
40308
274367a99f98 8136930: Simplify use of module-system options by custom launchers
jjg
parents: 39101
diff changeset
   231
        new Option(true, "--module-path") {
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   232
            void process(JdepsTask task, String opt, String arg) throws BadArgs {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   233
                task.options.modulePath = arg;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   234
            }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   235
        },
40308
274367a99f98 8136930: Simplify use of module-system options by custom launchers
jjg
parents: 39101
diff changeset
   236
        new Option(true, "--upgrade-module-path") {
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   237
            void process(JdepsTask task, String opt, String arg) throws BadArgs {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   238
                task.options.upgradeModulePath = arg;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   239
            }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   240
        },
40308
274367a99f98 8136930: Simplify use of module-system options by custom launchers
jjg
parents: 39101
diff changeset
   241
        new Option(true, "--system") {
38525
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   242
            void process(JdepsTask task, String opt, String arg) throws BadArgs {
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   243
                if (arg.equals("none")) {
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   244
                    task.options.systemModulePath = null;
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   245
                } else {
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   246
                    Path path = Paths.get(arg);
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   247
                    if (Files.isRegularFile(path.resolve("lib").resolve("modules")))
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   248
                        task.options.systemModulePath = arg;
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   249
                    else
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   250
                        throw new BadArgs("err.invalid.path", arg);
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   251
                }
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   252
            }
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   253
        },
40308
274367a99f98 8136930: Simplify use of module-system options by custom launchers
jjg
parents: 39101
diff changeset
   254
        new Option(true, "--add-modules") {
38525
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   255
            void process(JdepsTask task, String opt, String arg) throws BadArgs {
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   256
                Set<String> mods = Set.of(arg.split(","));
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   257
                task.options.addmods.addAll(mods);
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   258
            }
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   259
        },
40308
274367a99f98 8136930: Simplify use of module-system options by custom launchers
jjg
parents: 39101
diff changeset
   260
        new Option(true, "-m", "--module") {
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   261
            void process(JdepsTask task, String opt, String arg) throws BadArgs {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   262
                task.options.rootModule = arg;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   263
                task.options.addmods.add(arg);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   264
            }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   265
        },
41251
6112540cd0c1 8166238: Update jdeps for GNU-style long form options
mchung
parents: 41164
diff changeset
   266
        new Option(true, "--multi-release") {
6112540cd0c1 8166238: Update jdeps for GNU-style long form options
mchung
parents: 41164
diff changeset
   267
            void process(JdepsTask task, String opt, String arg) throws BadArgs {
6112540cd0c1 8166238: Update jdeps for GNU-style long form options
mchung
parents: 41164
diff changeset
   268
                if (arg.equalsIgnoreCase("base")) {
6112540cd0c1 8166238: Update jdeps for GNU-style long form options
mchung
parents: 41164
diff changeset
   269
                    task.options.multiRelease = JarFile.baseVersion();
6112540cd0c1 8166238: Update jdeps for GNU-style long form options
mchung
parents: 41164
diff changeset
   270
                } else {
6112540cd0c1 8166238: Update jdeps for GNU-style long form options
mchung
parents: 41164
diff changeset
   271
                    try {
6112540cd0c1 8166238: Update jdeps for GNU-style long form options
mchung
parents: 41164
diff changeset
   272
                        int v = Integer.parseInt(arg);
6112540cd0c1 8166238: Update jdeps for GNU-style long form options
mchung
parents: 41164
diff changeset
   273
                        if (v < 9) {
6112540cd0c1 8166238: Update jdeps for GNU-style long form options
mchung
parents: 41164
diff changeset
   274
                            throw new BadArgs("err.invalid.arg.for.option", arg);
6112540cd0c1 8166238: Update jdeps for GNU-style long form options
mchung
parents: 41164
diff changeset
   275
                        }
6112540cd0c1 8166238: Update jdeps for GNU-style long form options
mchung
parents: 41164
diff changeset
   276
                    } catch (NumberFormatException x) {
6112540cd0c1 8166238: Update jdeps for GNU-style long form options
mchung
parents: 41164
diff changeset
   277
                        throw new BadArgs("err.invalid.arg.for.option", arg);
6112540cd0c1 8166238: Update jdeps for GNU-style long form options
mchung
parents: 41164
diff changeset
   278
                    }
6112540cd0c1 8166238: Update jdeps for GNU-style long form options
mchung
parents: 41164
diff changeset
   279
                    task.options.multiRelease = Runtime.Version.parse(arg);
6112540cd0c1 8166238: Update jdeps for GNU-style long form options
mchung
parents: 41164
diff changeset
   280
                }
6112540cd0c1 8166238: Update jdeps for GNU-style long form options
mchung
parents: 41164
diff changeset
   281
            }
6112540cd0c1 8166238: Update jdeps for GNU-style long form options
mchung
parents: 41164
diff changeset
   282
        },
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   283
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   284
        // ---- Target filtering options ----
41251
6112540cd0c1 8166238: Update jdeps for GNU-style long form options
mchung
parents: 41164
diff changeset
   285
        new Option(true, "-p", "-package", "--package") {
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   286
            void process(JdepsTask task, String opt, String arg) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   287
                task.options.packageNames.add(arg);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   288
            }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   289
        },
41251
6112540cd0c1 8166238: Update jdeps for GNU-style long form options
mchung
parents: 41164
diff changeset
   290
        new Option(true, "-e", "-regex", "--regex") {
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   291
            void process(JdepsTask task, String opt, String arg) {
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   292
                task.options.regex = Pattern.compile(arg);
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   293
            }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   294
        },
41251
6112540cd0c1 8166238: Update jdeps for GNU-style long form options
mchung
parents: 41164
diff changeset
   295
        new Option(true, "--require") {
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   296
            void process(JdepsTask task, String opt, String arg) {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   297
                task.options.requires.add(arg);
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   298
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   299
        },
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   300
        new Option(true, "-f", "-filter") {
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   301
            void process(JdepsTask task, String opt, String arg) {
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   302
                task.options.filterRegex = Pattern.compile(arg);
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   303
            }
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   304
        },
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   305
        new Option(false, "-filter:package",
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   306
                          "-filter:archive", "-filter:module",
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   307
                          "-filter:none") {
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   308
            void process(JdepsTask task, String opt, String arg) {
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   309
                switch (opt) {
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   310
                    case "-filter:package":
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   311
                        task.options.filterSamePackage = true;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   312
                        task.options.filterSameArchive = false;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   313
                        break;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   314
                    case "-filter:archive":
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   315
                    case "-filter:module":
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   316
                        task.options.filterSameArchive = true;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   317
                        task.options.filterSamePackage = false;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   318
                        break;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   319
                    case "-filter:none":
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   320
                        task.options.filterSameArchive = false;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   321
                        task.options.filterSamePackage = false;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   322
                        break;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   323
                }
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   324
            }
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   325
        },
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   326
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   327
        // ---- Source filtering options ----
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   328
        new Option(true, "-include") {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   329
            void process(JdepsTask task, String opt, String arg) throws BadArgs {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   330
                task.options.includePattern = Pattern.compile(arg);
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   331
            }
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   332
        },
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   333
40762
f8883aa0053c 8160851: Remove old launcher module-related options
mchung
parents: 40308
diff changeset
   334
        // Another alternative to list modules in --add-modules option
40308
274367a99f98 8136930: Simplify use of module-system options by custom launchers
jjg
parents: 39101
diff changeset
   335
        new HiddenOption(true, "--include-system-modules") {
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   336
            void process(JdepsTask task, String opt, String arg) throws BadArgs {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   337
                task.options.includeSystemModulePattern = Pattern.compile(arg);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   338
            }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   339
        },
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   340
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   341
        new Option(false, "-P", "-profile") {
16550
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   342
            void process(JdepsTask task, String opt, String arg) throws BadArgs {
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   343
                task.options.showProfile = true;
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   344
            }
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   345
        },
38525
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   346
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   347
        new Option(false, "-R", "-recursive") {
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   348
            void process(JdepsTask task, String opt, String arg) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   349
                task.options.depth = 0;
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   350
                // turn off filtering
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   351
                task.options.filterSameArchive = false;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   352
                task.options.filterSamePackage = false;
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   353
            }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   354
        },
38525
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   355
41251
6112540cd0c1 8166238: Update jdeps for GNU-style long form options
mchung
parents: 41164
diff changeset
   356
        new Option(false, "-I", "--inverse") {
38525
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   357
            void process(JdepsTask task, String opt, String arg) {
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   358
                task.options.inverse = true;
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   359
                // equivalent to the inverse of compile-time view analysis
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   360
                task.options.compileTimeView = true;
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   361
                task.options.filterSamePackage = true;
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   362
                task.options.filterSameArchive = true;
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   363
            }
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   364
        },
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   365
40308
274367a99f98 8136930: Simplify use of module-system options by custom launchers
jjg
parents: 39101
diff changeset
   366
        new Option(false, "--compile-time") {
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   367
            void process(JdepsTask task, String opt, String arg) {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   368
                task.options.compileTimeView = true;
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   369
                task.options.filterSamePackage = true;
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   370
                task.options.filterSameArchive = true;
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   371
                task.options.depth = 0;
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   372
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   373
        },
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   374
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   375
        new Option(false, "-q", "-quiet") {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   376
            void process(JdepsTask task, String opt, String arg) {
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   377
                task.options.nowarning = true;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   378
            }
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   379
        },
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   380
41251
6112540cd0c1 8166238: Update jdeps for GNU-style long form options
mchung
parents: 41164
diff changeset
   381
        new Option(false, "-version", "--version") {
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   382
            void process(JdepsTask task, String opt, String arg) {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   383
                task.options.version = true;
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   384
            }
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   385
        },
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   386
        new HiddenOption(false, "-fullversion") {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   387
            void process(JdepsTask task, String opt, String arg) {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   388
                task.options.fullVersion = true;
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   389
            }
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   390
        },
21503
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   391
        new HiddenOption(false, "-showlabel") {
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   392
            void process(JdepsTask task, String opt, String arg) {
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   393
                task.options.showLabel = true;
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   394
            }
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   395
        },
40308
274367a99f98 8136930: Simplify use of module-system options by custom launchers
jjg
parents: 39101
diff changeset
   396
        new HiddenOption(false, "--hide-show-module") {
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   397
            void process(JdepsTask task, String opt, String arg) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   398
                task.options.showModule = false;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   399
            }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   400
        },
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   401
        new HiddenOption(true, "-depth") {
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   402
            void process(JdepsTask task, String opt, String arg) throws BadArgs {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   403
                try {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   404
                    task.options.depth = Integer.parseInt(arg);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   405
                } catch (NumberFormatException e) {
16560
c6c7f0c26568 8010361: fix some langtools findbugs issues
jjg
parents: 16550
diff changeset
   406
                    throw new BadArgs("err.invalid.arg.for.option", opt);
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   407
                }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   408
            }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   409
        },
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   410
    };
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   411
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   412
    private static final String PROGNAME = "jdeps";
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   413
    private final Options options = new Options();
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   414
    private final List<String> inputArgs = new ArrayList<>();
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   415
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   416
    private PrintWriter log;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   417
    void setLog(PrintWriter out) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   418
        log = out;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   419
    }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   420
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   421
    /**
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   422
     * Result codes.
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   423
     */
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   424
    static final int EXIT_OK = 0,       // Completed with no errors.
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   425
                     EXIT_ERROR = 1,    // Completed but reported errors.
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   426
                     EXIT_CMDERR = 2,   // Bad command-line arguments
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   427
                     EXIT_SYSERR = 3,   // System error or resource exhaustion.
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   428
                     EXIT_ABNORMAL = 4; // terminated abnormally
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   429
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   430
    int run(String... args) {
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   431
        if (log == null) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   432
            log = new PrintWriter(System.out);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   433
        }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   434
        try {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   435
            handleOptions(args);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   436
            if (options.help) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   437
                showHelp();
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   438
            }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   439
            if (options.version || options.fullVersion) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   440
                showVersion(options.fullVersion);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   441
            }
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   442
            if (!inputArgs.isEmpty() && options.rootModule != null) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   443
                reportError("err.invalid.arg.for.option", "-m");
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   444
            }
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   445
            if (inputArgs.isEmpty() && options.addmods.isEmpty() && options.includePattern == null
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   446
                    && options.includeSystemModulePattern == null && options.checkModuleDeps == null) {
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   447
                if (options.help || options.version || options.fullVersion) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   448
                    return EXIT_OK;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   449
                } else {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   450
                    showHelp();
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   451
                    return EXIT_CMDERR;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   452
                }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   453
            }
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   454
            if (options.genModuleInfo != null) {
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   455
                if (options.dotOutputDir != null || options.classpath != null || options.hasFilter()) {
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   456
                    showHelp();
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   457
                    return EXIT_CMDERR;
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   458
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   459
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   460
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   461
            if (options.numFilters() > 1) {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   462
                reportError("err.invalid.filters");
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   463
                return EXIT_CMDERR;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   464
            }
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   465
38525
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   466
            if (options.inverse && options.depth != 1) {
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   467
                reportError("err.invalid.inverse.option", "-R");
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   468
                return EXIT_CMDERR;
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   469
            }
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   470
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   471
            if (options.inverse && options.numFilters() == 0) {
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   472
                reportError("err.invalid.filters");
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   473
                return EXIT_CMDERR;
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   474
            }
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   475
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   476
            if ((options.findJDKInternals) && (options.hasFilter() || options.showSummary)) {
22007
907f7054db16 8029216: (jdeps) Provide a specific option to report JDK internal APIs
mchung
parents: 21503
diff changeset
   477
                showHelp();
907f7054db16 8029216: (jdeps) Provide a specific option to report JDK internal APIs
mchung
parents: 21503
diff changeset
   478
                return EXIT_CMDERR;
907f7054db16 8029216: (jdeps) Provide a specific option to report JDK internal APIs
mchung
parents: 21503
diff changeset
   479
            }
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   480
            if (options.showSummary && options.verbose != SUMMARY) {
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   481
                showHelp();
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   482
                return EXIT_CMDERR;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   483
            }
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   484
            if (options.checkModuleDeps != null && !inputArgs.isEmpty()) {
40308
274367a99f98 8136930: Simplify use of module-system options by custom launchers
jjg
parents: 39101
diff changeset
   485
                reportError("err.invalid.module.option", inputArgs, "--check");
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   486
            }
25874
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   487
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   488
            boolean ok = run();
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   489
            return ok ? EXIT_OK : EXIT_ERROR;
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   490
        } catch (BadArgs|UncheckedBadArgs e) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   491
            reportError(e.getKey(), e.getArgs());
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   492
            if (e.showUsage()) {
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   493
                log.println(getMessage("main.usage.summary", PROGNAME));
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   494
            }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   495
            return EXIT_CMDERR;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   496
        } catch (ResolutionException e) {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   497
            reportError("err.exception.message", e.getMessage());
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   498
            return EXIT_CMDERR;
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   499
        } catch (IOException e) {
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   500
            e.printStackTrace();
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   501
            return EXIT_CMDERR;
41164
69167c89e68f 8153654: Update jdeps to be multi-release jar aware
sdrach
parents: 40762
diff changeset
   502
        } catch (MultiReleaseException e) {
69167c89e68f 8153654: Update jdeps to be multi-release jar aware
sdrach
parents: 40762
diff changeset
   503
            reportError(e.getKey(), (Object)e.getMsg());
69167c89e68f 8153654: Update jdeps to be multi-release jar aware
sdrach
parents: 40762
diff changeset
   504
            return EXIT_CMDERR;  // could be EXIT_ABNORMAL sometimes
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   505
        } finally {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   506
            log.flush();
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   507
        }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   508
    }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   509
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   510
    boolean run() throws IOException {
38530
8e89d567748c 8157391: jdeps left JarFile open
mchung
parents: 38525
diff changeset
   511
        try (JdepsConfiguration config = buildConfig()) {
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   512
38530
8e89d567748c 8157391: jdeps left JarFile open
mchung
parents: 38525
diff changeset
   513
            // detect split packages
8e89d567748c 8157391: jdeps left JarFile open
mchung
parents: 38525
diff changeset
   514
            config.splitPackages().entrySet().stream()
8e89d567748c 8157391: jdeps left JarFile open
mchung
parents: 38525
diff changeset
   515
                .sorted(Map.Entry.comparingByKey())
8e89d567748c 8157391: jdeps left JarFile open
mchung
parents: 38525
diff changeset
   516
                .forEach(e -> System.out.format("split package: %s %s%n", e.getKey(),
8e89d567748c 8157391: jdeps left JarFile open
mchung
parents: 38525
diff changeset
   517
                    e.getValue().toString()));
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   518
38530
8e89d567748c 8157391: jdeps left JarFile open
mchung
parents: 38525
diff changeset
   519
            // check if any module specified in -requires is missing
8e89d567748c 8157391: jdeps left JarFile open
mchung
parents: 38525
diff changeset
   520
            Stream.concat(options.addmods.stream(), options.requires.stream())
8e89d567748c 8157391: jdeps left JarFile open
mchung
parents: 38525
diff changeset
   521
                .filter(mn -> !config.isValidToken(mn))
8e89d567748c 8157391: jdeps left JarFile open
mchung
parents: 38525
diff changeset
   522
                .forEach(mn -> config.findModule(mn).orElseThrow(() ->
8e89d567748c 8157391: jdeps left JarFile open
mchung
parents: 38525
diff changeset
   523
                    new UncheckedBadArgs(new BadArgs("err.module.not.found", mn))));
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   524
40308
274367a99f98 8136930: Simplify use of module-system options by custom launchers
jjg
parents: 39101
diff changeset
   525
            // --gen-module-info
38530
8e89d567748c 8157391: jdeps left JarFile open
mchung
parents: 38525
diff changeset
   526
            if (options.genModuleInfo != null) {
8e89d567748c 8157391: jdeps left JarFile open
mchung
parents: 38525
diff changeset
   527
                return genModuleInfo(config);
8e89d567748c 8157391: jdeps left JarFile open
mchung
parents: 38525
diff changeset
   528
            }
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   529
40308
274367a99f98 8136930: Simplify use of module-system options by custom launchers
jjg
parents: 39101
diff changeset
   530
            // --check
38530
8e89d567748c 8157391: jdeps left JarFile open
mchung
parents: 38525
diff changeset
   531
            if (options.checkModuleDeps != null) {
8e89d567748c 8157391: jdeps left JarFile open
mchung
parents: 38525
diff changeset
   532
                return new ModuleAnalyzer(config, log, options.checkModuleDeps).run();
8e89d567748c 8157391: jdeps left JarFile open
mchung
parents: 38525
diff changeset
   533
            }
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   534
38530
8e89d567748c 8157391: jdeps left JarFile open
mchung
parents: 38525
diff changeset
   535
            if (options.dotOutputDir != null &&
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   536
                (options.verbose == SUMMARY || options.verbose == MODULE) &&
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   537
                !options.addmods.isEmpty() && inputArgs.isEmpty()) {
38530
8e89d567748c 8157391: jdeps left JarFile open
mchung
parents: 38525
diff changeset
   538
                return new ModuleAnalyzer(config, log).genDotFiles(options.dotOutputDir);
8e89d567748c 8157391: jdeps left JarFile open
mchung
parents: 38525
diff changeset
   539
            }
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   540
38530
8e89d567748c 8157391: jdeps left JarFile open
mchung
parents: 38525
diff changeset
   541
            if (options.inverse) {
8e89d567748c 8157391: jdeps left JarFile open
mchung
parents: 38525
diff changeset
   542
                return analyzeInverseDeps(config);
8e89d567748c 8157391: jdeps left JarFile open
mchung
parents: 38525
diff changeset
   543
            } else {
8e89d567748c 8157391: jdeps left JarFile open
mchung
parents: 38525
diff changeset
   544
                return analyzeDeps(config);
8e89d567748c 8157391: jdeps left JarFile open
mchung
parents: 38525
diff changeset
   545
            }
38525
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   546
        }
25874
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   547
    }
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   548
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   549
    private JdepsConfiguration buildConfig() throws IOException {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   550
        JdepsConfiguration.Builder builder =
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   551
            new JdepsConfiguration.Builder(options.systemModulePath);
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   552
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   553
        builder.upgradeModulePath(options.upgradeModulePath)
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   554
               .appModulePath(options.modulePath)
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   555
               .addmods(options.addmods);
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   556
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   557
        if (options.checkModuleDeps != null) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   558
            // check all system modules in the image
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   559
            builder.allModules();
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   560
        }
25692
39537fdca12c 8050804: (jdeps) Recommend supported API to replace use of JDK internal API
mchung
parents: 25442
diff changeset
   561
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   562
        if (options.classpath != null)
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   563
            builder.addClassPath(options.classpath);
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   564
41164
69167c89e68f 8153654: Update jdeps to be multi-release jar aware
sdrach
parents: 40762
diff changeset
   565
        if (options.multiRelease != null)
69167c89e68f 8153654: Update jdeps to be multi-release jar aware
sdrach
parents: 40762
diff changeset
   566
            builder.multiRelease(options.multiRelease);
69167c89e68f 8153654: Update jdeps to be multi-release jar aware
sdrach
parents: 40762
diff changeset
   567
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   568
        // build the root set of archives to be analyzed
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   569
        for (String s : inputArgs) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   570
            Path p = Paths.get(s);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   571
            if (Files.exists(p)) {
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   572
                builder.addRoot(p);
41164
69167c89e68f 8153654: Update jdeps to be multi-release jar aware
sdrach
parents: 40762
diff changeset
   573
            } else {
69167c89e68f 8153654: Update jdeps to be multi-release jar aware
sdrach
parents: 40762
diff changeset
   574
                warning("warn.invalid.arg", s);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   575
            }
25874
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   576
        }
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   577
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   578
        return builder.build();
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   579
    }
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   580
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   581
    private boolean analyzeDeps(JdepsConfiguration config) throws IOException {
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   582
        // output result
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   583
        final JdepsWriter writer;
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   584
        if (options.dotOutputDir != null) {
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   585
            writer = new DotFileWriter(options.dotOutputDir,
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   586
                                       options.verbose,
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   587
                                       options.showProfile,
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   588
                                       options.showModule,
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   589
                                       options.showLabel);
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   590
        } else {
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   591
            writer = new SimpleWriter(log,
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   592
                                      options.verbose,
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   593
                                      options.showProfile,
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   594
                                      options.showModule);
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   595
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   596
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   597
        // analyze the dependencies
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   598
        DepsAnalyzer analyzer = new DepsAnalyzer(config,
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   599
                                        dependencyFilter(config),
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   600
                                        writer,
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   601
                                        options.verbose,
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   602
                                        options.apiOnly);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   603
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   604
        boolean ok = analyzer.run(options.compileTimeView, options.depth);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   605
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   606
        // print skipped entries, if any
38525
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   607
        analyzer.archives()
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   608
            .forEach(archive -> archive.reader()
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   609
                .skippedEntries().stream()
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   610
                .forEach(name -> warning("warn.skipped.entry",
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   611
                                         name, archive.getPathName())));
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   612
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   613
        if (options.findJDKInternals && !options.nowarning) {
39101
fd8a6392b7ea 8159524: jdeps -jdkinternals throws NPE when no replacement is known
mchung
parents: 38532
diff changeset
   614
            Map<String, String> jdkInternals = new TreeMap<>();
fd8a6392b7ea 8159524: jdeps -jdkinternals throws NPE when no replacement is known
mchung
parents: 38532
diff changeset
   615
            Set<String> deps = analyzer.dependences();
fd8a6392b7ea 8159524: jdeps -jdkinternals throws NPE when no replacement is known
mchung
parents: 38532
diff changeset
   616
            // find the ones with replacement
fd8a6392b7ea 8159524: jdeps -jdkinternals throws NPE when no replacement is known
mchung
parents: 38532
diff changeset
   617
            deps.forEach(cn -> replacementFor(cn).ifPresent(
fd8a6392b7ea 8159524: jdeps -jdkinternals throws NPE when no replacement is known
mchung
parents: 38532
diff changeset
   618
                repl -> jdkInternals.put(cn, repl))
fd8a6392b7ea 8159524: jdeps -jdkinternals throws NPE when no replacement is known
mchung
parents: 38532
diff changeset
   619
            );
fd8a6392b7ea 8159524: jdeps -jdkinternals throws NPE when no replacement is known
mchung
parents: 38532
diff changeset
   620
fd8a6392b7ea 8159524: jdeps -jdkinternals throws NPE when no replacement is known
mchung
parents: 38532
diff changeset
   621
            if (!deps.isEmpty()) {
fd8a6392b7ea 8159524: jdeps -jdkinternals throws NPE when no replacement is known
mchung
parents: 38532
diff changeset
   622
                log.println();
fd8a6392b7ea 8159524: jdeps -jdkinternals throws NPE when no replacement is known
mchung
parents: 38532
diff changeset
   623
                warning("warn.replace.useJDKInternals", getMessage("jdeps.wiki.url"));
fd8a6392b7ea 8159524: jdeps -jdkinternals throws NPE when no replacement is known
mchung
parents: 38532
diff changeset
   624
            }
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   625
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   626
            if (!jdkInternals.isEmpty()) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   627
                log.println();
39101
fd8a6392b7ea 8159524: jdeps -jdkinternals throws NPE when no replacement is known
mchung
parents: 38532
diff changeset
   628
                log.format("%-40s %s%n", "JDK Internal API", "Suggested Replacement");
fd8a6392b7ea 8159524: jdeps -jdkinternals throws NPE when no replacement is known
mchung
parents: 38532
diff changeset
   629
                log.format("%-40s %s%n", "----------------", "---------------------");
fd8a6392b7ea 8159524: jdeps -jdkinternals throws NPE when no replacement is known
mchung
parents: 38532
diff changeset
   630
                jdkInternals.entrySet().stream()
fd8a6392b7ea 8159524: jdeps -jdkinternals throws NPE when no replacement is known
mchung
parents: 38532
diff changeset
   631
                    .forEach(e -> {
fd8a6392b7ea 8159524: jdeps -jdkinternals throws NPE when no replacement is known
mchung
parents: 38532
diff changeset
   632
                        String key = e.getKey();
fd8a6392b7ea 8159524: jdeps -jdkinternals throws NPE when no replacement is known
mchung
parents: 38532
diff changeset
   633
                        String[] lines = e.getValue().split("\\n");
fd8a6392b7ea 8159524: jdeps -jdkinternals throws NPE when no replacement is known
mchung
parents: 38532
diff changeset
   634
                        for (String s : lines) {
fd8a6392b7ea 8159524: jdeps -jdkinternals throws NPE when no replacement is known
mchung
parents: 38532
diff changeset
   635
                            log.format("%-40s %s%n", key, s);
fd8a6392b7ea 8159524: jdeps -jdkinternals throws NPE when no replacement is known
mchung
parents: 38532
diff changeset
   636
                            key = "";
fd8a6392b7ea 8159524: jdeps -jdkinternals throws NPE when no replacement is known
mchung
parents: 38532
diff changeset
   637
                        }
fd8a6392b7ea 8159524: jdeps -jdkinternals throws NPE when no replacement is known
mchung
parents: 38532
diff changeset
   638
                    });
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   639
            }
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   640
        }
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   641
        return ok;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   642
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   643
38525
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   644
    private boolean analyzeInverseDeps(JdepsConfiguration config) throws IOException {
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   645
        JdepsWriter writer = new SimpleWriter(log,
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   646
                                              options.verbose,
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   647
                                              options.showProfile,
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   648
                                              options.showModule);
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   649
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   650
        InverseDepsAnalyzer analyzer = new InverseDepsAnalyzer(config,
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   651
                                                               dependencyFilter(config),
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   652
                                                               writer,
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   653
                                                               options.verbose,
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   654
                                                               options.apiOnly);
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   655
        boolean ok = analyzer.run();
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   656
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   657
        log.println();
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   658
        if (!options.requires.isEmpty())
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   659
            log.format("Inverse transitive dependences on %s%n", options.requires);
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   660
        else
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   661
            log.format("Inverse transitive dependences matching %s%n",
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   662
                options.regex != null
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   663
                    ? options.regex.toString()
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   664
                    : "packages " + options.packageNames);
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   665
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   666
        analyzer.inverseDependences().stream()
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   667
                .sorted(Comparator.comparing(this::sortPath))
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   668
                .forEach(path -> log.println(path.stream()
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   669
                                                .map(Archive::getName)
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   670
                                                .collect(Collectors.joining(" <- "))));
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   671
        return ok;
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   672
    }
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   673
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   674
    private String sortPath(Deque<Archive> path) {
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   675
        return path.peekFirst().getName();
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   676
    }
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   677
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   678
    private boolean genModuleInfo(JdepsConfiguration config) throws IOException {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   679
        ModuleInfoBuilder builder
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   680
            = new ModuleInfoBuilder(config, inputArgs, options.genModuleInfo);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   681
        boolean ok = builder.run();
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   682
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   683
        builder.modules().forEach(module -> {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   684
            if (module.packages().contains("")) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   685
                reportError("ERROR: %s contains unnamed package.  " +
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   686
                    "module-info.java not generated%n", module.getPathName());
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   687
            }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   688
        });
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   689
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   690
        if (!ok && !options.nowarning) {
38532
24f77d64bb1f 8153042: jdeps should continue to report JDK internal APIs that are removed/renamed in JDK
mchung
parents: 38530
diff changeset
   691
            log.println("ERROR: missing dependencies");
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   692
            builder.visitMissingDeps(
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   693
                new Analyzer.Visitor() {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   694
                    @Override
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   695
                    public void visitDependence(String origin, Archive originArchive,
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   696
                                                String target, Archive targetArchive) {
38532
24f77d64bb1f 8153042: jdeps should continue to report JDK internal APIs that are removed/renamed in JDK
mchung
parents: 38530
diff changeset
   697
                        if (builder.notFound(targetArchive))
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   698
                            log.format("   %-50s -> %-50s %s%n",
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   699
                                origin, target, targetArchive.getName());
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   700
                    }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   701
                });
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   702
        }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   703
        return ok;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   704
    }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   705
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   706
    /**
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   707
     * Returns a filter used during dependency analysis
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   708
     */
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   709
    private JdepsFilter dependencyFilter(JdepsConfiguration config) {
38525
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   710
        // Filter specified by -filter, -package, -regex, and -requires options
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   711
        JdepsFilter.Builder builder = new JdepsFilter.Builder();
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   712
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   713
        // source filters
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   714
        builder.includePattern(options.includePattern);
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   715
        builder.includeSystemModules(options.includeSystemModulePattern);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   716
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   717
        builder.filter(options.filterSamePackage, options.filterSameArchive);
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   718
        builder.findJDKInternals(options.findJDKInternals);
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   719
38525
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   720
        // -requires
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   721
        if (!options.requires.isEmpty()) {
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   722
            options.requires.stream()
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   723
                .forEach(mn -> {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   724
                    Module m = config.findModule(mn).get();
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   725
                    builder.requires(mn, m.packages());
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   726
                });
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   727
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   728
        // -regex
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   729
        if (options.regex != null)
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   730
            builder.regex(options.regex);
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   731
        // -package
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   732
        if (!options.packageNames.isEmpty())
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   733
            builder.packages(options.packageNames);
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   734
        // -filter
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   735
        if (options.filterRegex != null)
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   736
            builder.filter(options.filterRegex);
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   737
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   738
        // check if system module is set
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   739
        config.rootModules().stream()
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   740
            .map(Module::name)
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   741
            .forEach(builder::includeIfSystemModule);
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   742
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   743
        return builder.build();
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   744
    }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   745
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   746
    public void handleOptions(String[] args) throws BadArgs {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   747
        // process options
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   748
        for (int i=0; i < args.length; i++) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   749
            if (args[i].charAt(0) == '-') {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   750
                String name = args[i];
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   751
                Option option = getOption(name);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   752
                String param = null;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   753
                if (option.hasArg) {
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   754
                    if (name.startsWith("-") && name.indexOf('=') > 0) {
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   755
                        param = name.substring(name.indexOf('=') + 1, name.length());
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   756
                    } else if (i + 1 < args.length) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   757
                        param = args[++i];
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   758
                    }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   759
                    if (param == null || param.isEmpty() || param.charAt(0) == '-') {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   760
                        throw new BadArgs("err.missing.arg", name).showUsage(true);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   761
                    }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   762
                }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   763
                option.process(this, name, param);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   764
                if (option.ignoreRest()) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   765
                    i = args.length;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   766
                }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   767
            } else {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   768
                // process rest of the input arguments
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   769
                for (; i < args.length; i++) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   770
                    String name = args[i];
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   771
                    if (name.charAt(0) == '-') {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   772
                        throw new BadArgs("err.option.after.class", name).showUsage(true);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   773
                    }
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   774
                    inputArgs.add(name);
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   775
                }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   776
            }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   777
        }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   778
    }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   779
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   780
    private Option getOption(String name) throws BadArgs {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   781
        for (Option o : recognizedOptions) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   782
            if (o.matches(name)) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   783
                return o;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   784
            }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   785
        }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   786
        throw new BadArgs("err.unknown.option", name).showUsage(true);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   787
    }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   788
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   789
    private void reportError(String key, Object... args) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   790
        log.println(getMessage("error.prefix") + " " + getMessage(key, args));
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   791
    }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   792
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   793
    void warning(String key, Object... args) {
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   794
        log.println(getMessage("warn.prefix") + " " + getMessage(key, args));
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   795
    }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   796
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   797
    private void showHelp() {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   798
        log.println(getMessage("main.usage", PROGNAME));
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   799
        for (Option o : recognizedOptions) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   800
            String name = o.aliases[0].substring(1); // there must always be at least one name
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   801
            name = name.charAt(0) == '-' ? name.substring(1) : name;
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   802
            if (o.isHidden() || name.equals("h") || name.startsWith("filter:")) {
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   803
                continue;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   804
            }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   805
            log.println(getMessage("main.opt." + name));
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   806
        }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   807
    }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   808
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   809
    private void showVersion(boolean full) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   810
        log.println(version(full ? "full" : "release"));
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   811
    }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   812
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   813
    private String version(String key) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   814
        // key=version:  mm.nn.oo[-milestone]
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   815
        // key=full:     mm.mm.oo[-milestone]-build
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   816
        if (ResourceBundleHelper.versionRB == null) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   817
            return System.getProperty("java.version");
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   818
        }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   819
        try {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   820
            return ResourceBundleHelper.versionRB.getString(key);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   821
        } catch (MissingResourceException e) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   822
            return getMessage("version.unknown", System.getProperty("java.version"));
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   823
        }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   824
    }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   825
16290
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents: 15030
diff changeset
   826
    static String getMessage(String key, Object... args) {
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   827
        try {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   828
            return MessageFormat.format(ResourceBundleHelper.bundle.getString(key), args);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   829
        } catch (MissingResourceException e) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   830
            throw new InternalError("Missing message: " + key);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   831
        }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   832
    }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   833
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   834
    private static class Options {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   835
        boolean help;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   836
        boolean version;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   837
        boolean fullVersion;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   838
        boolean showProfile;
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   839
        boolean showModule = true;
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   840
        boolean showSummary;
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   841
        boolean apiOnly;
21503
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   842
        boolean showLabel;
22007
907f7054db16 8029216: (jdeps) Provide a specific option to report JDK internal APIs
mchung
parents: 21503
diff changeset
   843
        boolean findJDKInternals;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   844
        boolean nowarning = false;
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   845
        // default is to show package-level dependencies
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   846
        // and filter references from same package
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   847
        Analyzer.Type verbose = PACKAGE;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   848
        boolean filterSamePackage = true;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   849
        boolean filterSameArchive = false;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   850
        Pattern filterRegex;
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   851
        Path dotOutputDir;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   852
        Path genModuleInfo;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   853
        String classpath;
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   854
        int depth = 1;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   855
        Set<String> requires = new HashSet<>();
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   856
        Set<String> packageNames = new HashSet<>();
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   857
        Pattern regex;             // apply to the dependences
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   858
        Pattern includePattern;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   859
        Pattern includeSystemModulePattern;
38525
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   860
        boolean inverse = false;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   861
        boolean compileTimeView = false;
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   862
        Set<String> checkModuleDeps;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   863
        String systemModulePath = System.getProperty("java.home");
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   864
        String upgradeModulePath;
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   865
        String modulePath;
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   866
        String rootModule;
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   867
        Set<String> addmods = new HashSet<>();
41164
69167c89e68f 8153654: Update jdeps to be multi-release jar aware
sdrach
parents: 40762
diff changeset
   868
        Runtime.Version multiRelease;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   869
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   870
        boolean hasFilter() {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   871
            return numFilters() > 0;
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   872
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   873
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   874
        int numFilters() {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   875
            int count = 0;
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   876
            if (requires.size() > 0) count++;
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   877
            if (regex != null) count++;
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   878
            if (packageNames.size() > 0) count++;
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   879
            return count;
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   880
        }
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   881
    }
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   882
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   883
    private static class ResourceBundleHelper {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   884
        static final ResourceBundle versionRB;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   885
        static final ResourceBundle bundle;
25692
39537fdca12c 8050804: (jdeps) Recommend supported API to replace use of JDK internal API
mchung
parents: 25442
diff changeset
   886
        static final ResourceBundle jdkinternals;
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   887
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   888
        static {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   889
            Locale locale = Locale.getDefault();
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   890
            try {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   891
                bundle = ResourceBundle.getBundle("com.sun.tools.jdeps.resources.jdeps", locale);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   892
            } catch (MissingResourceException e) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   893
                throw new InternalError("Cannot find jdeps resource bundle for locale " + locale);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   894
            }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   895
            try {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   896
                versionRB = ResourceBundle.getBundle("com.sun.tools.jdeps.resources.version");
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   897
            } catch (MissingResourceException e) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   898
                throw new InternalError("version.resource.missing");
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   899
            }
25692
39537fdca12c 8050804: (jdeps) Recommend supported API to replace use of JDK internal API
mchung
parents: 25442
diff changeset
   900
            try {
39537fdca12c 8050804: (jdeps) Recommend supported API to replace use of JDK internal API
mchung
parents: 25442
diff changeset
   901
                jdkinternals = ResourceBundle.getBundle("com.sun.tools.jdeps.resources.jdkinternals");
39537fdca12c 8050804: (jdeps) Recommend supported API to replace use of JDK internal API
mchung
parents: 25442
diff changeset
   902
            } catch (MissingResourceException e) {
39537fdca12c 8050804: (jdeps) Recommend supported API to replace use of JDK internal API
mchung
parents: 25442
diff changeset
   903
                throw new InternalError("Cannot find jdkinternals resource bundle");
39537fdca12c 8050804: (jdeps) Recommend supported API to replace use of JDK internal API
mchung
parents: 25442
diff changeset
   904
            }
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   905
        }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   906
    }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   907
25692
39537fdca12c 8050804: (jdeps) Recommend supported API to replace use of JDK internal API
mchung
parents: 25442
diff changeset
   908
    /**
39537fdca12c 8050804: (jdeps) Recommend supported API to replace use of JDK internal API
mchung
parents: 25442
diff changeset
   909
     * Returns the recommended replacement API for the given classname;
39537fdca12c 8050804: (jdeps) Recommend supported API to replace use of JDK internal API
mchung
parents: 25442
diff changeset
   910
     * or return null if replacement API is not known.
39537fdca12c 8050804: (jdeps) Recommend supported API to replace use of JDK internal API
mchung
parents: 25442
diff changeset
   911
     */
39101
fd8a6392b7ea 8159524: jdeps -jdkinternals throws NPE when no replacement is known
mchung
parents: 38532
diff changeset
   912
    private Optional<String> replacementFor(String cn) {
25692
39537fdca12c 8050804: (jdeps) Recommend supported API to replace use of JDK internal API
mchung
parents: 25442
diff changeset
   913
        String name = cn;
39537fdca12c 8050804: (jdeps) Recommend supported API to replace use of JDK internal API
mchung
parents: 25442
diff changeset
   914
        String value = null;
39537fdca12c 8050804: (jdeps) Recommend supported API to replace use of JDK internal API
mchung
parents: 25442
diff changeset
   915
        while (value == null && name != null) {
39537fdca12c 8050804: (jdeps) Recommend supported API to replace use of JDK internal API
mchung
parents: 25442
diff changeset
   916
            try {
39537fdca12c 8050804: (jdeps) Recommend supported API to replace use of JDK internal API
mchung
parents: 25442
diff changeset
   917
                value = ResourceBundleHelper.jdkinternals.getString(name);
39537fdca12c 8050804: (jdeps) Recommend supported API to replace use of JDK internal API
mchung
parents: 25442
diff changeset
   918
            } catch (MissingResourceException e) {
39537fdca12c 8050804: (jdeps) Recommend supported API to replace use of JDK internal API
mchung
parents: 25442
diff changeset
   919
                // go up one subpackage level
39537fdca12c 8050804: (jdeps) Recommend supported API to replace use of JDK internal API
mchung
parents: 25442
diff changeset
   920
                int i = name.lastIndexOf('.');
39537fdca12c 8050804: (jdeps) Recommend supported API to replace use of JDK internal API
mchung
parents: 25442
diff changeset
   921
                name = i > 0 ? name.substring(0, i) : null;
39537fdca12c 8050804: (jdeps) Recommend supported API to replace use of JDK internal API
mchung
parents: 25442
diff changeset
   922
            }
39537fdca12c 8050804: (jdeps) Recommend supported API to replace use of JDK internal API
mchung
parents: 25442
diff changeset
   923
        }
39101
fd8a6392b7ea 8159524: jdeps -jdkinternals throws NPE when no replacement is known
mchung
parents: 38532
diff changeset
   924
        return Optional.ofNullable(value);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   925
    }
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   926
}