langtools/test/tools/jdeps/lib/JdepsRunner.java
author mchung
Fri, 11 Nov 2016 17:32:21 -0800
changeset 41997 79da2a8f4274
parent 41860 906670ff49c7
permissions -rw-r--r--
8168386: Fix jdeps verbose options Reviewed-by: dfuchs, 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;
41860
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents: 41442
diff changeset
    30
import java.util.spi.ToolProvider;
41442
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    31
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
    32
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    33
/**
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    34
 * 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
    35
 */
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    36
public class JdepsRunner {
41860
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents: 41442
diff changeset
    37
    private static final ToolProvider JDEPS_TOOL = ToolProvider.findFirst("jdeps")
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents: 41442
diff changeset
    38
        .orElseThrow(() -> new RuntimeException("jdeps tool not found"));
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents: 41442
diff changeset
    39
41442
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    40
    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
    41
        JdepsRunner jdeps = new JdepsRunner(args);
41860
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents: 41442
diff changeset
    42
        int rc = jdeps.run(true);
41442
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    43
        if (rc != 0)
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    44
           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
    45
        return jdeps;
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    46
    }
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    47
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    48
    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
    49
    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
    50
    final String[] args;
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    51
    public JdepsRunner(String... args) {
41860
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents: 41442
diff changeset
    52
        System.out.println("jdeps " + Arrays.stream(args)
41442
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    53
                                            .collect(Collectors.joining(" ")));
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    54
        this.args = args;
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    55
    }
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
    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
    58
        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
    59
    }
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
    public int run() {
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    62
        return run(false);
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    63
    }
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    64
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    65
    public int run(boolean showOutput) {
41860
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents: 41442
diff changeset
    66
        try (PrintWriter pwout = new PrintWriter(stdout);
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents: 41442
diff changeset
    67
             PrintWriter pwerr = new PrintWriter(stderr)) {
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents: 41442
diff changeset
    68
            int rc = JDEPS_TOOL.run(pwout, pwerr, args);
41442
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    69
            if (showOutput) {
41860
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents: 41442
diff changeset
    70
                printStdout(System.out);
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents: 41442
diff changeset
    71
                printStderr(System.out);
41442
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    72
            }
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    73
            return rc;
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
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    77
    public boolean outputContains(String s) {
41997
79da2a8f4274 8168386: Fix jdeps verbose options
mchung
parents: 41860
diff changeset
    78
        return stdout.toString().contains(s) || stderr.toString().contains(s);
41442
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
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    81
    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
    82
        stream.println(stdout.toString());
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
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    85
    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
    86
        stream.println(stderr.toString());
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
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    89
    public String[] output() {
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    90
        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
    91
        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
    92
    }
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
diff changeset
    93
}