Skip to content

Instantly share code, notes, and snippets.

@wolfc
Created January 25, 2012 11:55
Show Gist options
  • Save wolfc/1675941 to your computer and use it in GitHub Desktop.
Save wolfc/1675941 to your computer and use it in GitHub Desktop.
package org.jboss.noxius.bootstrap;
import javax.tools.DiagnosticCollector;
import javax.tools.JavaCompiler;
import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;
import java.io.Writer;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Arrays;
/**
* @author <a href="mailto:[email protected]">Carlo de Wolf</a>
*/
public class NoxiusCompiler {
static final JavaCompiler COMPILER = ToolProvider.getSystemJavaCompiler();
public Class<?> compile(final URL resource, final String className) throws URISyntaxException, ClassNotFoundException, NoxiusCompileException {
return compile(resource, className, null);
}
public Class<?> compile(final URL resource, final String className, final Iterable<String> options) throws URISyntaxException, ClassNotFoundException, NoxiusCompileException {
final DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
final StandardJavaFileManager standardJavaFileManager = COMPILER.getStandardFileManager(diagnostics, null, null);
final Iterable<URLJavaFileObject> compilationUnits = Arrays.asList(new URLJavaFileObject(resource, className));
final NoxiusFileManager fileManager = new NoxiusFileManager(standardJavaFileManager, compilationUnits);
final Writer out = null; // System.err
final Iterable<String> classes = null;
// System.out.println("options = " + options);
final boolean result = ToolProvider.getSystemJavaCompiler().getTask(out, fileManager, diagnostics, options, classes, compilationUnits).call();
if (!result)
throw new NoxiusCompileException("compilation failed", diagnostics.getDiagnostics());
final ClassLoader parent = Thread.currentThread().getContextClassLoader();
final NoxiusClassLoader classLoader = new NoxiusClassLoader(parent, fileManager);
return Class.forName(className, true, classLoader);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment