test/langtools/tools/jdeps/APIDeps.java
author egahlin
Mon, 25 Nov 2019 14:06:13 +0100
changeset 59246 fcad92f425c5
parent 47216 71c04702a3d5
permissions -rw-r--r--
8234703: JFR TestOutOfProcessMigration.java should clean up files Reviewed-by: mgronlun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
     1
/*
37848
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
     2
 * Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved.
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
     4
 *
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
     7
 * published by the Free Software Foundation.
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
     8
 *
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
    13
 * accompanied this code).
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
    14
 *
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
    18
 *
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
    21
 * questions.
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
    22
 */
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
    23
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
    24
/*
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
    25
 * @test
25692
39537fdca12c 8050804: (jdeps) Recommend supported API to replace use of JDK internal API
mchung
parents: 25442
diff changeset
    26
 * @bug 8015912 8029216 8048063 8050804
22007
907f7054db16 8029216: (jdeps) Provide a specific option to report JDK internal APIs
mchung
parents: 21046
diff changeset
    27
 * @summary Test -apionly and -jdkinternals options
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 37848
diff changeset
    28
 * @library lib
33021
8a0d5889acea 8139371: Three langtools test failures after the removal of sun.misc.Lock
chegar
parents: 30846
diff changeset
    29
 * @modules java.base/sun.security.x509
30730
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 27579
diff changeset
    30
 *          java.management
30846
2b3f379840f0 8074432: Move jdeps and javap to jdk.jdeps module
mchung
parents: 30730
diff changeset
    31
 *          jdk.jdeps/com.sun.tools.classfile
2b3f379840f0 8074432: Move jdeps and javap to jdk.jdeps module
mchung
parents: 30730
diff changeset
    32
 *          jdk.jdeps/com.sun.tools.jdeps
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
    33
 * @run main APIDeps
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
    34
 */
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
    35
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
    36
import java.io.File;
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
    37
import java.io.IOException;
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
    38
import java.io.PrintWriter;
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
    39
import java.io.StringWriter;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33021
diff changeset
    40
import java.nio.file.*;
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
    41
import java.util.*;
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
    42
import java.util.regex.*;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33021
diff changeset
    43
import java.util.stream.Collectors;
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
    44
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
    45
