Use JavaCompiler in tests isntead of manually finding "javac"
This commit is contained in:
@@ -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<? extends JavaFileObject> 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();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user