1 /* 2 * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 package jdk.test.lib.compiler; 25 26 import javax.tools.JavaCompiler; 27 import javax.tools.StandardJavaFileManager; 28 import javax.tools.StandardLocation; 29 import javax.tools.ToolProvider; 30 import java.io.IOException; 31 import java.nio.file.Files; 32 import java.nio.file.Path; 33 import java.util.Arrays; 34 import java.util.Collections; 35 import java.util.List; 36 import java.util.stream.Collectors; 37 38 /** 39 * This class consists exclusively of static utility methods for invoking the 40 * java compiler. 41 */ 42 public final class CompilerUtils { 43 private CompilerUtils() { } 44 45 /** 46 * Compile all the java sources in {@code <source>/**} to 47 * {@code <destination>/**}. The destination directory will be created if 48 * it doesn't exist. 49 * 50 * Equivalent to calling {@code compile(source, destination, true, options);}. 51 * 52 * All warnings/errors emitted by the compiler are output to System.out/err. 53 * 54 * @param source Path to the source directory 55 * @param destination Path to the destination directory 56 * @param options Any options to pass to the compiler 57 * 58 * @return true if the compilation is successful 59 * 60 * @throws IOException 61 * if there is an I/O error scanning the source tree or 62 * creating the destination directory 63 * @throws UnsupportedOperationException 64 * if there is no system java compiler 65 */ 66 public static boolean compile(Path source, Path destination, String... options) 67 throws IOException 68 { 69 return compile(source, destination, true, options); 70 } 71 72 /** 73 * Compile all the java sources in {@code <source>} and optionally its 74 * subdirectories, to 75 * {@code <destination>}. The destination directory will be created if 76 * it doesn't exist. 77 * 78 * All warnings/errors emitted by the compiler are output to System.out/err. 79 * 80 * @param source Path to the source directory 81 * @param destination Path to the destination directory 82 * @param recurse If {@code true} recurse into any {@code source} subdirectories 83 * to compile all java source files; else only compile those directly in 84 * {@code source}. 85 * @param options Any options to pass to the compiler 86 * 87 * @return true if the compilation is successful 88 * 89 * @throws IOException 90 * if there is an I/O error scanning the source tree or 91 * creating the destination directory 92 * @throws UnsupportedOperationException 93 * if there is no system java compiler 94 */ 95 96 public static boolean compile(Path source, Path destination, boolean recurse, String... options) 97 throws IOException 98 { 99 JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); 100 if (compiler == null) { 101 // no compiler available 102 throw new UnsupportedOperationException("Unable to get system java compiler. " 103 + "Perhaps, jdk.compiler module is not available."); 104 } 105 StandardJavaFileManager jfm = compiler.getStandardFileManager(null, null, null); 106 107 List<Path> sources 108 = Files.find(source, (recurse ? Integer.MAX_VALUE : 1), 109 (file, attrs) -> (file.toString().endsWith(".java"))) 110 .collect(Collectors.toList()); 111 112 Files.createDirectories(destination); 113 jfm.setLocation(StandardLocation.CLASS_PATH, Collections.emptyList()); 114 jfm.setLocationFromPaths(StandardLocation.CLASS_OUTPUT, 115 Collections.singletonList(destination)); 116 117 List<String> opts = Arrays.asList(options); 118 JavaCompiler.CompilationTask task 119 = compiler.getTask(null, jfm, null, opts, null, 120 jfm.getJavaFileObjectsFromPaths(sources)); 121 122 return task.call(); 123 } 124 }