public class APIDeps {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
    46
    public static void main(String... args) throws Exception {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
    47
        int errors = 0;
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
    48
        errors += new APIDeps().run();
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
    49
        if (errors > 0)
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
    50
            throw new Exception(errors + " errors found");
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
    51
    }
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
    52
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33021
diff changeset
    53
    private static final Path dest = Paths.get(System.getProperty("test.classes", "."), "tmp");
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33021
diff changeset
    54
    private static final String[] srcDirs = new String[] {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33021
diff changeset
    55
            "m", "b", "c", "d", "e", "f", "g"
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33021
diff changeset
    56
    };
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33021
diff changeset
    57
    void setup(Path dest) throws IOException {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33021
diff changeset
    58
        CompilerUtils.cleanDir(dest);
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33021
diff changeset
    59
        Files.createDirectories(dest);
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33021
diff changeset
    60
        Path testsrc = Paths.get(System.getProperty("test.src"));
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33021
diff changeset
    61
        List<String> options = new ArrayList<>();
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33021
diff changeset
    62
43258
45757c4367f2 8171380: Remove all exports from jdk.jlink
chegar
parents: 41251
diff changeset
    63
        // jdk.jdeps is a service provider module so needs to be explicitly included
45757c4367f2 8171380: Remove all exports from jdk.jlink
chegar
parents: 41251
diff changeset
    64
        options.add("--add-modules=jdk.jdeps");
45757c4367f2 8171380: Remove all exports from jdk.jlink
chegar
parents: 41251
diff changeset
    65
40308
274367a99f98 8136930: Simplify use of module-system options by custom launchers
jjg
parents: 38524
diff changeset
    66
        // add --add-exports
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33021
diff changeset
    67
        String testModules = System.getProperty("test.modules", "");
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33021
diff changeset
    68
        List<String> addExports = new ArrayList<>();
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33021
diff changeset
    69
        for (String s : testModules.split("\\s+")) {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33021
diff changeset
    70
            if (s.isEmpty()) continue;
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33021
diff changeset
    71
            if (s.indexOf('/') != -1)
40308
274367a99f98 8136930: Simplify use of module-system options by custom launchers
jjg
parents: 38524
diff changeset
    72
                addExports.add("--add-exports=" + s.trim() + "=ALL-UNNAMED");
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33021
diff changeset
    73
        }
37848
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
    74
        options.addAll(addExports);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33021
diff changeset
    75
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33021
diff changeset
    76
        for (String dir : srcDirs) {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33021
diff changeset
    77
            Path source = testsrc.resolve(dir);
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33021
diff changeset
    78
            boolean ok = CompilerUtils.compile(source, dest, options.toArray(new String[0]));
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33021
diff changeset
    79
            if (!ok) {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33021
diff changeset
    80
                throw new RuntimeException("compilation fails");
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33021
diff changeset
    81
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33021
diff changeset
    82
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33021
diff changeset
    83
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33021
diff changeset
    84
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
    85
    int run() throws IOException {
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33021
diff changeset
    86
        // compile classes in a separate directory for analysis
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33021
diff changeset
    87
        setup(dest);
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33021
diff changeset
    88
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33021
diff changeset
    89
        File testDir = dest.toFile();
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
    90
        String testDirBasename = testDir.toPath().getFileName().toString();
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
    91
        File mDir = new File(testDir, "m");
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
    92
        // all dependencies
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
    93
        test(new File(mDir, "Bar.class"),
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
    94
             new String[] {"java.lang.Object", "java.lang.String",
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
    95
                           "java.util.Set", "java.util.HashSet",
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
    96
                           "java.lang.management.ManagementFactory",
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
    97
                           "java.lang.management.RuntimeMXBean",
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
    98
                           "b.B", "c.C", "d.D", "f.F", "g.G"},
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
    99
             new String[] {"compact1", "compact3", testDirBasename},
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   100
             new String[] {"-classpath", testDir.getPath(), "-verbose", "-P"});
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   101
        test(new File(mDir, "Foo.class"),
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22448
diff changeset
   102
             new String[] {"c.I", "e.E", "f.F"},
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22448
diff changeset
   103
             new String[] {testDirBasename},
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22448
diff changeset
   104
             new String[] {"-classpath", testDir.getPath(), "-verbose:class", "-P"});
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22448
diff changeset
   105
        test(new File(mDir, "Foo.class"),
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   106
             new String[] {"c.I", "e.E", "f.F", "m.Bar"},
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   107
             new String[] {testDirBasename},
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22448
diff changeset
   108
             new String[] {"-classpath", testDir.getPath(), "-verbose:class", "-filter:none", "-P"});
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   109
        test(new File(mDir, "Gee.class"),
33021
8a0d5889acea 8139371: Three langtools test failures after the removal of sun.misc.Lock
chegar
parents: 30846
diff changeset
   110
             new String[] {"g.G", "sun.security.x509.X509CertInfo", "com.sun.tools.classfile.ClassFile",
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22448
diff changeset
   111
                           "com.sun.management.ThreadMXBean", "com.sun.source.tree.BinaryTree"},
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22448
diff changeset
   112
             new String[] {testDirBasename, "JDK internal API", "compact3", ""},
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22448
diff changeset
   113
             new String[] {"-classpath", testDir.getPath(), "-verbose", "-P"});
22007
907f7054db16 8029216: (jdeps) Provide a specific option to report JDK internal APIs
mchung
parents: 21046
diff changeset
   114
907f7054db16 8029216: (jdeps) Provide a specific option to report JDK internal APIs
mchung
parents: 21046
diff changeset
   115
        // -jdkinternals
907f7054db16 8029216: (jdeps) Provide a specific option to report JDK internal APIs
mchung
parents: 21046
diff changeset
   116
        test(new File(mDir, "Gee.class"),
33021
8a0d5889acea 8139371: Three langtools test failures after the removal of sun.misc.Lock
chegar
parents: 30846
diff changeset
   117
             new String[] {"sun.security.x509.X509CertInfo", "com.sun.tools.classfile.ClassFile"},
22007
907f7054db16 8029216: (jdeps) Provide a specific option to report JDK internal APIs
mchung
parents: 21046
diff changeset
   118
             new String[] {"JDK internal API"},
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33021
diff changeset
   119
             new String[] {"-jdkinternals", "-quiet"});
22007
907f7054db16 8029216: (jdeps) Provide a specific option to report JDK internal APIs
mchung
parents: 21046
diff changeset
   120
        // -jdkinternals parses all classes on -classpath and the input arguments
907f7054db16 8029216: (jdeps) Provide a specific option to report JDK internal APIs
mchung
parents: 21046
diff changeset
   121
        test(new File(mDir, "Gee.class"),
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33021
diff changeset
   122
             new String[] {"com.sun.tools.classfile.ClassFile",
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33021
diff changeset
   123
                           "sun.security.x509.X509CertInfo"},
22007
907f7054db16 8029216: (jdeps) Provide a specific option to report JDK internal APIs
mchung
parents: 21046
diff changeset
   124
             new String[] {"JDK internal API"},
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33021
diff changeset
   125
             // use -classpath tmp/a with no use of JDK internal API
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33021
diff changeset
   126
             new String[] {"-classpath", dest.resolve("a").toString(), "-jdkinternals", "-quiet"});
22007
907f7054db16 8029216: (jdeps) Provide a specific option to report JDK internal APIs
mchung
parents: 21046
diff changeset
   127
907f7054db16 8029216: (jdeps) Provide a specific option to report JDK internal APIs
mchung
parents: 21046
diff changeset
   128
        // parse only APIs
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22448
diff changeset
   129
        test(mDir,
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22448
diff changeset
   130
             new String[] {"java.lang.Object", "java.lang.String",
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22448
diff changeset
   131
                           "java.util.Set",
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22448
diff changeset
   132
                           "c.C", "d.D", "c.I", "e.E"},
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22448
diff changeset
   133
             new String[] {"compact1", testDirBasename},
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22448
diff changeset
   134
             new String[] {"-classpath", testDir.getPath(), "-verbose:class", "-P", "-apionly"});
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22448
diff changeset
   135
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   136
        test(mDir,
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   137
             new String[] {"java.lang.Object", "java.lang.String",
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   138
                           "java.util.Set",
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   139
                           "c.C", "d.D", "c.I", "e.E", "m.Bar"},
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   140
             new String[] {"compact1", testDirBasename, mDir.getName()},
41251
6112540cd0c1 8166238: Update jdeps for GNU-style long form options
mchung
parents: 40308
diff changeset
   141
             new String[] {"-classpath", testDir.getPath(), "-verbose", "-P", "--api-only"});
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   142
        return errors;
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   143
    }
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   144
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   145
    void test(File file, String[] expect, String[] profiles) {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   146
        test(file, expect, profiles, new String[0]);
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   147
    }
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   148
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   149
    void test(File file, String[] expect, String[] profiles, String[] options) {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   150
        List<String> args = new ArrayList<>(Arrays.asList(options));
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   151
        if (file != null) {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   152
            args.add(file.getPath());
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   153
        }
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   154
        checkResult("api-dependencies", expect, profiles,
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   155
                    jdeps(args.toArray(new String[0])));
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   156
    }
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   157
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   158
    Map<String,String> jdeps(String... args) {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   159
        StringWriter sw = new StringWriter();
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   160
        PrintWriter pw = new PrintWriter(sw);
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 37848
diff changeset
   161
        System.err.println("jdeps " + Arrays.stream(args)
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 37848
diff changeset
   162
            .collect(Collectors.joining(" ")));
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   163
        int rc = com.sun.tools.jdeps.Main.run(args, pw);
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   164
        pw.close();
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   165
        String out = sw.toString();
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   166
        if (!out.isEmpty())
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   167
            System.err.println(out);
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   168
        if (rc != 0)
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   169
            throw new Error("jdeps failed: rc=" + rc);
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   170
        return findDeps(out);
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   171
    }
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   172
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   173
    // Pattern used to parse lines
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   174
    private static Pattern linePattern = Pattern.compile(".*\r?\n");
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   175
    private static Pattern pattern = Pattern.compile("\\s+ -> (\\S+) +(.*)");
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   176
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   177
    // Use the linePattern to break the given String into lines, applying
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   178
    // the pattern to each line to see if we have a match
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   179
    private static Map<String,String> findDeps(String out) {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   180
        Map<String,String> result = new HashMap<>();
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   181
        Matcher lm = linePattern.matcher(out);  // Line matcher
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   182
        Matcher pm = null;                      // Pattern matcher
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   183
        int lines = 0;
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   184
        while (lm.find()) {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   185
            lines++;
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   186
            CharSequence cs = lm.group();       // The current line
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   187
            if (pm == null)
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   188
                pm = pattern.matcher(cs);
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   189
            else
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   190
                pm.reset(cs);
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   191
            if (pm.find())
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   192
                result.put(pm.group(1), pm.group(2).trim());
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   193
            if (lm.end() == out.length())
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   194
                break;
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   195
        }
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   196
        return result;
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   197
    }
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   198
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   199
    void checkResult(String label, String[] expect, Collection<String> found) {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   200
        List<String> list = Arrays.asList(expect);
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   201
        if (!isEqual(list, found))
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   202
            error("Unexpected " + label + " found: '" + found + "', expected: '" + list + "'");
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   203
    }
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   204
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   205
    void checkResult(String label, String[] expect, String[] profiles, Map<String,String> result) {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   206
        // check the dependencies
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   207
        checkResult(label, expect, result.keySet());
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   208
        // check profile information
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   209
        Set<String> values = new TreeSet<>();
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   210
        String internal = "JDK internal API";
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   211
        for (String s: result.values()) {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   212
            if (s.startsWith(internal)){
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   213
                values.add(internal);
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   214
            } else {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   215
                values.add(s);
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   216
            }
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   217
        }
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   218
        checkResult(label, profiles, values);
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   219
    }
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   220
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   221
    boolean isEqual(List<String> expected, Collection<String> found) {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   222
        if (expected.size() != found.size())
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   223
            return false;
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   224
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   225
        List<String> list = new ArrayList<>(found);
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   226
        list.removeAll(expected);
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   227
        return list.isEmpty();
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   228
    }
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   229
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   230
    void error(String msg) {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   231
        System.err.println("Error: " + msg);
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   232
        errors++;
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   233
    }
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   234
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   235
    int errors;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33021
diff changeset
   236
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents:
diff changeset
   237
}