langtools/test/tools/jdeps/Basic.java
author duke
Wed, 05 Jul 2017 18:40:20 +0200
changeset 15572 d17eb2e13e36
parent 15030 2d8dec41f029
child 16290 b0b4f52de7ea
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
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    26
 * @bug 8003562
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;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    36
import java.util.*;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    37
import java.util.regex.*;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    38
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    39
public class Basic {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    40
    public static void main(String... args) throws Exception {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    41
        int errors = 0;
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
        errors += new Basic().run();
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    44
        if (errors > 0)
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    45
            throw new Exception(errors + " errors found");
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    46
    }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    47
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    48
    int run() throws IOException {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    49
        File testDir = new File(System.getProperty("test.classes", "."));
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    50
        // test a .class file
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    51
        test(new File(testDir, "Test.class"),
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    52
             new String[] {"java.lang", "p"});
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    53
        // test a directory
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    54
        test(new File(testDir, "p"),
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    55
             new String[] {"java.lang", "java.util"});
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    56
        // test class-level dependency output
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    57
        test(new File(testDir, "Test.class"),
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    58
             new String[] {"java.lang.Object", "p.Foo"},
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    59
             new String[] {"-V", "class"});
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    60
        // test -p option
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    61
        test(new File(testDir, "Test.class"),
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    62
             new String[] {"p.Foo"},
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    63
             new String[] {"--verbose-level=class", "-p", "p"});
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    64
        // test -e option
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    65
        test(new File(testDir, "Test.class"),
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    66
             new String[] {"p.Foo"},
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    67
             new String[] {"-V", "class", "-e", "p\\..*"});
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    68
        test(new File(testDir, "Test.class"),
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    69
             new String[] {"java.lang"},
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    70
             new String[] {"-V", "package", "-e", "java\\.lang\\..*"});
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    71
        // test -classpath and -all options
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    72
        test(null,
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    73
             new String[] {"com.sun.tools.jdeps", "java.lang", "java.util",
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    74
                           "java.util.regex", "java.io", "p"},
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    75
             new String[] {"--classpath", testDir.getPath(), "*"});
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    76
        return errors;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    77
    }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    78
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    79
    void test(File file, String[] expect) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    80
        test(file, expect, new String[0]);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    81
    }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    82
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    83
    void test(File file, String[] expect, String[] options) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    84
        String[] args;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    85
        if (file != null) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    86
            args = Arrays.copyOf(options, options.length+1);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    87
            args[options.length] = file.getPath();
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    88
        } else {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    89
            args = options;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    90
        }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    91
        String[] deps = jdeps(args);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    92
        checkEqual("dependencies", expect, deps);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    93
    }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    94
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    95
    String[] jdeps(String... args) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    96
        StringWriter sw = new StringWriter();
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    97
        PrintWriter pw = new PrintWriter(sw);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    98
        System.err.println("jdeps " + Arrays.toString(args));
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    99
        int rc = com.sun.tools.jdeps.Main.run(args, pw);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   100
        pw.close();
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   101
        String out = sw.toString();
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   102
        if (!out.isEmpty())
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   103
            System.err.println(out);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   104
        if (rc != 0)
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   105
            throw new Error("jdeps failed: rc=" + rc);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   106
        return findDeps(out);
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
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   109
    // Pattern used to parse lines
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   110
    private static Pattern linePattern = Pattern.compile(".*\r?\n");
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   111
    private static Pattern pattern = Pattern.compile("\\s+ -> (\\S+) +.*");
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   112
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   113
    // 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
   114
    // the pattern to each line to see if we have a match
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   115
    private static String[] findDeps(String out) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   116
        List<String> result = new ArrayList<>();
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   117
        Matcher lm = linePattern.matcher(out);  // Line matcher
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   118
        Matcher pm = null;                      // Pattern matcher
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   119
        int lines = 0;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   120
        while (lm.find()) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   121
            lines++;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   122
            CharSequence cs = lm.group();       // The current line
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   123
            if (pm == null)
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   124
                pm = pattern.matcher(cs);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   125
            else
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   126
                pm.reset(cs);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   127
            if (pm.find())
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   128
                result.add(pm.group(1));
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   129
            if (lm.end() == out.length())
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   130
                break;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   131
        }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   132
        return result.toArray(new String[0]);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   133
    }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   134
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   135
    void checkEqual(String label, String[] expect, String[] found) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   136
        Set<String> s1 = new HashSet<>(Arrays.asList(expect));
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   137
        Set<String> s2 = new HashSet<>(Arrays.asList(found));
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   138
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   139
        if (!s1.equals(s2))
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   140
            error("Unexpected " + label + " found: '" + s2 + "', expected: '" + s1 + "'");
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   141
    }
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
    void error(String msg) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   144
        System.err.println("Error: " + msg);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   145
        errors++;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   146
    }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   147
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   148
    int errors;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   149
}