langtools/test/tools/jdeps/lib/JdepsRunner.java
author mchung
Wed, 05 Oct 2016 18:41:43 -0700
changeset 41442 14db641d4a9f
child 41860 906670ff49c7
permissions -rw-r--r--
8166846: jdeps fails to generate module info if there is any class in unnamed package Reviewed-by: lancea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
41442
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
     1
/*
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
     4
 *
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
     7
 * published by the Free Software Foundation.
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
     8
 *
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    13
 * accompanied this code).
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    14
 *
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    18
 *
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    21
 * questions.
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    22
 */
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    23
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    24
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    25
import java.io.PrintStream;
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    26
import java.io.PrintWriter;
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    27
import java.io.StringWriter;
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    28
import java.util.Arrays;
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    29
import java.util.List;
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    30
import java.util.stream.Collectors;
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    31
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    32
/**
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    33
 * JdepsRunner class to invoke jdeps with the given command line argument
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    34
 */
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    35
public class JdepsRunner {
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    36
    public static JdepsRunner run(String... args) {
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    37
        JdepsRunner jdeps = new JdepsRunner(args);
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    38
        int rc = jdeps.run();
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    39
        jdeps.printStdout(System.err);
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    40
        if (rc != 0)
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    41
           throw new Error("jdeps failed: rc=" + rc);
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    42
        return jdeps;
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    43
    }
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    44
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    45
    final StringWriter stdout = new StringWriter();
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    46
    final StringWriter stderr = new StringWriter();
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    47
    final String[] args;
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    48
    public JdepsRunner(String... args) {
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    49
        System.err.println("jdeps " + Arrays.stream(args)
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    50
                                            .collect(Collectors.joining(" ")));
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    51
        this.args = args;
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    52
    }
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    53
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    54
    public JdepsRunner(List<String> args) {
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    55
        this(args.toArray(new String[0]));
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    56
    }
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    57
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    58
    public int run() {
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    59
        return run(false);
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    60
    }
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    61
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    62
    public int run(boolean showOutput) {
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    63
        try (PrintWriter pw = new PrintWriter(stdout)) {
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    64
            int rc = com.sun.tools.jdeps.Main.run(args, pw);
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    65
            if (showOutput) {
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    66
                System.err.println(stdout.toString());
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    67
            }
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    68
            return rc;
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    69
        }
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    70
    }
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    71
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    72
    public boolean outputContains(String s) {
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    73
        return stdout.toString().contains(s);
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    74
    }
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    75
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    76
    public void printStdout(PrintStream stream) {
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    77
        stream.println(stdout.toString());
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    78
    }
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    79
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    80
    public void printStderr(PrintStream stream) {
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    81
        stream.println(stderr.toString());
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    82
    }
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    83
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    84
    public String[] output() {
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    85
        String lineSep = System.getProperty("line.separator");
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    86
        return stdout.toString().split(lineSep);
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    87
    }
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    88
}