From a27e921ed86cbd5380f8f9d9a5c501f2fa941272 Mon Sep 17 00:00:00 2001 From: Rob Winch Date: Wed, 30 Oct 2013 13:45:54 -0500 Subject: [PATCH] Use JavaCompiler in tests isntead of manually finding "javac" --- .../odm/test/utils/CompilerInterface.java | 37 +++++++------------ 1 file changed, 13 insertions(+), 24 deletions(-) diff --git a/odm/src/test/java/org/springframework/ldap/odm/test/utils/CompilerInterface.java b/odm/src/test/java/org/springframework/ldap/odm/test/utils/CompilerInterface.java index e4cb6f21..afb27d2a 100755 --- a/odm/src/test/java/org/springframework/ldap/odm/test/utils/CompilerInterface.java +++ b/odm/src/test/java/org/springframework/ldap/odm/test/utils/CompilerInterface.java @@ -1,35 +1,24 @@ package org.springframework.ldap.odm.test.utils; import java.io.File; -import java.io.InputStream; -import java.io.InputStreamReader; +import java.util.Arrays; + +import javax.tools.JavaCompiler; +import javax.tools.JavaFileObject; +import javax.tools.StandardJavaFileManager; +import javax.tools.ToolProvider; public class CompilerInterface { // Compile the given file - when we can drop Java 5 we'll use the Java 6 compiler API public static void compile(String directory, String file) throws Exception { + File toCompile = new File(directory, file); + JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); + StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null); - ProcessBuilder pb = new ProcessBuilder( - new String[] { "javac", - "-cp", "."+File.pathSeparatorChar+"target"+File.separatorChar+"classes"+ - File.pathSeparatorChar+System.getProperty("java.class.path"), - directory+File.separatorChar+file }); + Iterable javaFileObjects = + fileManager.getJavaFileObjectsFromFiles(Arrays.asList(toCompile)); + compiler.getTask(null, fileManager, null, null, null, javaFileObjects).call(); - pb.redirectErrorStream(true); - Process proc = pb.start(); - InputStream is = proc.getInputStream(); - InputStreamReader isr = new InputStreamReader(is); - - char[] buf = new char[1024]; - int count; - StringBuilder builder = new StringBuilder(); - while ((count = isr.read(buf)) > 0) { - builder.append(buf, 0, count); - } - - boolean ok = proc.waitFor() == 0; - - if (!ok) { - throw new RuntimeException(builder.toString()); - } + fileManager.close(); } }