Path: blob/master/test/langtools/tools/jdeps/lib/CompilerUtils.java
41152 views
/*1* Copyright (c) 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*/2223import javax.tools.JavaCompiler;24import javax.tools.StandardJavaFileManager;25import javax.tools.StandardLocation;26import javax.tools.ToolProvider;27import java.io.IOException;28import java.io.UncheckedIOException;29import java.nio.file.FileVisitResult;30import java.nio.file.Files;31import java.nio.file.Path;32import java.nio.file.SimpleFileVisitor;33import java.nio.file.attribute.BasicFileAttributes;34import java.util.ArrayList;35import java.util.Arrays;36import java.util.List;37import java.util.stream.Collectors;38import java.util.stream.Stream;3940public final class CompilerUtils {41private CompilerUtils() { }4243/**44* Compile all the java sources in {@code <source>/**} to45* {@code <destination>/**}. The destination directory will be created if46* it doesn't exist.47*48* All warnings/errors emitted by the compiler are output to System.out/err.49*50* @return true if the compilation is successful51*52* @throws IOException if there is an I/O error scanning the source tree or53* creating the destination directory54*/55public static boolean compile(Path source, Path destination, String... options)56throws IOException57{58JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();59StandardJavaFileManager jfm = compiler.getStandardFileManager(null, null, null);6061List<Path> sources62= Files.find(source, Integer.MAX_VALUE,63(file, attrs) -> (file.toString().endsWith(".java")))64.collect(Collectors.toList());6566Files.createDirectories(destination);67jfm.setLocationFromPaths(StandardLocation.CLASS_OUTPUT,68Arrays.asList(destination));6970List<String> opts = Arrays.asList(options);71JavaCompiler.CompilationTask task72= compiler.getTask(null, jfm, null, opts, null,73jfm.getJavaFileObjectsFromPaths(sources));7475return task.call();76}7778/**79* Compile the specified module from the given module sourcepath80*81* All warnings/errors emitted by the compiler are output to System.out/err.82*83* @return true if the compilation is successful84*85* @throws IOException if there is an I/O error scanning the source tree or86* creating the destination directory87*/88public static boolean compileModule(Path source, Path destination,89String moduleName, String... options) {90JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();91StandardJavaFileManager jfm = compiler.getStandardFileManager(null, null, null);9293try {94Files.createDirectories(destination);95jfm.setLocationFromPaths(StandardLocation.CLASS_OUTPUT,96Arrays.asList(destination));97} catch (IOException e) {98throw new UncheckedIOException(e);99}100101Stream<String> opts = Arrays.stream(new String[] {102"--module-source-path", source.toString(), "-m", moduleName103});104List<String> javacOpts = Stream.concat(opts, Arrays.stream(options))105.collect(Collectors.toList());106JavaCompiler.CompilationTask task107= compiler.getTask(null, jfm, null, javacOpts, null, null);108return task.call();109}110111112public static void cleanDir(Path dir) throws IOException {113if (Files.notExists(dir)) return;114115Files.walkFileTree(dir, new SimpleFileVisitor<Path>() {116@Override117public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)118throws IOException119{120Files.delete(file);121return FileVisitResult.CONTINUE;122}123@Override124public FileVisitResult postVisitDirectory(Path dir, IOException e)125throws IOException126{127if (e == null) {128Files.delete(dir);129return FileVisitResult.CONTINUE;130} else {131// directory iteration failed132throw e;133}134}135});136Files.deleteIfExists(dir);137}138}139140141