langtools/test/tools/jdeps/Basic.java
author duke
Wed, 05 Jul 2017 18:59:39 +0200
changeset 18044 f1a01fb5f958
parent 16550 f20e2521f3df
child 21046 ebf16a1a6328
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
     1
/*
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
     2
 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
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.
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
     8
 *
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
     9
 * 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
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    11
 * 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
    12
 * 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
    13
 * accompanied this code).
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    14
 *
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    15
 * 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
    16
 * 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
    17
 * 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
    18
 *
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    19
 * 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
    20
 * 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
    21
 * questions.
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    22
 */
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    23
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
 * @test
16550
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
    26
 * @bug 8003562 8005428
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    27
 * @summary Basic tests for jdeps tool
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    28
 * @build Test p.Foo
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    29
 * @run main Basic
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    30
 */
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    31
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    32
import java.io.File;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    33
import java.io.IOException;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    34
import java.io.PrintWriter;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    35
import java.io.StringWriter;
16550
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
    36
import java.nio.file.Path;
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
    37
import java.nio.file.Paths;
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    38
import java.util.*;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    39
import java.util.regex.*;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    40
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    41
public class Basic {
16550
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
    42
    private static boolean symbolFileExist = initProfiles();
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
    43
    private static boolean initProfiles() {
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
    44
        // check if ct.sym exists; if not use the profiles.properties file
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
    45
        Path home = Paths.get(System.getProperty("java.home"));
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
    46
        if (home.endsWith("jre")) {
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
    47
            home = home.getParent();
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
    48
        }
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
    49
        Path ctsym = home.resolve("lib").resolve("ct.sym");
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
    50
        boolean symbolExists = ctsym.toFile().exists();
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
    51
        if (!symbolExists) {
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
    52
            Path testSrcProfiles =
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
    53
                Paths.get(System.getProperty("test.src", "."), "profiles.properties");
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
    54
            if (!testSrcProfiles.toFile().exists())
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
    55
                throw new Error(testSrcProfiles + " does not exist");
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
    56
            System.out.format("%s doesn't exist.%nUse %s to initialize profiles info%n",
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
    57
                ctsym, testSrcProfiles);
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
    58
            System.setProperty("jdeps.profiles", testSrcProfiles.toString());
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
    59
        }
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
    60
        return symbolExists;
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
    61
    }
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
    62
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    63
    public static void main(String... args) throws Exception {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    64
        int errors = 0;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    65
        errors += new Basic().run();
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    66
        if (errors > 0)
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    67
            throw new Exception(errors + " errors found");
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
    int run() throws IOException {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    71
        File testDir = new File(System.getProperty("test.classes", "."));
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    72
        // test a .class file
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    73
        test(new File(testDir, "Test.class"),
16550
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
    74
             new String[] {"java.lang", "p"},
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
    75
             new String[] {"compact1", "not found"});
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    76
        // test a directory
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    77
        test(new File(testDir, "p"),
16550
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
    78
             new String[] {"java.lang", "java.util", "java.lang.management"},
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
    79
             new String[] {"compact1", "compact1", "compact3"});
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    80
        // test class-level dependency output
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    81
        test(new File(testDir, "Test.class"),
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    82
             new String[] {"java.lang.Object", "p.Foo"},
16550
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
    83
             new String[] {"compact1", "not found"},
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    84
             new String[] {"-V", "class"});
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    85
        // test -p option
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    86
        test(new File(testDir, "Test.class"),
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    87
             new String[] {"p.Foo"},
16550
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
    88
             new String[] {"not found"},
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    89
             new String[] {"--verbose-level=class", "-p", "p"});
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    90
        // test -e option
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    91
        test(new File(testDir, "Test.class"),
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    92
             new String[] {"p.Foo"},
16550
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
    93
             new String[] {"not found"},
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    94
             new String[] {"-V", "class", "-e", "p\\..*"});
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    95
        test(new File(testDir, "Test.class"),
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    96
             new String[] {"java.lang"},
16550
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
    97
             new String[] {"compact1"},
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    98
             new String[] {"-V", "package", "-e", "java\\.lang\\..*"});
16290
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents: 15030
diff changeset
    99
        // test -classpath and wildcard options
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   100
        test(null,
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   101
             new String[] {"com.sun.tools.jdeps", "java.lang", "java.util",
16550
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   102
                           "java.util.regex", "java.io", "java.nio.file",
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   103
                           "java.lang.management"},
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   104
             new String[] {(symbolFileExist? "not found" : "JDK internal API (classes)"),
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   105
                           "compact1", "compact1", "compact1",
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   106
                           "compact1", "compact1", "compact3"},
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   107
             new String[] {"--classpath", testDir.getPath(), "*"});
16550
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   108
        /* Temporary disable this test case.  Test.class has a dependency
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   109
         * on java.lang.String on certain windows machine (8008479).
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   110
         // -v shows intra-dependency
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   111
         test(new File(testDir, "Test.class"),
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   112
              new String[] {"java.lang.Object", "p.Foo"},
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   113
              new String[] {"compact1", testDir.getName()},
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   114
              new String[] {"-v", "--classpath", testDir.getPath(), "Test.class"});
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   115
        */
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   116
        return errors;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   117
    }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   118
16550
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   119
    void test(File file, String[] expect, String[] profiles) {
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   120
        test(file, expect, profiles, new String[0]);
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   121
    }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   122
16550
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   123
    void test(File file, String[] expect, String[] profiles, String[] options) {
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   124
        List<String> args = new ArrayList<>(Arrays.asList(options));
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   125
        if (file != null) {
16550
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   126
            args.add(file.getPath());
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   127
        }
16550
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   128
        List<String> argsWithDashP = new ArrayList<>();
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   129
        argsWithDashP.add("-P");
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   130
        argsWithDashP.addAll(args);
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   131
        // test without -P
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   132
        checkResult("dependencies", expect, jdeps(args.toArray(new String[0])).keySet());
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   133
        // test with -P
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   134
        checkResult("profiles", expect, profiles, jdeps(argsWithDashP.toArray(new String[0])));
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   135
    }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   136
16550
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   137
    Map<String,String> jdeps(String... args) {
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   138
        StringWriter sw = new StringWriter();
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   139
        PrintWriter pw = new PrintWriter(sw);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   140
        System.err.println("jdeps " + Arrays.toString(args));
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   141
        int rc = com.sun.tools.jdeps.Main.run(args, pw);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   142
        pw.close();
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   143
        String out = sw.toString();
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   144
        if (!out.isEmpty())
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   145
            System.err.println(out);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   146
        if (rc != 0)
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   147
            throw new Error("jdeps failed: rc=" + rc);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   148
        return findDeps(out);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   149
    }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   150
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   151
    // Pattern used to parse lines
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   152
    private static Pattern linePattern = Pattern.compile(".*\r?\n");
16550
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   153
    private static Pattern pattern = Pattern.compile("\\s+ -> (\\S+) +(.*)");
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   154
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   155
    // Use the linePattern to break the given String into lines, applying
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   156
    // the pattern to each line to see if we have a match
16550
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   157
    private static Map<String,String> findDeps(String out) {
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   158
        Map<String,String> result = new HashMap<>();
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   159
        Matcher lm = linePattern.matcher(out);  // Line matcher
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   160
        Matcher pm = null;                      // Pattern matcher
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   161
        int lines = 0;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   162
        while (lm.find()) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   163
            lines++;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   164
            CharSequence cs = lm.group();       // The current line
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   165
            if (pm == null)
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   166
                pm = pattern.matcher(cs);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   167
            else
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   168
                pm.reset(cs);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   169
            if (pm.find())
16550
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   170
                result.put(pm.group(1), pm.group(2).trim());
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   171
            if (lm.end() == out.length())
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   172
                break;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   173
        }
16550
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   174
        return result;
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   175
    }
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   176
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   177
    void checkResult(String label, String[] expect, Collection<String> found) {
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   178
        List<String> list = Arrays.asList(expect);
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   179
        if (!isEqual(list, found))
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   180
            error("Unexpected " + label + " found: '" + found + "', expected: '" + list + "'");
15030
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
16550
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   183
    void checkResult(String label, String[] expect, String[] profiles, Map<String,String> result) {
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   184
        if (expect.length != profiles.length)
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   185
            error("Invalid expected names and profiles");
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   186
16550
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   187
        // check the dependencies
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   188
        checkResult(label, expect, result.keySet());
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   189
        // check profile information
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   190
        checkResult(label, profiles, result.values());
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   191
        for (int i=0; i < expect.length; i++) {
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   192
            String profile = result.get(expect[i]);
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   193
            if (!profile.equals(profiles[i]))
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   194
                error("Unexpected profile: '" + profile + "', expected: '" + profiles[i] + "'");
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   195
        }
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   196
    }
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   197
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   198
    boolean isEqual(List<String> expected, Collection<String> found) {
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   199
        if (expected.size() != found.size())
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   200
            return false;
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   201
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   202
        List<String> list = new ArrayList<>(found);
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   203
        list.removeAll(expected);
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   204
        return list.isEmpty();
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   205
    }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   206
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   207
    void error(String msg) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   208
        System.err.println("Error: " + msg);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   209
        errors++;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   210
    }
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
    int errors;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   213
}