langtools/src/share/classes/com/sun/tools/jdeps/JdepsTask.java
author briangoetz
Wed, 18 Dec 2013 16:05:18 -0500
changeset 22163 3651128c74eb
parent 22007 907f7054db16
child 25442 755ff386d1ac
permissions -rw-r--r--
8030244: Update langtools to use Diamond Reviewed-by: darcy
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
/*
16560
c6c7f0c26568 8010361: fix some langtools findbugs issues
jjg
parents: 16550
diff changeset
     2
 * Copyright (c) 2012, 2013, 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;
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
    33
import com.sun.tools.jdeps.PlatformClassPath.JDKArchive;
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    34
import java.io.*;
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
    35
import java.nio.file.DirectoryStream;
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
    36
import java.nio.file.Files;
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
    37
import java.nio.file.Path;
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
    38
import java.nio.file.Paths;
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    39
import java.text.MessageFormat;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    40
import java.util.*;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    41
import java.util.regex.Pattern;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    42
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
 * Implementation for the jdeps tool for static class dependency analysis.
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    45
 */
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    46
class JdepsTask {
16560
c6c7f0c26568 8010361: fix some langtools findbugs issues
jjg
parents: 16550
diff changeset
    47
    static class BadArgs extends Exception {
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    48
        static final long serialVersionUID = 8765093759964640721L;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    49
        BadArgs(String key, Object... args) {
16290
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents: 15030
diff changeset
    50
            super(JdepsTask.getMessage(key, args));
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    51
            this.key = key;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    52
            this.args = args;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    53
        }
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
        BadArgs showUsage(boolean b) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    56
            showUsage = b;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    57
            return this;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    58
        }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    59
        final String key;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    60
        final Object[] args;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    61
        boolean showUsage;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    62
    }
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
    static abstract class Option {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    65
        Option(boolean hasArg, String... aliases) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    66
            this.hasArg = hasArg;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    67
            this.aliases = aliases;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    68
        }
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
        boolean isHidden() {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    71
            return false;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    72
        }
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
        boolean matches(String opt) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    75
            for (String a : aliases) {
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
    76
                if (a.equals(opt))
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    77
                    return true;
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
    78
                if (hasArg && opt.startsWith(a + "="))
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    79
                    return true;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    80
            }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    81
            return false;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    82
        }
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
        boolean ignoreRest() {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    85
            return false;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    86
        }
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
        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
    89
        final boolean hasArg;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    90
        final String[] aliases;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    91
    }
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
    static abstract class HiddenOption extends Option {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    94
        HiddenOption(boolean hasArg, String... aliases) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    95
            super(hasArg, aliases);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    96
        }
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
        boolean isHidden() {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    99
            return true;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   100
        }
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
    static Option[] recognizedOptions = {
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   104
        new Option(false, "-h", "-?", "-help") {
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   105
            void process(JdepsTask task, String opt, String arg) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   106
                task.options.help = true;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   107
            }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   108
        },
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   109
        new Option(true, "-dotoutput") {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   110
            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
   111
                Path p = Paths.get(arg);
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   112
                if (Files.exists(p) && (!Files.isDirectory(p) || !Files.isWritable(p))) {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   113
                    throw new BadArgs("err.dot.output.path", arg);
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   114
                }
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   115
                task.options.dotOutputDir = arg;
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   116
            }
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
        new Option(false, "-s", "-summary") {
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   119
            void process(JdepsTask task, String opt, String arg) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   120
                task.options.showSummary = true;
16290
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents: 15030
diff changeset
   121
                task.options.verbose = Analyzer.Type.SUMMARY;
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   122
            }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   123
        },
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   124
        new Option(false, "-v", "-verbose",
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   125
                          "-verbose:package",
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   126
                          "-verbose:class")
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   127
        {
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":
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   132
                        task.options.verbose = Analyzer.Type.VERBOSE;
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   133
                        break;
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   134
                    case "-verbose:package":
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   135
                            task.options.verbose = Analyzer.Type.PACKAGE;
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   136
                            break;
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   137
                    case "-verbose:class":
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   138
                            task.options.verbose = Analyzer.Type.CLASS;
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   139
                            break;
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   140
                    default:
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   141
                        throw new BadArgs("err.invalid.arg.for.option", opt);
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   142
                }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   143
            }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   144
        },
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   145
        new Option(true, "-cp", "-classpath") {
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   146
            void process(JdepsTask task, String opt, String arg) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   147
                task.options.classpath = arg;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   148
            }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   149
        },
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   150
        new Option(true, "-p", "-package") {
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   151
            void process(JdepsTask task, String opt, String arg) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   152
                task.options.packageNames.add(arg);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   153
            }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   154
        },
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   155
        new Option(true, "-e", "-regex") {
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   156
            void process(JdepsTask task, String opt, String arg) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   157
                task.options.regex = arg;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   158
            }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   159
        },
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   160
        new Option(true, "-include") {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   161
            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
   162
                task.options.includePattern = Pattern.compile(arg);
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   163
            }
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   164
        },
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   165
        new Option(false, "-P", "-profile") {
16550
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   166
            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
   167
                task.options.showProfile = true;
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   168
                if (Profile.getProfileCount() == 0) {
16560
c6c7f0c26568 8010361: fix some langtools findbugs issues
jjg
parents: 16550
diff changeset
   169
                    throw new BadArgs("err.option.unsupported", opt, getMessage("err.profiles.msg"));
16550
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   170
                }
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   171
            }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   172
        },
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   173
        new Option(false, "-apionly") {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   174
            void process(JdepsTask task, String opt, String arg) {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   175
                task.options.apiOnly = true;
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   176
            }
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   177
        },
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   178
        new Option(false, "-R", "-recursive") {
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   179
            void process(JdepsTask task, String opt, String arg) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   180
                task.options.depth = 0;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   181
            }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   182
        },
