Path: blob/master/test/langtools/tools/lib/toolbox/JavacTask.java
41149 views
/*1* Copyright (c) 2013, 2021, 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.File;26import java.io.IOException;27import java.io.PrintWriter;28import java.nio.file.Path;29import java.nio.file.Paths;30import java.util.ArrayList;31import java.util.Arrays;32import java.util.Collections;33import java.util.HashMap;34import java.util.List;35import java.util.Map;36import java.util.function.Consumer;37import java.util.stream.Collectors;38import java.util.stream.Stream;39import javax.annotation.processing.Processor;40import javax.tools.JavaCompiler;41import javax.tools.JavaFileManager;42import javax.tools.JavaFileObject;43import javax.tools.StandardJavaFileManager;44import javax.tools.StandardLocation;4546import com.sun.tools.javac.api.JavacTaskImpl;47import com.sun.tools.javac.api.JavacTool;4849/**50* A task to configure and run the Java compiler, javac.51*/52public class JavacTask extends AbstractTask<JavacTask> {53private boolean includeStandardOptions;54private List<Path> classpath;55private List<Path> sourcepath;56private Path outdir;57private List<String> options;58private List<String> classes;59private List<String> files;60private List<JavaFileObject> fileObjects;61private JavaFileManager fileManager;62private Consumer<com.sun.source.util.JavacTask> callback;63private List<Processor> procs;6465private JavaCompiler compiler;66private StandardJavaFileManager internalFileManager;6768/**69* Creates a task to execute {@code javac} using API mode.70* @param toolBox the {@code ToolBox} to use71*/72public JavacTask(ToolBox toolBox) {73super(toolBox, Task.Mode.API);74}7576/**77* Creates a task to execute {@code javac} in a specified mode.78* @param toolBox the {@code ToolBox} to use79* @param mode the mode to be used80*/81public JavacTask(ToolBox toolBox, Task.Mode mode) {82super(toolBox, mode);83}8485/**86* Sets the classpath.87* @param classpath the classpath88* @return this task object89*/90public JavacTask classpath(String classpath) {91this.classpath = Stream.of(classpath.split(File.pathSeparator))92.filter(s -> !s.isEmpty())93.map(s -> Paths.get(s))94.collect(Collectors.toList());95return this;96}9798/**99* Sets the classpath.100* @param classpath the classpath101* @return this task object102*/103public JavacTask classpath(Path... classpath) {104this.classpath = Arrays.asList(classpath);105return this;106}107108/**109* Sets the classpath.110* @param classpath the classpath111* @return this task object112*/113public JavacTask classpath(List<Path> classpath) {114this.classpath = classpath;115return this;116}117118/**119* Sets the sourcepath.120* @param sourcepath the sourcepath121* @return this task object122*/123public JavacTask sourcepath(String sourcepath) {124this.sourcepath = Stream.of(sourcepath.split(File.pathSeparator))125.filter(s -> !s.isEmpty())126.map(s -> Paths.get(s))127.collect(Collectors.toList());128return this;129}130131/**132* Sets the sourcepath.133* @param sourcepath the sourcepath134* @return this task object135*/136public JavacTask sourcepath(Path... sourcepath) {137this.sourcepath = Arrays.asList(sourcepath);138return this;139}140141/**142* Sets the sourcepath.143* @param sourcepath the sourcepath144* @return this task object145*/146public JavacTask sourcepath(List<Path> sourcepath) {147this.sourcepath = sourcepath;148return this;149}150151/**152* Sets the output directory.153* @param outdir the output directory154* @return this task object155*/156public JavacTask outdir(String outdir) {157this.outdir = Paths.get(outdir);158return this;159}160161/**162* Sets the output directory.163* @param outdir the output directory164* @return this task object165*/166public JavacTask outdir(Path outdir) {167this.outdir = outdir;168return this;169}170171/**172* Sets the options.173* @param options the options174* @return this task object175*/176public JavacTask options(String... options) {177this.options = Arrays.asList(options);178return this;179}180181/**182* Sets the options.183* @param spaceSeparatedOption the space separated options184* @return this task object185*/186public JavacTask spaceSeparatedOptions(String spaceSeparatedOption) {187this.options = Arrays.asList(spaceSeparatedOption.split("\\s+"));188return this;189}190191/**192* Sets the options.193* @param options the options194* @return this task object195*/196public JavacTask options(List<String> options) {197this.options = options;198return this;199}200201/**202* Sets the classes to be analyzed.203* @param classes the classes204* @return this task object205*/206public JavacTask classes(String... classes) {207this.classes = Arrays.asList(classes);208return this;209}210211/**212* Sets the files to be compiled or analyzed.213* @param files the files214* @return this task object215*/216public JavacTask files(String... files) {217this.files = Arrays.asList(files);218return this;219}220221/**222* Sets the files to be compiled or analyzed.223* @param files the files224* @return this task object225*/226public JavacTask files(Path... files) {227this.files = Stream.of(files)228.map(Path::toString)229.collect(Collectors.toList());230return this;231}232233/**234* Sets the files to be compiled or analyzed.235* @param files the files236* @return this task object237*/238public JavacTask files(List<Path> files) {239this.files = files.stream()240.map(Path::toString)241.collect(Collectors.toList());242return this;243}244245/**246* Sets the sources to be compiled or analyzed.247* Each source string is converted into an in-memory object that248* can be passed directly to the compiler.249* @param sources the sources250* @return this task object251*/252public JavacTask sources(String... sources) {253fileObjects = Stream.of(sources)254.map(s -> new ToolBox.JavaSource(s))255.collect(Collectors.toList());256return this;257}258259/**260* Sets the the annotation processors to be used.261*/262public JavacTask processors(Processor... procs) {263this.procs = List.of(procs);264return this;265}266267/**268* Sets the file manager to be used by this task.269* @param fileManager the file manager270* @return this task object271*/272public JavacTask fileManager(JavaFileManager fileManager) {273this.fileManager = fileManager;274return this;275}276277/**278* Set a callback to be used by this task.279* @param callback the callback280* @return this task object281*/282public JavacTask callback(Consumer<com.sun.source.util.JavacTask> callback) {283this.callback = callback;284return this;285}286287/**288* {@inheritDoc}289* @return the name "javac"290*/291@Override292public String name() {293return "javac";294}295296/**297* Calls the compiler with the arguments as currently configured.298* @return a Result object indicating the outcome of the compilation299* and the content of any output written to stdout, stderr, or the300* main stream by the compiler.301* @throws TaskError if the outcome of the task is not as expected.302*/303@Override304public Task.Result run() {305if (mode == Task.Mode.EXEC)306return runExec();307308AbstractTask.WriterOutput direct = new AbstractTask.WriterOutput();309// The following are to catch output to System.out and System.err,310// in case these are used instead of the primary (main) stream311AbstractTask.StreamOutput sysOut = new AbstractTask.StreamOutput(System.out, System::setOut);312AbstractTask.StreamOutput sysErr = new AbstractTask.StreamOutput(System.err, System::setErr);313int rc;314Map<Task.OutputKind, String> outputMap = new HashMap<>();315try {316switch (mode == null ? Task.Mode.API : mode) {317case API:318rc = runAPI(direct.pw);319break;320case CMDLINE:321if (fileManager != null) {322throw new IllegalStateException("file manager set in CMDLINE mode");323}324if (callback != null) {325throw new IllegalStateException("callback set in CMDLINE mode");326}327rc = runCommand(direct.pw);328break;329default:330throw new IllegalStateException("unknown mode " + mode);331}332} catch (IOException e) {333toolBox.out.println("Exception occurred: " + e);334rc = 99;335} finally {336outputMap.put(Task.OutputKind.STDOUT, sysOut.close());337outputMap.put(Task.OutputKind.STDERR, sysErr.close());338outputMap.put(Task.OutputKind.DIRECT, direct.close());339}340return checkExit(new Task.Result(toolBox, this, rc, outputMap));341}342343private int runAPI(PrintWriter pw) throws IOException {344try {345// if (compiler == null) {346// TODO: allow this to be set externally347// compiler = ToolProvider.getSystemJavaCompiler();348compiler = JavacTool.create();349// }350351if (fileManager == null)352fileManager = internalFileManager = compiler.getStandardFileManager(null, null, null);353if (outdir != null)354setLocationFromPaths(StandardLocation.CLASS_OUTPUT, Collections.singletonList(outdir));355if (classpath != null)356setLocationFromPaths(StandardLocation.CLASS_PATH, classpath);357if (sourcepath != null)358setLocationFromPaths(StandardLocation.SOURCE_PATH, sourcepath);359List<String> allOpts = new ArrayList<>();360if (options != null)361allOpts.addAll(options);362363Iterable<? extends JavaFileObject> allFiles = joinFiles(files, fileObjects);364JavaCompiler.CompilationTask task = compiler.getTask(pw,365fileManager,366null, // diagnostic listener; should optionally collect diags367allOpts,368classes,369allFiles);370if (procs != null) {371task.setProcessors(procs);372}373JavacTaskImpl taskImpl = (JavacTaskImpl) task;374if (callback != null) {375callback.accept(taskImpl);376}377return taskImpl.doCall().exitCode;378} finally {379if (internalFileManager != null)380internalFileManager.close();381}382}383384private void setLocationFromPaths(StandardLocation location, List<Path> files) throws IOException {385if (!(fileManager instanceof StandardJavaFileManager))386throw new IllegalStateException("not a StandardJavaFileManager");387((StandardJavaFileManager) fileManager).setLocationFromPaths(location, files);388}389390private int runCommand(PrintWriter pw) {391List<String> args = getAllArgs();392String[] argsArray = args.toArray(new String[args.size()]);393return com.sun.tools.javac.Main.compile(argsArray, pw);394}395396private Task.Result runExec() {397List<String> args = new ArrayList<>();398Path javac = toolBox.getJDKTool("javac");399args.add(javac.toString());400if (includeStandardOptions) {401args.addAll(toolBox.split(System.getProperty("test.tool.vm.opts"), " +"));402args.addAll(toolBox.split(System.getProperty("test.compiler.opts"), " +"));403}404args.addAll(getAllArgs());405406String[] argsArray = args.toArray(new String[args.size()]);407ProcessBuilder pb = getProcessBuilder();408pb.command(argsArray);409try {410return runProcess(toolBox, this, pb.start());411} catch (IOException | InterruptedException e) {412throw new Error(e);413}414}415416private List<String> getAllArgs() {417List<String> args = new ArrayList<>();418if (options != null)419args.addAll(options);420if (outdir != null) {421args.add("-d");422args.add(outdir.toString());423}424if (classpath != null) {425args.add("-classpath");426args.add(toSearchPath(classpath));427}428if (sourcepath != null) {429args.add("-sourcepath");430args.add(toSearchPath(sourcepath));431}432if (classes != null)433args.addAll(classes);434if (files != null)435args.addAll(files);436437return args;438}439440private String toSearchPath(List<Path> files) {441return files.stream()442.map(Path::toString)443.collect(Collectors.joining(File.pathSeparator));444}445446private Iterable<? extends JavaFileObject> joinFiles(447List<String> files, List<JavaFileObject> fileObjects) {448if (files == null)449return fileObjects;450if (internalFileManager == null)451internalFileManager = compiler.getStandardFileManager(null, null, null);452Iterable<? extends JavaFileObject> filesAsFileObjects =453internalFileManager.getJavaFileObjectsFromStrings(files);454if (fileObjects == null)455return filesAsFileObjects;456List<JavaFileObject> combinedList = new ArrayList<>();457for (JavaFileObject o : filesAsFileObjects)458combinedList.add(o);459combinedList.addAll(fileObjects);460return combinedList;461}462}463464465