Java Compiler API - JavaFileManager, DiagnosticListener and DiagnosticCollector Examples
|
|
Java Compiler API Java public class FileManagerExample { public static void main(String[] args) throws ExecutionException, InterruptedException, IOException { JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
//Already available DiagnosticListener implementation DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>();
//that's how we get the instance of StandardJavaFileManager //the compiler just returns the new instance. StandardJavaFileManager standardFileManager = compiler.getStandardFileManager(diagnostics, null, null);
File file = new File("test-sources/FileWithErrors.java");
//files are wrapped into JavaFileObjects by StandardFileManager Iterable<? extends JavaFileObject> javaFileObjects = standardFileManager.getJavaFileObjects(file); for (JavaFileObject javaFileObject : javaFileObjects) { System.out.println(javaFileObject.getClass()); }
//we still have to pass file manager and others as compiler doesn't track //anything about them. Also there might be a new instance of customized //StandardFileManager here. JavaCompiler.CompilationTask task = compiler.getTask( null, standardFileManager, diagnostics, null, null, javaFileObjects);
Future<Boolean> future = Executors.newFixedThreadPool(1).submit(task); Boolean result = future.get(); if (result != null && result) { System.out.println("Compilation done"); } else { // we might show the diagnostics other than // standard output e.g. in some GUI screen. diagnostics.getDiagnostics().forEach(System.out::println); } standardFileManager.close(); } }
Original Post
import javax.tools.*; import java.io.IOException; import java.util.Arrays;
public class StringCompilation {
public static void main(String[] args) throws Exception { JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>();
String className = "Test";
final JavaByteObject byteObject = new JavaByteObject(className);
StandardJavaFileManager standardFileManager = compiler.getStandardFileManager(diagnostics, null, null);
JavaFileManager fileManager = createFileManager(standardFileManager, byteObject);
JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, diagnostics, null, null, getCompilationUnits(className));
if (!task.call()) { diagnostics.getDiagnostics().forEach(System.out::println); } fileManager.close();
//loading and using our compiled class final ClassLoader inMemoryClassLoader = createClassLoader(byteObject); Class<ITest> test = (Class<ITest>) inMemoryClassLoader.loadClass(className); ITest iTest = test.newInstance(); iTest.doSomething(); }
private static JavaFileManager createFileManager(StandardJavaFileManager fileManager, JavaByteObject byteObject) { return new ForwardingJavaFileManager<StandardJavaFileManager>(fileManager) { @Override public JavaFileObject getJavaFileForOutput(Location location, String className, JavaFileObject.Kind kind, FileObject sibling) throws IOException { return byteObject; } }; }
private static ClassLoader createClassLoader(final JavaByteObject byteObject) { return new ClassLoader() { @Override public Class<?> findClass(String name) throws ClassNotFoundException { //no need to search class path, we already have byte code. byte[] bytes = byteObject.getBytes(); return defineClass(name, bytes, 0, bytes.length); } }; }
public static Iterable<? extends JavaFileObject> getCompilationUnits(String className) { JavaStringObject stringObject = new JavaStringObject(className, getSource()); return Arrays.asList(stringObject); }
public static String getSource() { return "public class Test implements com.logicbig.example.ITest{" + "public void doSomething(){" + "System.out.println(\"testing\");}}"; } }
Original Post
public class StringCompilation {
private static final File outputFile;
static { String outputPath = System.getProperty("user.dir") + File.separatorChar + "compiledClasses"; outputFile = new File(outputPath); if (!outputFile.exists()) { try { Files.createDirectory(outputFile.toPath()); } catch (IOException e) { throw new RuntimeException(e); } } }
public static void main(String[] args) throws Exception { JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null);
fileManager.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(outputFile));
JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, diagnostics, null, null, getCompilationUnits());
if (!task.call()) { diagnostics.getDiagnostics().forEach(System.out::println); } fileManager.close();
//loading and using our compiled class ClassLoader loader = new URLClassLoader(new URL[]{outputFile.toURI().toURL()}); Class<ITest> test = (Class<ITest>) loader.loadClass("Test"); ITest iTest = test.newInstance(); iTest.doSomething(); }
public static Iterable<? extends JavaFileObject> getCompilationUnits() { JavaStringObject stringObject = new JavaStringObject("Test", getSource()); return Arrays.asList(stringObject); }
public static String getSource() { return "public class Test implements com.logicbig.example.ITest{" + "public void doSomething(){" + "System.out.println(\"testing\");}}"; } }
Original Post
|
|
|
Share
|
|
|
|