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