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 import javax.tools.*; 25 import java.io.ByteArrayOutputStream; 26 import java.io.IOException; 27 import java.io.OutputStream; 28 import java.net.URI; 29 import java.util.Arrays; 30 import java.util.List; 31 import java.util.Map; 32 import java.util.stream.Collectors; 33 34 class Compiler { 35 final private Map<String,String> input; 36 private List<String> options; 37 38 Compiler(Map<String,String> input) { 39 this.input = input; 40 } 41 42 Compiler setRelease(int release) { 43 // Setting the -release option does not work for some reason 44 // so do it the old fashioned way 45 // options = Arrays.asList("-release", String.valueOf(release)); 46 String target = String.valueOf(release); 47 options = Arrays.asList("-source", target, "-target", target, "-classpath", ""); 48 return this; 49 } 50 51 Map<String,byte[]> compile() { 52 List<SourceFileObject> cunits = createCompilationUnits(); 53 Map<String,ClassFileObject> cfos = createClassFileObjects(); 54 JavaCompiler jc = ToolProvider.getSystemJavaCompiler(); 55 JavaFileManager jfm = new CustomFileManager(jc.getStandardFileManager(null, null, null), cfos); 56 if(!jc.getTask(null, jfm, null, options, null, cunits).call()) { 57 throw new RuntimeException("Compilation failed"); 58 } 59 return createOutput(cfos); 60 } 61 62 private List<SourceFileObject> createCompilationUnits() { 63 return input.entrySet().stream() 64 .map(e -> new SourceFileObject(e.getKey(), e.getValue())).collect(Collectors.toList()); 65 } 66 67 private Map<String,ClassFileObject> createClassFileObjects() { 68 return input.keySet().stream() 69 .collect(Collectors.toMap(k -> k, k -> new ClassFileObject(k))); 70 } 71 | 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.*; 27 import java.io.ByteArrayOutputStream; 28 import java.io.IOException; 29 import java.io.OutputStream; 30 import java.net.URI; 31 import java.util.Arrays; 32 import java.util.List; 33 import java.util.Map; 34 import java.util.stream.Collectors; 35 36 public class Compiler { 37 final private Map<String,String> input; 38 private List<String> options; 39 40 public Compiler(Map<String,String> input) { 41 this.input = input; 42 } 43 44 public Compiler setRelease(int release) { 45 // Setting the -release option does not work for some reason 46 // so do it the old fashioned way 47 // options = Arrays.asList("-release", String.valueOf(release)); 48 String target = String.valueOf(release); 49 options = Arrays.asList("-source", target, "-target", target, "-classpath", ""); 50 return this; 51 } 52 53 public Map<String,byte[]> compile() { 54 List<SourceFileObject> cunits = createCompilationUnits(); 55 Map<String,ClassFileObject> cfos = createClassFileObjects(); 56 JavaCompiler jc = ToolProvider.getSystemJavaCompiler(); 57 JavaFileManager jfm = new CustomFileManager(jc.getStandardFileManager(null, null, null), cfos); 58 if(!jc.getTask(null, jfm, null, options, null, cunits).call()) { 59 throw new RuntimeException("Compilation failed"); 60 } 61 return createOutput(cfos); 62 } 63 64 private List<SourceFileObject> createCompilationUnits() { 65 return input.entrySet().stream() 66 .map(e -> new SourceFileObject(e.getKey(), e.getValue())).collect(Collectors.toList()); 67 } 68 69 private Map<String,ClassFileObject> createClassFileObjects() { 70 return input.keySet().stream() 71 .collect(Collectors.toMap(k -> k, k -> new ClassFileObject(k))); 72 } 73 |