22007
907f7054db16 8029216: (jdeps) Provide a specific option to report JDK internal APIs
mchung
parents: 21503
diff changeset
   183
        new Option(false, "-jdkinternals") {
907f7054db16 8029216: (jdeps) Provide a specific option to report JDK internal APIs
mchung
parents: 21503
diff changeset
   184
            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
   185
                task.options.findJDKInternals = true;
907f7054db16 8029216: (jdeps) Provide a specific option to report JDK internal APIs
mchung
parents: 21503
diff changeset
   186
                task.options.verbose = Analyzer.Type.CLASS;
907f7054db16 8029216: (jdeps) Provide a specific option to report JDK internal APIs
mchung
parents: 21503
diff changeset
   187
                if (task.options.includePattern == null) {
907f7054db16 8029216: (jdeps) Provide a specific option to report JDK internal APIs
mchung
parents: 21503
diff changeset
   188
                    task.options.includePattern = Pattern.compile(".*");
907f7054db16 8029216: (jdeps) Provide a specific option to report JDK internal APIs
mchung
parents: 21503
diff changeset
   189
                }
907f7054db16 8029216: (jdeps) Provide a specific option to report JDK internal APIs
mchung
parents: 21503
diff changeset
   190
            }
907f7054db16 8029216: (jdeps) Provide a specific option to report JDK internal APIs
mchung
parents: 21503
diff changeset
   191
        },
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   192
        new Option(false, "-version") {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   193
            void process(JdepsTask task, String opt, String arg) {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   194
                task.options.version = true;
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   195
            }
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   196
        },
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   197
        new HiddenOption(false, "-fullversion") {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   198
            void process(JdepsTask task, String opt, String arg) {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   199
                task.options.fullVersion = true;
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   200
            }
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   201
        },
21503
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   202
        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
   203
            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
   204
                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
   205
            }
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   206
        },
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   207
        new HiddenOption(true, "-depth") {
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   208
            void process(JdepsTask task, String opt, String arg) throws BadArgs {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   209
                try {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   210
                    task.options.depth = Integer.parseInt(arg);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   211
                } catch (NumberFormatException e) {
16560
c6c7f0c26568 8010361: fix some langtools findbugs issues
jjg
parents: 16550
diff changeset
   212
                    throw new BadArgs("err.invalid.arg.for.option", opt);
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   213
                }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   214
            }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   215
        },
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
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   218
    private static final String PROGNAME = "jdeps";
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   219
    private final Options options = new Options();
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 22007
diff changeset
   220
    private final List<String> classes = new ArrayList<>();
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   221
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   222
    private PrintWriter log;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   223
    void setLog(PrintWriter out) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   224
        log = out;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   225
    }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   226
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   227
    /**
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   228
     * Result codes.
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   229
     */
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   230
    static final int EXIT_OK = 0, // Completed with no errors.
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   231
                     EXIT_ERROR = 1, // Completed but reported errors.
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   232
                     EXIT_CMDERR = 2, // Bad command-line arguments
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   233
                     EXIT_SYSERR = 3, // System error or resource exhaustion.
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   234
                     EXIT_ABNORMAL = 4;// terminated abnormally
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   235
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   236
    int run(String[] args) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   237
        if (log == null) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   238
            log = new PrintWriter(System.out);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   239
        }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   240
        try {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   241
            handleOptions(args);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   242
            if (options.help) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   243
                showHelp();
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   244
            }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   245
            if (options.version || options.fullVersion) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   246
                showVersion(options.fullVersion);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   247
            }
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   248
            if (classes.isEmpty() && options.includePattern == null) {
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   249
                if (options.help || options.version || options.fullVersion) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   250
                    return EXIT_OK;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   251
                } else {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   252
                    showHelp();
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   253
                    return EXIT_CMDERR;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   254
                }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   255
            }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   256
            if (options.regex != null && options.packageNames.size() > 0) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   257
                showHelp();
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   258
                return EXIT_CMDERR;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   259
            }
