--- /dev/null 2016-10-07 11:39:32.000000000 -0700 +++ new/test/lib/testlibrary/jdk/testlibrary/tasks/JavaTask.java 2016-10-07 11:39:32.000000000 -0700 @@ -0,0 +1,254 @@ +/* + * Copyright (c) 2013, 2016, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package jdk.testlibrary.tasks; + +import java.util.ArrayList; +import java.util.Arrays; +import static java.util.Arrays.asList; +import static java.util.Collections.EMPTY_LIST; +import java.util.List; +import java.util.Map; +import java.util.function.Function; +import java.util.stream.Collectors; +import static java.util.stream.Collectors.toList; +import java.util.stream.Stream; + +/** + * A task to configure and run the Java launcher. + */ +public class JavaTask extends AbstractTask { + private String classpath; + private String modulepath; + private String className; + private String moduleName; + private List vmOptions; + private List classArgs; + private List addExports; + private List addModules; + private List standardOptions; + + /** + * Create a task to run the Java launcher, using {@code EXEC} mode. + */ + public JavaTask() { + super(Task.Mode.EXEC); + standardOptions = Stream.of("test.vm.opts", "test.java.opts") + .map(p -> System.getProperty(p)) + .filter(p -> p != null && !p.isEmpty()) + .map(o -> asList(o.split(" +"))) + .flatMap(l -> l.stream()).collect(toList()); + } + + /** + * Sets the classpath. + * @param classpath the classpath + * @return this task object + */ + public JavaTask classpath(String classpath) { + this.classpath = classpath; + return this; + } + + /** + * Sets the modulepath. + * @param modulepath the modulepath + * @return this task object + */ + public JavaTask modulepath(String modulepath) { + this.modulepath = modulepath; + return this; + } + + /** + * Sets the {@code --add-exports} option. + * @param addExports value for the option + * @return this task object + */ + public JavaTask addExports(String... addExports) { + this.addExports = Arrays.asList(addExports); + return this; + } + + /** + * Sets the {@code --add-modules} option. + * @param addModules value for the option + * @return this task object + */ + public JavaTask addModules(String... addModules) { + this.addModules = Arrays.asList(addModules); + return this; + } + + /** + * Sets the VM options. + * @param vmOptions the options + * @return this task object + */ + public JavaTask vmOptions(String... vmOptions) { + this.vmOptions = Arrays.asList(vmOptions); + return this; + } + + /** + * Sets the name of the class to be executed. + * @param className the name of the class + * @return this task object + */ + public JavaTask className(String className) { + this.className = className; + return this; + } + + /** + * Sets the name of the module which contains class to be executed. If empty, + * the main class is assumed to be in the unnamed module. + * @param moduleName the name of the module + * @return this task object + */ + public JavaTask moduleName(String moduleName) { + this.moduleName = moduleName; + return this; + } + + /** + * Sets the arguments for the class to be executed. + * @param classArgs the arguments + * @return this task object + */ + public JavaTask classArgs(String... classArgs) { + this.classArgs = Arrays.asList(classArgs); + return this; + } + + /** + * Sets that the standard VM and java options would not be passed + * to the new VM instance. If this method is not called, the default behavior + * is that the options will be passed to the new VM instance. + * + * @return this task object + */ + public JavaTask ignoreStandardOptions() { + standardOptions = EMPTY_LIST; + return this; + } + + /** + * Filters the standard VM and java options. + * @param filter a filter + * @return + */ + public JavaTask filterStandardOption(Function, List> filter) { + standardOptions = filter.apply(standardOptions); + return this; + } + + /** + * Filters the standard VM and java options by taking away a particular option + * and a specified number or parameters after it. + * + * @param option the option, such as {@code --limit-modules} + * @param valueCount number of parameters to remove after the option. + * @return this task object + */ + public JavaTask ignoreStandardOption(String option, int valueCount) { + return filterStandardOption(os -> { + List filtered = new ArrayList<>(); + for(int i = 0; i < os.size(); i++) { + if(os.get(i).equals(option)) + i += valueCount; + else + filtered.add(os.get(i)); + } + return filtered; + }); + } + + /** + * Filters the standard VM and java options so that there are no options which + * affects a set of resolvable modules or a set of accessible APIs. + * + * @return this task object + */ + public JavaTask ignoreStandardModuleOptions() { + moduleOptions.entrySet().stream() + .forEach(e -> ignoreStandardOption(e.getKey(), e.getValue())); + return this; + } + + /** + * {@inheritDoc} + * @return the name "java" + */ + @Override + public String name() { + return "java"; + } + + /** + * Calls the Java launcher with the arguments as currently configured. + * @return a Result object indicating the outcome of the task + * and the content of any output written to stdout or stderr. + * @throws TaskError if the outcome of the task is not as expected. + */ + @Override + public Task.Result run() { + List args = new ArrayList<>(standardOptions); + if (classpath != null) { + args.add("--class-path"); + args.add(classpath); + } + if (modulepath != null) { + args.add("--module-path"); + args.add(modulepath); + } + if (addExports != null) { + args.add("--add-exports"); + args.add(addExports.stream().collect(Collectors.joining(","))); + } + if (addModules != null) { + args.add("--add-modules"); + args.add(addModules.stream().collect(Collectors.joining(","))); + } + if (vmOptions != null) + args.addAll(vmOptions); + if (className != null) + if (moduleName != null) { + args.add("-m"); + args.add(moduleName + "/" + className); + } else + args.add(className); + if (classArgs != null) + args.addAll(classArgs); + return run(Tool.JAVA, args); + } + + //this is a list of options to ignore in ignoreStandardModuleOptions() + private static final Map moduleOptions = + Map.of("--limit-modules", 1, + "--upgrade-module-path", 1, + "--add-modules", 1, + "--add-reads", 1, + "--add-exports", 1, + "--patch-module", 1); +}