langtools/src/share/classes/com/sun/tools/jdeps/JdepsTask.java
author mchung
Thu, 17 Oct 2013 13:19:48 -0700
changeset 21046 ebf16a1a6328
parent 16560 c6c7f0c26568
child 21503 45fc62482cae
permissions -rw-r--r--
8015912: jdeps support to output in dot file format 8026255: Switch jdeps to follow traditional Java option style Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
     1
/*
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
        },
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   183
        new Option(false, "-version") {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   184
            void process(JdepsTask task, String opt, String arg) {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   185
                task.options.version = true;
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   186
            }
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   187
        },
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   188
        new HiddenOption(false, "-fullversion") {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   189
            void process(JdepsTask task, String opt, String arg) {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   190
                task.options.fullVersion = true;
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   191
            }
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   192
        },
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   193
        new HiddenOption(true, "-depth") {
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   194
            void process(JdepsTask task, String opt, String arg) throws BadArgs {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   195
                try {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   196
                    task.options.depth = Integer.parseInt(arg);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   197
                } catch (NumberFormatException e) {
16560
c6c7f0c26568 8010361: fix some langtools findbugs issues
jjg
parents: 16550
diff changeset
   198
                    throw new BadArgs("err.invalid.arg.for.option", opt);
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   199
                }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   200
            }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   201
        },
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   202
    };
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   203
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   204
    private static final String PROGNAME = "jdeps";
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   205
    private final Options options = new Options();
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   206
    private final List<String> classes = new ArrayList<String>();
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   207
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   208
    private PrintWriter log;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   209
    void setLog(PrintWriter out) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   210
        log = out;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   211
    }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   212
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
     * Result codes.
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
    static final int EXIT_OK = 0, // Completed with no errors.
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   217
                     EXIT_ERROR = 1, // Completed but reported errors.
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   218
                     EXIT_CMDERR = 2, // Bad command-line arguments
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   219
                     EXIT_SYSERR = 3, // System error or resource exhaustion.
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   220
                     EXIT_ABNORMAL = 4;// terminated abnormally
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
    int run(String[] args) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   223
        if (log == null) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   224
            log = new PrintWriter(System.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
        try {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   227
            handleOptions(args);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   228
            if (options.help) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   229
                showHelp();
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   230
            }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   231
            if (options.version || options.fullVersion) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   232
                showVersion(options.fullVersion);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   233
            }
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   234
            if (classes.isEmpty() && options.includePattern == null) {
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   235
                if (options.help || options.version || options.fullVersion) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   236
                    return EXIT_OK;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   237
                } else {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   238
                    showHelp();
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   239
                    return EXIT_CMDERR;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   240
                }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   241
            }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   242
            if (options.regex != null && options.packageNames.size() > 0) {
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
                return EXIT_CMDERR;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   245
            }
16290
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents: 15030
diff changeset
   246
            if (options.showSummary && options.verbose != Analyzer.Type.SUMMARY) {
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   247
                showHelp();
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   248
                return EXIT_CMDERR;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   249
            }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   250
            boolean ok = run();
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   251
            return ok ? EXIT_OK : EXIT_ERROR;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   252
        } catch (BadArgs e) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   253
            reportError(e.key, e.args);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   254
            if (e.showUsage) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   255
                log.println(getMessage("main.usage.summary", PROGNAME));
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   256
            }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   257
            return EXIT_CMDERR;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   258
        } catch (IOException e) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   259
            return EXIT_ABNORMAL;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   260
        } finally {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   261
            log.flush();
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   262
        }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   263
    }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   264
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   265
    private final List<Archive> sourceLocations = new ArrayList<>();
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   266
    private boolean run() throws IOException {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   267
        findDependencies();
16290
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents: 15030
diff changeset
   268
        Analyzer analyzer = new Analyzer(options.verbose);
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents: 15030
diff changeset
   269
        analyzer.run(sourceLocations);
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   270
        if (options.dotOutputDir != null) {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   271
            Path dir = Paths.get(options.dotOutputDir);
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   272
            Files.createDirectories(dir);
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   273
            generateDotFiles(dir, analyzer);
16290
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents: 15030
diff changeset
   274
        } else {
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   275
            printRawOutput(log, analyzer);
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   276
        }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   277
        return true;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   278
    }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   279
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   280
    private void generateDotFiles(Path dir, Analyzer analyzer) throws IOException {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   281
        Path summary = dir.resolve("summary.dot");
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   282
        try (PrintWriter sw = new PrintWriter(Files.newOutputStream(summary));
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   283
             DotFileFormatter formatter = new DotFileFormatter(sw, "summary")) {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   284
            for (Archive archive : sourceLocations) {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   285
                 analyzer.visitArchiveDependences(archive, formatter);
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   286
            }
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   287
        }
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   288
        if (options.verbose != Analyzer.Type.SUMMARY) {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   289
            for (Archive archive : sourceLocations) {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   290
                if (analyzer.hasDependences(archive)) {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   291
                    Path dotfile = dir.resolve(archive.getFileName() + ".dot");
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   292
                    try (PrintWriter pw = new PrintWriter(Files.newOutputStream(dotfile));
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   293
                         DotFileFormatter formatter = new DotFileFormatter(pw, archive)) {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   294
                        analyzer.visitDependences(archive, formatter);
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   295
                    }
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   296
                }
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   297
            }
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   298
        }
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   299
    }
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   300
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   301
    private void printRawOutput(PrintWriter writer, Analyzer analyzer) {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   302
        for (Archive archive : sourceLocations) {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   303
            RawOutputFormatter formatter = new RawOutputFormatter(writer);
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   304
            analyzer.visitArchiveDependences(archive, formatter);
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   305
            if (options.verbose != Analyzer.Type.SUMMARY) {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   306
                analyzer.visitDependences(archive, formatter);
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   307
            }
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   308
        }
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   309
    }
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   310
    private boolean isValidClassName(String name) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   311
        if (!Character.isJavaIdentifierStart(name.charAt(0))) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   312
            return false;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   313
        }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   314
        for (int i=1; i < name.length(); i++) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   315
            char c = name.charAt(i);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   316
            if (c != '.'  && !Character.isJavaIdentifierPart(c)) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   317
                return false;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   318
            }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   319
        }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   320
        return true;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   321
    }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   322
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   323
    private Dependency.Filter getDependencyFilter() {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   324
         if (options.regex != null) {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   325
            return Dependencies.getRegexFilter(Pattern.compile(options.regex));
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   326
        } else if (options.packageNames.size() > 0) {
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   327
            return Dependencies.getPackageFilter(options.packageNames, false);
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   328
        } else {
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   329
            return new Dependency.Filter() {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   330
                @Override
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   331
                public boolean accepts(Dependency dependency) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   332
                    return !dependency.getOrigin().equals(dependency.getTarget());
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   333
                }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   334
            };
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   335
        }
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   336
    }
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   337
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   338
    private boolean matches(String classname, AccessFlags flags) {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   339
        if (options.apiOnly && !flags.is(AccessFlags.ACC_PUBLIC)) {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   340
            return false;
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   341
        } else if (options.includePattern != null) {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   342
            return options.includePattern.matcher(classname.replace('/', '.')).matches();
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   343
        } else {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   344
            return true;
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   345
        }
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   346
    }
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   347
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   348
    private void findDependencies() throws IOException {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   349
        Dependency.Finder finder =
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   350
            options.apiOnly ? Dependencies.getAPIFinder(AccessFlags.ACC_PROTECTED)
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   351
                            : Dependencies.getClassDependencyFinder();
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   352
        Dependency.Filter filter = getDependencyFilter();
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   353
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   354
        List<Archive> archives = new ArrayList<>();
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   355
        Deque<String> roots = new LinkedList<>();
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   356
        for (String s : classes) {
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   357
            Path p = Paths.get(s);
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   358
            if (Files.exists(p)) {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   359
                archives.add(new Archive(p, ClassFileReader.newInstance(p)));
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   360
            } else {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   361
                if (isValidClassName(s)) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   362
                    roots.add(s);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   363
                } else {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   364
                    warning("warn.invalid.arg", s);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   365
                }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   366
            }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   367
        }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   368
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   369
        List<Archive> classpaths = new ArrayList<>(); // for class file lookup
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   370
        if (options.includePattern != null) {
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   371
            archives.addAll(getClassPathArchives(options.classpath));
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   372
        } else {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   373
            classpaths.addAll(getClassPathArchives(options.classpath));
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   374
        }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   375
        classpaths.addAll(PlatformClassPath.getArchives());
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   376
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   377
        // add all archives to the source locations for reporting
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   378
        sourceLocations.addAll(archives);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   379
        sourceLocations.addAll(classpaths);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   380
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   381
        // Work queue of names of classfiles to be searched.
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   382
        // 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
   383
        // dependencies in the results map.
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   384
        Deque<String> deque = new LinkedList<>();
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   385
        Set<String> doneClasses = new HashSet<>();
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   386
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   387
        // get the immediate dependencies of the input files
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   388
        for (Archive a : archives) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   389
            for (ClassFile cf : a.reader().getClassFiles()) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   390
                String classFileName;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   391
                try {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   392
                    classFileName = cf.getName();
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   393
                } catch (ConstantPoolException e) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   394
                    throw new ClassFileError(e);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   395
                }
16290
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents: 15030
diff changeset
   396
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   397
                if (matches(classFileName, cf.access_flags)) {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   398
                    if (!doneClasses.contains(classFileName)) {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   399
                        doneClasses.add(classFileName);
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   400
                    }
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   401
                    for (Dependency d : finder.findDependencies(cf)) {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   402
                        if (filter.accepts(d)) {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   403
                            String cn = d.getTarget().getName();
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   404
                            if (!doneClasses.contains(cn) && !deque.contains(cn)) {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   405
                                deque.add(cn);
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   406
                            }
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   407
                            a.addClass(d.getOrigin(), d.getTarget());
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   408
                        }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   409
                    }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   410
                }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   411
            }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   412
        }
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
        // add Archive for looking up classes from the classpath
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   415
        // for transitive dependency analysis
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   416
        Deque<String> unresolved = roots;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   417
        int depth = options.depth > 0 ? options.depth : Integer.MAX_VALUE;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   418
        do {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   419
            String name;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   420
            while ((name = unresolved.poll()) != null) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   421
                if (doneClasses.contains(name)) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   422
                    continue;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   423
                }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   424
                ClassFile cf = null;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   425
                for (Archive a : classpaths) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   426
                    cf = a.reader().getClassFile(name);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   427
                    if (cf != null) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   428
                        String classFileName;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   429
                        try {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   430
                            classFileName = cf.getName();
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   431
                        } catch (ConstantPoolException e) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   432
                            throw new ClassFileError(e);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   433
                        }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   434
                        if (!doneClasses.contains(classFileName)) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   435
                            // if name is a fully-qualified class name specified
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   436
                            // from command-line, this class might already be parsed
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   437
                            doneClasses.add(classFileName);
16290
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents: 15030
diff changeset
   438
                            for (Dependency d : finder.findDependencies(cf)) {
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents: 15030
diff changeset
   439
                                if (depth == 0) {
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents: 15030
diff changeset
   440
                                    // ignore the dependency
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents: 15030
diff changeset
   441
                                    a.addClass(d.getOrigin());
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents: 15030
diff changeset
   442
                                    break;
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents: 15030
diff changeset
   443
                                } else if (filter.accepts(d)) {
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents: 15030
diff changeset
   444
                                    a.addClass(d.getOrigin(), d.getTarget());
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents: 15030
diff changeset
   445
                                    String cn = d.getTarget().getName();
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents: 15030
diff changeset
   446
                                    if (!doneClasses.contains(cn) && !deque.contains(cn)) {
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents: 15030
diff changeset
   447
                                        deque.add(cn);
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   448
                                    }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   449
                                }
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
                        }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   452
                        break;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   453
                    }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   454
                }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   455
                if (cf == null) {
16290
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents: 15030
diff changeset
   456
                    doneClasses.add(name);
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   457
                }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   458
            }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   459
            unresolved = deque;
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   460
            deque = new LinkedList<>();
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   461
        } while (!unresolved.isEmpty() && depth-- > 0);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   462
    }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   463
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   464
    public void handleOptions(String[] args) throws BadArgs {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   465
        // process options
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   466
        for (int i=0; i < args.length; i++) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   467
            if (args[i].charAt(0) == '-') {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   468
                String name = args[i];
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   469
                Option option = getOption(name);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   470
                String param = null;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   471
                if (option.hasArg) {
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   472
                    if (name.startsWith("-") && name.indexOf('=') > 0) {
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   473
                        param = name.substring(name.indexOf('=') + 1, name.length());
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   474
                    } else if (i + 1 < args.length) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   475
                        param = args[++i];
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
                    if (param == null || param.isEmpty() || param.charAt(0) == '-') {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   478
                        throw new BadArgs("err.missing.arg", name).showUsage(true);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   479
                    }
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
                option.process(this, name, param);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   482
                if (option.ignoreRest()) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   483
                    i = args.length;
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
            } else {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   486
                // process rest of the input arguments
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   487
                for (; i < args.length; i++) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   488
                    String name = args[i];
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   489
                    if (name.charAt(0) == '-') {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   490
                        throw new BadArgs("err.option.after.class", name).showUsage(true);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   491
                    }
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   492
                    classes.add(name);
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   493
                }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   494
            }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   495
        }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   496
    }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   497
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   498
    private Option getOption(String name) throws BadArgs {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   499
        for (Option o : recognizedOptions) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   500
            if (o.matches(name)) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   501
                return o;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   502
            }
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
        throw new BadArgs("err.unknown.option", name).showUsage(true);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   505
    }
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
    private void reportError(String key, Object... args) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   508
        log.println(getMessage("error.prefix") + " " + getMessage(key, args));
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   509
    }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   510
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   511
    private void warning(String key, Object... args) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   512
        log.println(getMessage("warn.prefix") + " " + getMessage(key, args));
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   513
    }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   514
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   515
    private void showHelp() {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   516
        log.println(getMessage("main.usage", PROGNAME));
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   517
        for (Option o : recognizedOptions) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   518
            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
   519
            name = name.charAt(0) == '-' ? name.substring(1) : name;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   520
            if (o.isHidden() || name.equals("h")) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   521
                continue;
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
            log.println(getMessage("main.opt." + name));
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
    }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   526
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   527
    private void showVersion(boolean full) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   528
        log.println(version(full ? "full" : "release"));
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
    private String version(String key) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   532
        // key=version:  mm.nn.oo[-milestone]
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   533
        // key=full:     mm.mm.oo[-milestone]-build
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   534
        if (ResourceBundleHelper.versionRB == null) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   535
            return System.getProperty("java.version");
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
        try {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   538
            return ResourceBundleHelper.versionRB.getString(key);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   539
        } catch (MissingResourceException e) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   540
            return getMessage("version.unknown", System.getProperty("java.version"));
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
    }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   543
16290
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents: 15030
diff changeset
   544
    static String getMessage(String key, Object... args) {
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   545
        try {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   546
            return MessageFormat.format(ResourceBundleHelper.bundle.getString(key), args);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   547
        } catch (MissingResourceException e) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   548
            throw new InternalError("Missing message: " + key);
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
    }
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
    private static class Options {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   553
        boolean help;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   554
        boolean version;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   555
        boolean fullVersion;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   556
        boolean showProfile;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   557
        boolean showSummary;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   558
        boolean wildcard;
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   559
        boolean apiOnly;
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   560
        String dotOutputDir;
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   561
        String classpath = "";
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   562
        int depth = 1;
16290
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents: 15030
diff changeset
   563
        Analyzer.Type verbose = Analyzer.Type.PACKAGE;
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   564
        Set<String> packageNames = new HashSet<>();
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   565
        String regex;             // apply to the dependences
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   566
        Pattern includePattern;   // apply to classes
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   567
    }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   568
    private static class ResourceBundleHelper {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   569
        static final ResourceBundle versionRB;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   570
        static final ResourceBundle bundle;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   571
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   572
        static {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   573
            Locale locale = Locale.getDefault();
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   574
            try {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   575
                bundle = ResourceBundle.getBundle("com.sun.tools.jdeps.resources.jdeps", locale);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   576
            } catch (MissingResourceException e) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   577
                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
   578
            }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   579
            try {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   580
                versionRB = ResourceBundle.getBundle("com.sun.tools.jdeps.resources.version");
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   581
            } catch (MissingResourceException e) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   582
                throw new InternalError("version.resource.missing");
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   583
            }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   584
        }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   585
    }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   586
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   587
    private List<Archive> getArchives(List<String> filenames) throws IOException {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   588
        List<Archive> result = new ArrayList<Archive>();
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   589
        for (String s : filenames) {
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   590
            Path p = Paths.get(s);
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   591
            if (Files.exists(p)) {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   592
                result.add(new Archive(p, ClassFileReader.newInstance(p)));
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   593
            } else {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   594
                warning("warn.file.not.exist", s);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   595
            }
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
        return result;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   598
    }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   599
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   600
    private List<Archive> getClassPathArchives(String paths) throws IOException {
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   601
        List<Archive> result = new ArrayList<>();
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   602
        if (paths.isEmpty()) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   603
            return result;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   604
        }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   605
        for (String p : paths.split(File.pathSeparator)) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   606
            if (p.length() > 0) {
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   607
                List<Path> files = new ArrayList<>();
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   608
                // 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
   609
                int i = p.lastIndexOf(".*");
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   610
                if (i > 0) {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   611
                    Path dir = Paths.get(p.substring(0, i));
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   612
                    try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir, "*.jar")) {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   613
                        for (Path entry : stream) {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   614
                            files.add(entry);
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   615
                        }
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   616
                    }
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   617
                } else {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   618
                    files.add(Paths.get(p));
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   619
                }
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   620
                for (Path f : files) {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   621
                    if (Files.exists(f)) {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   622
                        result.add(new Archive(f, ClassFileReader.newInstance(f)));
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   623
                    }
15030
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
        }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   627
        return result;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   628
    }
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   629
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   630
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   631
    /**
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   632
     * Returns the file name of the archive for non-JRE class or
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   633
     * internal JRE classes.  It returns empty string for SE API.
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   634
     */
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   635
    private static String getArchiveName(Archive source, String profile) {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   636
        String name = source.getFileName();
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   637
        if (source instanceof JDKArchive)
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   638
            return profile.isEmpty() ? "JDK internal API (" + name + ")" : "";
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   639
        return name;
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   640
    }
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   641
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   642
    class RawOutputFormatter implements Analyzer.Visitor {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   643
        private final PrintWriter writer;
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   644
        RawOutputFormatter(PrintWriter writer) {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   645
            this.writer = writer;
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   646
        }
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   647
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   648
        private String pkg = "";
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   649
        @Override
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   650
        public void visitDependence(String origin, Archive source,
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   651
                                    String target, Archive archive, String profile) {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   652
            if (!origin.equals(pkg)) {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   653
                pkg = origin;
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   654
                writer.format("   %s (%s)%n", origin, source.getFileName());
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   655
            }
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   656
            String name = (options.showProfile && !profile.isEmpty())
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   657
                                ? profile
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   658
                                : getArchiveName(archive, profile);
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   659
            writer.format("      -> %-50s %s%n", target, name);
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   660
        }
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   661
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   662
        @Override
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   663
        public void visitArchiveDependence(Archive origin, Archive target, String profile) {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   664
            writer.format("%s -> %s", origin, target);
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   665
            if (options.showProfile && !profile.isEmpty()) {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   666
                writer.format(" (%s)%n", profile);
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   667
            } else {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   668
                writer.format("%n");
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   669
            }
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   670
        }
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   671
    }
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   672
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   673
    class DotFileFormatter implements Analyzer.Visitor, AutoCloseable {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   674
        private final PrintWriter writer;
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   675
        private final String name;
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   676
        DotFileFormatter(PrintWriter writer, String name) {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   677
            this.writer = writer;
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   678
            this.name = name;
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   679
            writer.format("digraph \"%s\" {%n", name);
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   680
        }
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   681
        DotFileFormatter(PrintWriter writer, Archive archive) {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   682
            this.writer = writer;
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   683
            this.name = archive.getFileName();
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   684
            writer.format("digraph \"%s\" {%n", name);
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   685
            writer.format("    // Path: %s%n", archive.toString());
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   686
        }
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
        @Override
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   689
        public void close() {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   690
            writer.println("}");
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   691
        }
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   692
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   693
        private final Set<String> edges = new HashSet<>();
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   694
        private String node = "";
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   695
        @Override
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   696
        public void visitDependence(String origin, Archive source,
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   697
                                    String target, Archive archive, String profile) {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   698
            if (!node.equals(origin)) {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   699
                edges.clear();
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   700
                node = origin;
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   701
            }
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   702
            // if -P option is specified, package name -> profile will
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   703
            // be shown and filter out multiple same edges.
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   704
            if (!edges.contains(target)) {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   705
                StringBuilder sb = new StringBuilder();
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   706
                String name = options.showProfile && !profile.isEmpty()
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   707
                                  ? profile
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   708
                                  : getArchiveName(archive, profile);
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   709
                writer.format("   %-50s -> %s;%n",
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   710
                                 String.format("\"%s\"", origin),
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   711
                                 name.isEmpty() ? String.format("\"%s\"", target)
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   712
                                                :  String.format("\"%s (%s)\"", target, name));
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   713
                edges.add(target);
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   714
            }
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
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   718
        public void visitArchiveDependence(Archive origin, Archive target, String profile) {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   719
             String name = options.showProfile && !profile.isEmpty()
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   720
                                ? profile : "";
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   721
             writer.format("   %-30s -> \"%s\";%n",
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   722
                           String.format("\"%s\"", origin.getFileName()),
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   723
                           name.isEmpty()
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   724
                               ? target.getFileName()
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   725
                               : String.format("%s (%s)", target.getFileName(), name));
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
    }
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   728
}