22007
907f7054db16 8029216: (jdeps) Provide a specific option to report JDK internal APIs
mchung
parents: 21503
diff changeset
   260
            if (options.findJDKInternals &&
907f7054db16 8029216: (jdeps) Provide a specific option to report JDK internal APIs
mchung
parents: 21503
diff changeset
   261
                   (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
   262
                showHelp();
907f7054db16 8029216: (jdeps) Provide a specific option to report JDK internal APIs
mchung
parents: 21503
diff changeset
   263
                return EXIT_CMDERR;
907f7054db16 8029216: (jdeps) Provide a specific option to report JDK internal APIs
mchung
parents: 21503
diff changeset
   264
            }
16290
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents: 15030
diff changeset
   265
            if (options.showSummary && options.verbose != Analyzer.Type.SUMMARY) {
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   266
                showHelp();
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   267
                return EXIT_CMDERR;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   268
            }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   269
            boolean ok = run();
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   270
            return ok ? EXIT_OK : EXIT_ERROR;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   271
        } catch (BadArgs e) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   272
            reportError(e.key, e.args);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   273
            if (e.showUsage) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   274
                log.println(getMessage("main.usage.summary", PROGNAME));
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
            return EXIT_CMDERR;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   277
        } catch (IOException e) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   278
            return EXIT_ABNORMAL;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   279
        } finally {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   280
            log.flush();
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   281
        }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   282
    }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   283
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   284
    private final List<Archive> sourceLocations = new ArrayList<>();
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   285
    private boolean run() throws IOException {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   286
        findDependencies();
16290
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents: 15030
diff changeset
   287
        Analyzer analyzer = new Analyzer(options.verbose);
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents: 15030
diff changeset
   288
        analyzer.run(sourceLocations);
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   289
        if (options.dotOutputDir != null) {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   290
            Path dir = Paths.get(options.dotOutputDir);
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   291
            Files.createDirectories(dir);
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   292
            generateDotFiles(dir, analyzer);
16290
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents: 15030
diff changeset
   293
        } else {
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   294
            printRawOutput(log, analyzer);
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   295
        }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   296
        return true;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   297
    }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   298
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   299
    private void generateDotFiles(Path dir, Analyzer analyzer) throws IOException {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   300
        Path summary = dir.resolve("summary.dot");
21503
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   301
        boolean verbose = options.verbose == Analyzer.Type.VERBOSE;
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   302
        DotGraph<?> graph = verbose ? new DotSummaryForPackage()
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   303
                                    : new DotSummaryForArchive();
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   304
        for (Archive archive : sourceLocations) {
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   305
            analyzer.visitArchiveDependences(archive, graph);
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   306
            if (verbose || options.showLabel) {
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   307
                // traverse detailed dependences to generate package-level
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   308
                // summary or build labels for edges
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   309
                analyzer.visitDependences(archive, graph);
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   310
            }
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   311
        }
21503
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   312
        try (PrintWriter sw = new PrintWriter(Files.newOutputStream(summary))) {
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   313
            graph.writeTo(sw);
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   314
        }
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   315
        // output individual .dot file for each archive
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   316
        if (options.verbose != Analyzer.Type.SUMMARY) {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   317
            for (Archive archive : sourceLocations) {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   318
                if (analyzer.hasDependences(archive)) {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   319
                    Path dotfile = dir.resolve(archive.getFileName() + ".dot");
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   320
                    try (PrintWriter pw = new PrintWriter(Files.newOutputStream(dotfile));
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   321
                         DotFileFormatter formatter = new DotFileFormatter(pw, archive)) {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   322
                        analyzer.visitDependences(archive, formatter);
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   323
                    }
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   324
                }
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   325
            }
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   326
        }
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   327
    }
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   328
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   329
    private void printRawOutput(PrintWriter writer, Analyzer analyzer) {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   330
        for (Archive archive : sourceLocations) {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   331
            RawOutputFormatter formatter = new RawOutputFormatter(writer);
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   332
            analyzer.visitArchiveDependences(archive, formatter);
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   333
            if (options.verbose != Analyzer.Type.SUMMARY) {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   334
                analyzer.visitDependences(archive, formatter);
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   335
            }
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   336
        }
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   337
    }
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   338
    private boolean isValidClassName(String name) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   339
        if (!Character.isJavaIdentifierStart(name.charAt(0))) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   340
            return false;
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
        for (int i=1; i < name.length(); i++) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   343
            char c = name.charAt(i);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   344
            if (c != '.'  && !Character.isJavaIdentifierPart(c)) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   345
                return false;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   346
            }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   347
        }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   348
        return true;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   349
    }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   350
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   351
    private Dependency.Filter getDependencyFilter() {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   352
         if (options.regex != null) {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   353
            return Dependencies.getRegexFilter(Pattern.compile(options.regex));
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   354
        } else if (options.packageNames.size() > 0) {
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   355
            return Dependencies.getPackageFilter(options.packageNames, false);
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   356
        } else {
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   357
            return new Dependency.Filter() {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   358
                @Override
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   359
                public boolean accepts(Dependency dependency) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   360
                    return !dependency.getOrigin().equals(dependency.getTarget());
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   361
                }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   362
            };
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   363
        }
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   364
    }
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   365
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   366
    private boolean matches(String classname, AccessFlags flags) {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   367
        if (options.apiOnly && !flags.is(AccessFlags.ACC_PUBLIC)) {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   368
            return false;
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   369
        } else if (options.includePattern != null) {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   370
            return options.includePattern.matcher(classname.replace('/', '.')).matches();
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   371
        } else {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   372
            return true;
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   373
        }
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   374
    }
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   375
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   376
    private void findDependencies() throws IOException {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   377
        Dependency.Finder finder =
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   378
            options.apiOnly ? Dependencies.getAPIFinder(AccessFlags.ACC_PROTECTED)
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   379
                            : Dependencies.getClassDependencyFinder();
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   380
        Dependency.Filter filter = getDependencyFilter();
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   381
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   382
        List<Archive> archives = new ArrayList<>();
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   383
        Deque<String> roots = new LinkedList<>();
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   384
        for (String s : classes) {
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   385
            Path p = Paths.get(s);
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   386
            if (Files.exists(p)) {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   387
                archives.add(new Archive(p, ClassFileReader.newInstance(p)));
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   388
            } else {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   389
                if (isValidClassName(s)) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   390
                    roots.add(s);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   391
                } else {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   392
                    warning("warn.invalid.arg", s);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   393
                }
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
        }
21503
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   396
        sourceLocations.addAll(archives);
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   397
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   398
        List<Archive> classpaths = new ArrayList<>(); // for class file lookup
21503
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   399
        classpaths.addAll(getClassPathArchives(options.classpath));
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   400
        if (options.includePattern != null) {
21503
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   401
            archives.addAll(classpaths);
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   402
        }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   403
        classpaths.addAll(PlatformClassPath.getArchives());
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   404
21503
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   405
        // add all classpath archives to the source locations for reporting
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   406
        sourceLocations.addAll(classpaths);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   407
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   408
        // Work queue of names of classfiles to be searched.
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   409
        // 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
   410
        // dependencies in the results map.
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   411
        Deque<String> deque = new LinkedList<>();
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   412
        Set<String> doneClasses = new HashSet<>();
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   413
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   414
        // get the immediate dependencies of the input files
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   415
        for (Archive a : archives) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   416
            for (ClassFile cf : a.reader().getClassFiles()) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   417
                String classFileName;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   418
                try {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   419
                    classFileName = cf.getName();
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   420
                } catch (ConstantPoolException e) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   421
                    throw new ClassFileError(e);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   422
                }
