Path: blob/master/test/langtools/tools/lib/toolbox/Task.java
41149 views
/*1* Copyright (c) 2013, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223package toolbox;2425import java.io.PrintStream;26import java.util.ArrayList;27import java.util.Arrays;28import java.util.List;29import java.util.Map;30import static toolbox.ToolBox.lineSeparator;3132/**33* The supertype for tasks.34* Complex operations are modeled by building and running a "Task" object.35* Tasks are typically configured in a fluent series of calls.36*/37public interface Task {38/**39* Returns the name of the task.40* @return the name of the task41*/42String name();4344/**45* Executes the task as currently configured.46* @return a Result object containing the results of running the task47* @throws TaskError if the outcome of the task was not as expected48*/49Result run() throws TaskError;5051/**52* Exception thrown by {@code Task.run} when the outcome is not as53* expected.54*/55public static class TaskError extends Error {56/**57* Creates a TaskError object with the given message.58* @param message the message59*/60public TaskError(String message) {61super(message);62}63}6465/**66* An enum to indicate the mode a task should use it is when executed.67*/68public enum Mode {69/**70* The task should use the interface used by the command71* line launcher for the task.72* For example, for javac: com.sun.tools.javac.Main.compile73*/74CMDLINE,75/**76* The task should use a publicly defined API for the task.77* For example, for javac: javax.tools.JavaCompiler78*/79API,80/**81* The task should use the standard launcher for the task.82* For example, $JAVA_HOME/bin/javac83*/84EXEC85}8687/**88* An enum to indicate the expected success or failure of executing a task.89*/90public enum Expect {91/** It is expected that the task will complete successfully. */92SUCCESS,93/** It is expected that the task will not complete successfully. */94FAIL95}9697/**98* An enum to identify the streams that may be written by a {@code Task}.99*/100public enum OutputKind {101/** Identifies output written to {@code System.out} or {@code stdout}. */102STDOUT,103/** Identifies output written to {@code System.err} or {@code stderr}. */104STDERR,105/** Identifies output written to a stream provided directly to the task. */106DIRECT107};108109/**110* The results from running a {@link Task}.111* The results contain the exit code returned when the tool was invoked,112* and a map containing the output written to any streams during the113* execution of the tool.114* All tools support "stdout" and "stderr".115* Tools that take an explicit PrintWriter save output written to that116* stream as "main".117*/118public static class Result {119final ToolBox toolBox;120final Task task;121final int exitCode;122final Map<OutputKind, String> outputMap;123124Result(ToolBox toolBox, Task task, int exitCode, Map<OutputKind, String> outputMap) {125this.toolBox = toolBox;126this.task = task;127this.exitCode = exitCode;128this.outputMap = outputMap;129}130131/**132* Returns the content of a specified stream.133* @param outputKind the kind of the selected stream134* @return the content that was written to that stream when the tool135* was executed.136*/137public String getOutput(OutputKind outputKind) {138return outputMap.get(outputKind);139}140141/**142* Returns the content of named streams as a list of lines.143* @param outputKinds the kinds of the selected streams144* @return the content that was written to the given streams when the tool145* was executed.146*/147public List<String> getOutputLines(OutputKind... outputKinds) {148List<String> result = new ArrayList<>();149for (OutputKind outputKind : outputKinds) {150result.addAll(Arrays.asList(outputMap.get(outputKind).split(lineSeparator)));151}152return result;153}154155/**156* Writes the content of the specified stream to the log.157* @param kind the kind of the selected stream158* @return this Result object159*/160public Result write(OutputKind kind) {161PrintStream out = toolBox.out;162String text = getOutput(kind);163if (text == null || text.isEmpty())164out.println("[" + task.name() + ":" + kind + "]: empty");165else {166out.println("[" + task.name() + ":" + kind + "]:");167out.print(text);168}169return this;170}171172/**173* Writes the content of all streams with any content to the log.174* @return this Result object175*/176public Result writeAll() {177PrintStream out = toolBox.out;178outputMap.forEach((name, text) -> {179if (!text.isEmpty()) {180out.println("[" + name + "]:");181out.print(text);182}183});184return this;185}186}187}188189190191