/* * Copyright (c) 2013, 2022, 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.
*/
/** * Creates a task to execute {@code javac} using API mode. * @param toolBox the {@code ToolBox} to use
*/ public JavacTask(ToolBox toolBox) { super(toolBox, Task.Mode.API);
}
/** * Creates a task to execute {@code javac} in a specified mode. * @param toolBox the {@code ToolBox} to use * @param mode the mode to be used
*/ public JavacTask(ToolBox toolBox, Task.Mode mode) { super(toolBox, mode);
}
/** * Sets the classpath. * @param classpath the classpath * @return this task object
*/ public JavacTask classpath(String classpath) { this.classpath = Stream.of(classpath.split(File.pathSeparator))
.filter(s -> !s.isEmpty())
.map(s -> Paths.get(s))
.collect(Collectors.toList()); returnthis;
}
/** * Sets the classpath. * @param classpath the classpath * @return this task object
*/ public JavacTask classpath(Path... classpath) { this.classpath = Arrays.asList(classpath); returnthis;
}
/** * Sets the classpath. * @param classpath the classpath * @return this task object
*/ public JavacTask classpath(List<Path> classpath) { this.classpath = classpath; returnthis;
}
/** * Sets the sourcepath. * @param sourcepath the sourcepath * @return this task object
*/ public JavacTask sourcepath(String sourcepath) { this.sourcepath = Stream.of(sourcepath.split(File.pathSeparator))
.filter(s -> !s.isEmpty())
.map(s -> Paths.get(s))
.collect(Collectors.toList()); returnthis;
}
/** * Sets the sourcepath. * @param sourcepath the sourcepath * @return this task object
*/ public JavacTask sourcepath(Path... sourcepath) { this.sourcepath = Arrays.asList(sourcepath); returnthis;
}
/** * Sets the sourcepath. * @param sourcepath the sourcepath * @return this task object
*/ public JavacTask sourcepath(List<Path> sourcepath) { this.sourcepath = sourcepath; returnthis;
}
/** * Sets the output directory. * @param outdir the output directory * @return this task object
*/ public JavacTask outdir(String outdir) { this.outdir = Paths.get(outdir); returnthis;
}
/** * Sets the output directory. * @param outdir the output directory * @return this task object
*/ public JavacTask outdir(Path outdir) { this.outdir = outdir; returnthis;
}
/** * Sets the options. * @param options the options * @return this task object
*/ public JavacTask options(String... options) { this.options = Arrays.asList(options); returnthis;
}
/** * Sets the options. * @param spaceSeparatedOption the space separated options * @return this task object
*/ public JavacTask spaceSeparatedOptions(String spaceSeparatedOption) { this.options = Arrays.asList(spaceSeparatedOption.split("\\s+")); returnthis;
}
/** * Sets the options. * @param options the options * @return this task object
*/ public JavacTask options(List<String> options) { this.options = options; returnthis;
}
/** * Sets the classes to be analyzed. * @param classes the classes * @return this task object
*/ public JavacTask classes(String... classes) { this.classes = Arrays.asList(classes); returnthis;
}
/** * Sets the files to be compiled or analyzed. * @param files the files * @return this task object
*/ public JavacTask files(String... files) { this.files = Arrays.asList(files); returnthis;
}
/** * Sets the files to be compiled or analyzed. * @param files the files * @return this task object
*/ public JavacTask files(Path... files) { this.files = Stream.of(files)
.map(Path::toString)
.collect(Collectors.toList()); returnthis;
}
/** * Sets the files to be compiled or analyzed. * @param files the files * @return this task object
*/ public JavacTask files(List<Path> files) { this.files = files.stream()
.map(Path::toString)
.collect(Collectors.toList()); returnthis;
}
/** * Sets the sources to be compiled or analyzed. * Each source string is converted into an in-memory object that * can be passed directly to the compiler. * @param sources the sources * @return this task object
*/ public JavacTask sources(String... sources) {
fileObjects = Stream.of(sources)
.map(s -> new ToolBox.JavaSource(s))
.collect(Collectors.toList()); returnthis;
}
/** * Sets the annotation processors to be used.
*/ public JavacTask processors(Processor... procs) { this.procs = List.of(procs); returnthis;
}
/** * Sets the file manager to be used by this task. * @param fileManager the file manager * @return this task object
*/ public JavacTask fileManager(JavaFileManager fileManager) { this.fileManager = fileManager; returnthis;
}
/** * Set a callback to be used by this task. * @param callback the callback * @return this task object
*/ public JavacTask callback(Consumer<com.sun.source.util.JavacTask> callback) { this.callback = callback; returnthis;
}
/** * {@inheritDoc} * @return the name "javac"
*/
@Override public String name() { return"javac";
}
/** * Calls the compiler with the arguments as currently configured. * @return a Result object indicating the outcome of the compilation * and the content of any output written to stdout, stderr, or the * main stream by the compiler. * @throws TaskError if the outcome of the task is not as expected.
*/
@Override public Task.Result run() { if (mode == Task.Mode.EXEC) return runExec();
AbstractTask.WriterOutput direct = new AbstractTask.WriterOutput(); // The following are to catch output to System.out and System.err, // in case these are used instead of the primary (main) stream
AbstractTask.StreamOutput sysOut = new AbstractTask.StreamOutput(System.out, System::setOut);
AbstractTask.StreamOutput sysErr = new AbstractTask.StreamOutput(System.err, System::setErr); int rc;
Map<Task.OutputKind, String> outputMap = new HashMap<>(); try { switch (mode == null ? Task.Mode.API : mode) { case API:
rc = runAPI(direct.pw); break; case CMDLINE: if (fileManager != null) { thrownew IllegalStateException("file manager set in CMDLINE mode");
} if (callback != null) { thrownew IllegalStateException("callback set in CMDLINE mode");
}
rc = runCommand(direct.pw); break; default: thrownew IllegalStateException("unknown mode " + mode);
}
} catch (IOException e) {
toolBox.out.println("Exception occurred: " + e);
rc = 99;
} finally {
outputMap.put(Task.OutputKind.STDOUT, sysOut.close());
outputMap.put(Task.OutputKind.STDERR, sysErr.close());
outputMap.put(Task.OutputKind.DIRECT, direct.close());
} return checkExit(new Task.Result(toolBox, this, rc, outputMap));
}
privateint runAPI(PrintWriter pw) throws IOException { try { // if (compiler == null) { // TODO: allow this to be set externally // compiler = ToolProvider.getSystemJavaCompiler();
compiler = JavacTool.create(); // }
if (fileManager == null)
fileManager = internalFileManager = compiler.getStandardFileManager(null, null, null); if (outdir != null)
setLocationFromPaths(StandardLocation.CLASS_OUTPUT, Collections.singletonList(outdir)); if (classpath != null)
setLocationFromPaths(StandardLocation.CLASS_PATH, classpath); if (sourcepath != null)
setLocationFromPaths(StandardLocation.SOURCE_PATH, sourcepath);
List<String> allOpts = new ArrayList<>(); if (options != null)
allOpts.addAll(options);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.