16290
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents: 15030
diff changeset
   423
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   424
                if (matches(classFileName, cf.access_flags)) {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   425
                    if (!doneClasses.contains(classFileName)) {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   426
                        doneClasses.add(classFileName);
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   427
                    }
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   428
                    for (Dependency d : finder.findDependencies(cf)) {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   429
                        if (filter.accepts(d)) {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   430
                            String cn = d.getTarget().getName();
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   431
                            if (!doneClasses.contains(cn) && !deque.contains(cn)) {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   432
                                deque.add(cn);
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   433
                            }
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   434
                            a.addClass(d.getOrigin(), d.getTarget());
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   435
                        }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   436
                    }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   437
                }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   438
            }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   439
        }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   440
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   441
        // add Archive for looking up classes from the classpath
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   442
        // for transitive dependency analysis
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   443
        Deque<String> unresolved = roots;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   444
        int depth = options.depth > 0 ? options.depth : Integer.MAX_VALUE;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   445
        do {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   446
            String name;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   447
            while ((name = unresolved.poll()) != null) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   448
                if (doneClasses.contains(name)) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   449
                    continue;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   450
                }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   451
                ClassFile cf = null;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   452
                for (Archive a : classpaths) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   453
                    cf = a.reader().getClassFile(name);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   454
                    if (cf != null) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   455
                        String classFileName;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   456
                        try {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   457
                            classFileName = cf.getName();
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   458
                        } catch (ConstantPoolException e) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   459
                            throw new ClassFileError(e);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   460
                        }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   461
                        if (!doneClasses.contains(classFileName)) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   462
                            // if name is a fully-qualified class name specified
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   463
                            // from command-line, this class might already be parsed
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   464
                            doneClasses.add(classFileName);
