Path: blob/master/test/langtools/tools/lib/toolbox/JavaTask.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.IOException;26import java.util.ArrayList;27import java.util.Arrays;28import java.util.List;2930/**31* A task to configure and run the Java launcher.32*/33public class JavaTask extends AbstractTask<JavaTask> {34boolean includeStandardOptions = true;35private String classpath;36private List<String> vmOptions;37private String className;38private List<String> classArgs;3940/**41* Create a task to run the Java launcher, using {@code EXEC} mode.42* @param toolBox the {@code ToolBox} to use43*/44public JavaTask(ToolBox toolBox) {45super(toolBox, Task.Mode.EXEC);46}4748/**49* Sets the classpath.50* @param classpath the classpath51* @return this task object52*/53public JavaTask classpath(String classpath) {54this.classpath = classpath;55return this;56}5758/**59* Sets the VM options.60* @param vmOptions the options61* @return this task object62*/63public JavaTask vmOptions(String... vmOptions) {64this.vmOptions = Arrays.asList(vmOptions);65return this;66}6768/**69* Sets the VM options.70* @param vmOptions the options71* @return this task object72*/73public JavaTask vmOptions(List<String> vmOptions) {74this.vmOptions = vmOptions;75return this;76}7778/**79* Sets the name of the class to be executed.80* @param className the name of the class81* @return this task object82*/83public JavaTask className(String className) {84this.className = className;85return this;86}8788/**89* Sets the arguments for the class to be executed.90* @param classArgs the arguments91* @return this task object92*/93public JavaTask classArgs(String... classArgs) {94this.classArgs = Arrays.asList(classArgs);95return this;96}9798/**99* Sets the arguments for the class to be executed.100* @param classArgs the arguments101* @return this task object102*/103public JavaTask classArgs(List<String> classArgs) {104this.classArgs = classArgs;105return this;106}107108/**109* Sets whether or not the standard VM and java options for the test should be passed110* to the new VM instance. If this method is not called, the default behavior is that111* the options will be passed to the new VM instance.112*113* @param includeStandardOptions whether or not the standard VM and java options for114* the test should be passed to the new VM instance.115* @return this task object116*/117public JavaTask includeStandardOptions(boolean includeStandardOptions) {118this.includeStandardOptions = includeStandardOptions;119return this;120}121122/**123* {@inheritDoc}124* @return the name "java"125*/126@Override127public String name() {128return "java";129}130131/**132* Calls the Java launcher with the arguments as currently configured.133* @return a Result object indicating the outcome of the task134* and the content of any output written to stdout or stderr.135* @throws TaskError if the outcome of the task is not as expected.136*/137@Override138public Task.Result run() {139List<String> args = new ArrayList<>();140args.add(toolBox.getJDKTool("java").toString());141if (includeStandardOptions) {142args.addAll(toolBox.split(System.getProperty("test.vm.opts"), " +"));143args.addAll(toolBox.split(System.getProperty("test.java.opts"), " +"));144}145if (classpath != null) {146args.add("-classpath");147args.add(classpath);148}149if (vmOptions != null)150args.addAll(vmOptions);151if (className != null)152args.add(className);153if (classArgs != null)154args.addAll(classArgs);155ProcessBuilder pb = getProcessBuilder();156pb.command(args);157try {158return runProcess(toolBox, this, pb.start());159} catch (IOException | InterruptedException e) {160throw new Error(e);161}162}163}164165166