16290
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents: 15030
diff changeset
   465
                            for (Dependency d : finder.findDependencies(cf)) {
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents: 15030
diff changeset
   466
                                if (depth == 0) {
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents: 15030
diff changeset
   467
                                    // ignore the dependency
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents: 15030
diff changeset
   468
                                    a.addClass(d.getOrigin());
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents: 15030
diff changeset
   469
                                    break;
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents: 15030
diff changeset
   470
                                } else if (filter.accepts(d)) {
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents: 15030
diff changeset
   471
                                    a.addClass(d.getOrigin(), d.getTarget());
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents: 15030
diff changeset
   472
                                    String cn = d.getTarget().getName();
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents: 15030
diff changeset
   473
                                    if (!doneClasses.contains(cn) && !deque.contains(cn)) {
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents: 15030
diff changeset
   474
                                        deque.add(cn);
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   475
                                    }
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
                            }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   478
                        }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   479
                        break;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   480
                    }
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
                if (cf == null) {
16290
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents: 15030
diff changeset
   483
                    doneClasses.add(name);
15030
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
            }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   486
            unresolved = deque;
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   487
            deque = new LinkedList<>();
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   488
        } while (!unresolved.isEmpty() && depth-- > 0);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   489
    }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   490
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   491
    public void handleOptions(String[] args) throws BadArgs {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   492
        // process options
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   493
        for (int i=0; i < args.length; i++) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   494
            if (args[i].charAt(0) == '-') {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   495
                String name = args[i];
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   496
                Option option = getOption(name);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   497
                String param = null;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   498
                if (option.hasArg) {
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   499
                    if (name.startsWith("-") && name.indexOf('=') > 0) {
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   500
                        param = name.substring(name.indexOf('=') + 1, name.length());
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   501
                    } else if (i + 1 < args.length) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   502
                        param = args[++i];
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   503
                    }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   504
                    if (param == null || param.isEmpty() || param.charAt(0) == '-') {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   505
                        throw new BadArgs("err.missing.arg", name).showUsage(true);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   506
                    }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   507
                }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   508
                option.process(this, name, param);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   509
                if (option.ignoreRest()) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   510
                    i = args.length;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   511
                }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   512
            } else {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   513
                // process rest of the input arguments
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   514
                for (; i < args.length; i++) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   515
                    String name = args[i];
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   516
                    if (name.charAt(0) == '-') {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   517
                        throw new BadArgs("err.option.after.class", name).showUsage(true);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   518
                    }
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   519
                    classes.add(name);
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   520
                }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   521
            }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   522
        }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   523
    }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   524
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   525
    private Option getOption(String name) throws BadArgs {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   526
        for (Option o : recognizedOptions) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   527
            if (o.matches(name)) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   528
                return o;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   529
            }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   530
        }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   531
        throw new BadArgs("err.unknown.option", name).showUsage(true);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   532
    }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   533
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   534
    private void reportError(String key, Object... args) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   535
        log.println(getMessage("error.prefix") + " " + getMessage(key, args));
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   536
    }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   537
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   538
    private void warning(String key, Object... args) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   539
        log.println(getMessage("warn.prefix") + " " + getMessage(key, args));
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   540
    }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   541
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   542
    private void showHelp() {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   543
        log.println(getMessage("main.usage", PROGNAME));
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   544
        for (Option o : recognizedOptions) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   545
            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
   546
            name = name.charAt(0) == '-' ? name.substring(1) : name;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   547
            if (o.isHidden() || name.equals("h")) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   548
                continue;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   549
            }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   550
            log.println(getMessage("main.opt." + name));
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   551
        }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   552
    }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   553
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   554
    private void showVersion(boolean full) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   555
        log.println(version(full ? "full" : "release"));
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   556
    }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   557
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   558
    private String version(String key) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   559
        // key=version:  mm.nn.oo[-milestone]
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   560
        // key=full:     mm.mm.oo[-milestone]-build
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   561
        if (ResourceBundleHelper.versionRB == null) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   562
            return System.getProperty("java.version");
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   563
        }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   564
        try {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   565
            return ResourceBundleHelper.versionRB.getString(key);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   566
        } catch (MissingResourceException e) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   567
            return getMessage("version.unknown", System.getProperty("java.version"));
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   568
        }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   569
    }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   570
16290
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents: 15030
diff changeset
   571
    static String getMessage(String key, Object... args) {
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   572
        try {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   573
            return MessageFormat.format(ResourceBundleHelper.bundle.getString(key), args);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   574
        } catch (MissingResourceException e) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   575
            throw new InternalError("Missing message: " + key);
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
    private static class Options {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   580
        boolean help;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   581
        boolean version;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   582
        boolean fullVersion;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   583
        boolean showProfile;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   584
        boolean showSummary;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   585
        boolean wildcard;
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   586
        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
   587
        boolean showLabel;
22007
907f7054db16 8029216: (jdeps) Provide a specific option to report JDK internal APIs
mchung
parents: 21503
diff changeset
   588
        boolean findJDKInternals;
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   589
        String dotOutputDir;
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   590
        String classpath = "";
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   591
        int depth = 1;
16290
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents: 15030
diff changeset
   592
        Analyzer.Type verbose = Analyzer.Type.PACKAGE;
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   593
        Set<String> packageNames = new HashSet<>();
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   594
        String regex;             // apply to the dependences
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   595
        Pattern includePattern;   // apply to classes
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   596
    }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   597
    private static class ResourceBundleHelper {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   598
        static final ResourceBundle versionRB;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   599
        static final ResourceBundle bundle;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   600
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   601
        static {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   602
            Locale locale = Locale.getDefault();
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   603
            try {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   604
                bundle = ResourceBundle.getBundle("com.sun.tools.jdeps.resources.jdeps", locale);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   605
            } catch (MissingResourceException e) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   606
                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
   607
            }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   608
            try {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   609
                versionRB = ResourceBundle.getBundle("com.sun.tools.jdeps.resources.version");
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   610
            } catch (MissingResourceException e) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   611
                throw new InternalError("version.resource.missing");
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   612
            }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   613
        }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   614
    }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   615
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   616
    private List<Archive> getArchives(List<String> filenames) throws IOException {
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 22007
diff changeset
   617
        List<Archive> result = new ArrayList<>();
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   618
        for (String s : filenames) {
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   619
            Path p = Paths.get(s);
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   620
            if (Files.exists(p)) {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   621
                result.add(new Archive(p, ClassFileReader.newInstance(p)));
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   622
            } else {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   623
                warning("warn.file.not.exist", s);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   624
            }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   625
        }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   626
        return result;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   627
    }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   628
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   629
    private List<Archive> getClassPathArchives(String paths) throws IOException {
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   630
        List<Archive> result = new ArrayList<>();
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   631
        if (paths.isEmpty()) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   632
            return result;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   633
        }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   634
        for (String p : paths.split(File.pathSeparator)) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   635
            if (p.length() > 0) {
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   636
                List<Path> files = new ArrayList<>();
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   637
                // 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
   638
                int i = p.lastIndexOf(".*");
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   639
                if (i > 0) {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   640
                    Path dir = Paths.get(p.substring(0, i));
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   641
                    try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir, "*.jar")) {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   642
                        for (Path entry : stream) {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   643
                            files.add(entry);
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   644
                        }
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   645
                    }
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   646
                } else {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   647
                    files.add(Paths.get(p));
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   648
                }
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   649
                for (Path f : files) {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   650
                    if (Files.exists(f)) {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   651
                        result.add(new Archive(f, ClassFileReader.newInstance(f)));
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   652
                    }
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   653
                }
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
        return result;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   657
    }
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   658
21503
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   659
    /**
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   660
     * If the given archive is JDK archive and non-null Profile,
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   661
     * this method returns the profile name only if -profile option is specified;
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   662
     * a null profile indicates it accesses a private JDK API and this method
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   663
     * will return "JDK internal API".
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   664
     *
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   665
     * For non-JDK archives, this method returns the file name of the archive.
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   666
     */
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   667
    private String getProfileArchiveInfo(Archive source, Profile profile) {
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   668
        if (options.showProfile && profile != null)
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   669
            return profile.toString();
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   670
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   671
        if (source instanceof JDKArchive) {
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   672
            return profile == null ? "JDK internal API (" + source.getFileName() + ")" : "";
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   673
        }
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   674
        return source.getFileName();
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   675
    }
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   676
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   677
    /**
21503
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   678
     * Returns the profile name or "JDK internal API" for JDK archive;
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   679
     * otherwise empty string.
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   680
     */
21503
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   681
    private String profileName(Archive archive, Profile profile) {
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   682
        if (archive instanceof JDKArchive) {
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   683
            return Objects.toString(profile, "JDK internal API");
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   684
        } else {
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   685
            return "";
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   686
        }
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   687
    }
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   688
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   689
    class RawOutputFormatter implements Analyzer.Visitor {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   690
        private final PrintWriter writer;
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   691
        RawOutputFormatter(PrintWriter writer) {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   692
            this.writer = writer;
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   693
        }
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   694
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   695
        private String pkg = "";
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   696
        @Override
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   697
        public void visitDependence(String origin, Archive source,
21503
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   698
                                    String target, Archive archive, Profile profile) {
22007
907f7054db16 8029216: (jdeps) Provide a specific option to report JDK internal APIs
mchung
parents: 21503
diff changeset
   699
            if (options.findJDKInternals &&
907f7054db16 8029216: (jdeps) Provide a specific option to report JDK internal APIs
mchung
parents: 21503
diff changeset
   700
                    !(archive instanceof JDKArchive && profile == null)) {
907f7054db16 8029216: (jdeps) Provide a specific option to report JDK internal APIs
mchung
parents: 21503
diff changeset
   701
                // filter dependences other than JDK internal APIs
907f7054db16 8029216: (jdeps) Provide a specific option to report JDK internal APIs
mchung
parents: 21503
diff changeset
   702
                return;
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   703
            }
22007
907f7054db16 8029216: (jdeps) Provide a specific option to report JDK internal APIs
mchung
parents: 21503
diff changeset
   704
            if (options.verbose == Analyzer.Type.VERBOSE) {
907f7054db16 8029216: (jdeps) Provide a specific option to report JDK internal APIs
mchung
parents: 21503
diff changeset
   705
                writer.format("   %-50s -> %-50s %s%n",
907f7054db16 8029216: (jdeps) Provide a specific option to report JDK internal APIs
mchung
parents: 21503
diff changeset
   706
                              origin, target, getProfileArchiveInfo(archive, profile));
907f7054db16 8029216: (jdeps) Provide a specific option to report JDK internal APIs
mchung
parents: 21503
diff changeset
   707
            } else {
907f7054db16 8029216: (jdeps) Provide a specific option to report JDK internal APIs
mchung
parents: 21503
diff changeset
   708
                if (!origin.equals(pkg)) {
907f7054db16 8029216: (jdeps) Provide a specific option to report JDK internal APIs
mchung
parents: 21503
diff changeset
   709
                    pkg = origin;
907f7054db16 8029216: (jdeps) Provide a specific option to report JDK internal APIs
mchung
parents: 21503
diff changeset
   710
                    writer.format("   %s (%s)%n", origin, source.getFileName());
907f7054db16 8029216: (jdeps) Provide a specific option to report JDK internal APIs
mchung
parents: 21503
diff changeset
   711
                }
907f7054db16 8029216: (jdeps) Provide a specific option to report JDK internal APIs
mchung
parents: 21503
diff changeset
   712
                writer.format("      -> %-50s %s%n",
907f7054db16 8029216: (jdeps) Provide a specific option to report JDK internal APIs
mchung
parents: 21503
diff changeset
   713
                              target, getProfileArchiveInfo(archive, profile));
907f7054db16 8029216: (jdeps) Provide a specific option to report JDK internal APIs
mchung
parents: 21503
diff changeset
   714
            }
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   715
        }
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   716
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   717
        @Override
21503
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   718
        public void visitArchiveDependence(Archive origin, Archive target, Profile profile) {
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   719
            writer.format("%s -> %s", origin.getPathName(), target.getPathName());
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   720
            if (options.showProfile && profile != null) {
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   721
                writer.format(" (%s)%n", profile);
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   722
            } else {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   723
                writer.format("%n");
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   724
            }
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   725
        }
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   726
    }
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   727
21503
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   728
    class DotFileFormatter extends DotGraph<String> implements AutoCloseable {
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   729
        private final PrintWriter writer;
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   730
        private final String name;
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   731
        DotFileFormatter(PrintWriter writer, Archive archive) {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   732
            this.writer = writer;
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   733
            this.name = archive.getFileName();
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   734
            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
   735
            writer.format("    // Path: %s%n", archive.getPathName());
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   736
        }
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   737
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   738
        @Override
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   739
        public void close() {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   740
            writer.println("}");
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   741
        }
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   742
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   743
        @Override
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   744
        public void visitDependence(String origin, Archive source,
21503
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   745
                                    String target, Archive archive, Profile profile) {
22007
907f7054db16 8029216: (jdeps) Provide a specific option to report JDK internal APIs
mchung
parents: 21503
diff changeset
   746
            if (options.findJDKInternals &&
907f7054db16 8029216: (jdeps) Provide a specific option to report JDK internal APIs
mchung
parents: 21503
diff changeset
   747
                    !(archive instanceof JDKArchive && profile == null)) {
907f7054db16 8029216: (jdeps) Provide a specific option to report JDK internal APIs
mchung
parents: 21503
diff changeset
   748
                // filter dependences other than JDK internal APIs
907f7054db16 8029216: (jdeps) Provide a specific option to report JDK internal APIs
mchung
parents: 21503
diff changeset
   749
                return;
907f7054db16 8029216: (jdeps) Provide a specific option to report JDK internal APIs
mchung
parents: 21503
diff changeset
   750
            }
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   751
            // if -P option is specified, package name -> profile will
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   752
            // be shown and filter out multiple same edges.
21503
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   753
            String name = getProfileArchiveInfo(archive, profile);
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   754
            writeEdge(writer, new Edge(origin, target, getProfileArchiveInfo(archive, profile)));
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   755
        }
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   756
        @Override
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   757
        public void visitArchiveDependence(Archive origin, Archive target, Profile profile) {
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   758
            throw new UnsupportedOperationException();
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   759
        }
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   760
    }
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   761
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   762
    class DotSummaryForArchive extends DotGraph<Archive> {
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   763
        @Override
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   764
        public void visitDependence(String origin, Archive source,
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   765
                                    String target, Archive archive, Profile profile) {
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   766
            Edge e = findEdge(source, archive);
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   767
            assert e != null;
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   768
            // add the dependency to the label if enabled and not compact1
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   769
            if (profile == Profile.COMPACT1) {
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   770
                return;
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   771
            }
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   772
            e.addLabel(origin, target, profileName(archive, profile));
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   773
        }
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   774
        @Override
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   775
        public void visitArchiveDependence(Archive origin, Archive target, Profile profile) {
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   776
            // add an edge with the archive's name with no tag
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   777
            // so that there is only one node for each JDK archive
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   778
            // while there may be edges to different profiles
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   779
            Edge e = addEdge(origin, target, "");
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   780
            if (target instanceof JDKArchive) {
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   781
                // add a label to print the profile
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   782
                if (profile == null) {
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   783
                    e.addLabel("JDK internal API");
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   784
                } else if (options.showProfile && !options.showLabel) {
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   785
                    e.addLabel(profile.toString());
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   786
                }
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   787
            }
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   788
        }
21503
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   789
    }
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   790
21503
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   791
    // DotSummaryForPackage generates the summary.dot file for verbose mode
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   792
    // (-v or -verbose option) that includes all class dependencies.
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   793
    // The summary.dot file shows package-level dependencies.
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   794
    class DotSummaryForPackage extends DotGraph<String> {
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   795
        private String packageOf(String cn) {
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   796
            int i = cn.lastIndexOf('.');
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   797
            return i > 0 ? cn.substring(0, i) : "<unnamed>";
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   798
        }
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   799
        @Override
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   800
        public void visitDependence(String origin, Archive source,
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   801
                                    String target, Archive archive, Profile profile) {
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   802
            // add a package dependency edge
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   803
            String from = packageOf(origin);
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   804
            String to = packageOf(target);
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   805
            Edge e = addEdge(from, to, getProfileArchiveInfo(archive, profile));
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   806
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   807
            // add the dependency to the label if enabled and not compact1
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   808
            if (!options.showLabel || profile == Profile.COMPACT1) {
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   809
                return;
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   810
            }
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   811
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   812
            // trim the package name of origin to shorten the label
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   813
            int i = origin.lastIndexOf('.');
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   814
            String n1 = i < 0 ? origin : origin.substring(i+1);
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   815
            e.addLabel(n1, target, profileName(archive, profile));
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   816
        }
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   817
        @Override
21503
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   818
        public void visitArchiveDependence(Archive origin, Archive target, Profile profile) {
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   819
            // nop
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   820
        }
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   821
    }
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   822
    abstract class DotGraph<T> implements Analyzer.Visitor  {
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   823
        private final Set<Edge> edges = new LinkedHashSet<>();
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   824
        private Edge curEdge;
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   825
        public void writeTo(PrintWriter writer) {
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   826
            writer.format("digraph \"summary\" {%n");
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   827
            for (Edge e: edges) {
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   828
                writeEdge(writer, e);
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   829
            }
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   830
            writer.println("}");
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   831
        }
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   832
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   833
        void writeEdge(PrintWriter writer, Edge e) {
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   834
            writer.format("   %-50s -> \"%s\"%s;%n",
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   835
                          String.format("\"%s\"", e.from.toString()),
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   836
                          e.tag.isEmpty() ? e.to
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   837
                                          : String.format("%s (%s)", e.to, e.tag),
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   838
                          getLabel(e));
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   839
        }
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   840
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   841
        Edge addEdge(T origin, T target, String tag) {
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   842
            Edge e = new Edge(origin, target, tag);
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   843
            if (e.equals(curEdge)) {
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   844
                return curEdge;
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   845
            }
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   846
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   847
            if (edges.contains(e)) {
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   848
                for (Edge e1 : edges) {
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   849
                   if (e.equals(e1)) {
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   850
                       curEdge = e1;
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   851
                   }
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   852
                }
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   853
            } else {
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   854
                edges.add(e);
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   855
                curEdge = e;
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   856
            }
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   857
            return curEdge;
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   858
        }
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   859
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   860
        Edge findEdge(T origin, T target) {
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   861
            for (Edge e : edges) {
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   862
                if (e.from.equals(origin) && e.to.equals(target)) {
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   863
                    return e;
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   864
                }
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   865
            }
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   866
            return null;
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   867
        }
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   868
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   869
        String getLabel(Edge e) {
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   870
            String label = e.label.toString();
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   871
            return label.isEmpty() ? "" : String.format("[label=\"%s\",fontsize=9]", label);
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   872
        }
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   873
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   874
        class Edge {
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   875
            final T from;
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   876
            final T to;
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   877
            final String tag;  // optional tag
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   878
            final StringBuilder label = new StringBuilder();
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   879
            Edge(T from, T to, String tag) {
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   880
                this.from = from;
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   881
                this.to = to;
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   882
                this.tag = tag;
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   883
            }
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   884
            void addLabel(String s) {
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   885
                label.append(s).append("\\n");
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   886
            }
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   887
            void addLabel(String origin, String target, String profile) {
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   888
                label.append(origin).append(" -> ").append(target);
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   889
                if (!profile.isEmpty()) {
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   890
                    label.append(" (" + profile + ")");
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   891
                }
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   892
                label.append("\\n");
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   893
            }
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   894
            @Override @SuppressWarnings("unchecked")
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   895
            public boolean equals(Object o) {
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   896
                if (o instanceof DotGraph<?>.Edge) {
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   897
                    DotGraph<?>.Edge e = (DotGraph<?>.Edge)o;
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   898
                    return this.from.equals(e.from) &&
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   899
                           this.to.equals(e.to) &&
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   900
                           this.tag.equals(e.tag);
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   901
                }
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   902
                return false;
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   903
            }
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   904
            @Override
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   905
            public int hashCode() {
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   906
                int hash = 7;
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   907
                hash = 67 * hash + Objects.hashCode(this.from) +
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   908
                       Objects.hashCode(this.to) + Objects.hashCode(this.tag);
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   909
                return hash;
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   910
            }
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   911
        }
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   912
    }
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   913
}