use of javax.tools.JavaCompiler.CompilationTask in project ceylon by eclipse.
the class TestSuperclass method run.
int run(JavaCompiler comp, StandardJavaFileManager fm) throws IOException {
System.err.println("test: ck:" + ck + " gk:" + gk + " sk:" + sk);
File testDir = new File(ck + "-" + gk + "-" + sk);
testDir.mkdirs();
fm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(testDir));
JavaSource js = new JavaSource();
System.err.println(js.getCharContent(false));
CompilationTask t = comp.getTask(null, fm, null, null, null, Arrays.asList(js));
if (!t.call())
throw new Error("compilation failed");
File testClass = new File(testDir, "Test.class");
String out = javap(testClass);
// Extract class sig from first line of Java source
String expect = js.source.replaceAll("(?s)^(.* Test[^{]+?) *\\{.*", "$1");
// Extract class sig from line from javap output
String found = out.replaceAll("(?s).*\n(.* Test[^{]+?) *\\{.*", "$1");
checkEqual("class signature", expect, found);
return errors;
}
use of javax.tools.JavaCompiler.CompilationTask in project ceylon by eclipse.
the class TestGetResource2 method testCompositeSourcePath.
private void testCompositeSourcePath(JavaCompiler javac) throws Exception {
System.err.println("testCompositeSearchPath");
File tmpdir = new File("testCompositeSourcePath");
File srcdir = new File(tmpdir, "src");
File rsrcdir = new File(tmpdir, "rsrc");
File destdir = new File(tmpdir, "dest");
write(srcdir, "pkg/X.java", "package pkg; class X {}");
write(rsrcdir, "resources/file.txt", "hello");
destdir.mkdirs();
CompilationTask task = javac.getTask(null, null, null, Arrays.asList("-sourcepath", srcdir + File.pathSeparator + rsrcdir, "-d", destdir.toString()), Collections.singleton("pkg.X"), null);
task.setProcessors(Collections.singleton(new AnnoProc()));
boolean result = task.call();
System.err.println("javac result with composite source path: " + result);
expect(result, true);
}
use of javax.tools.JavaCompiler.CompilationTask in project ceylon by eclipse.
the class TestGetResource2 method testSingleSourceDir.
private void testSingleSourceDir(JavaCompiler javac) throws Exception {
System.err.println("testSingleSourceDir");
File tmpdir = new File("testSingleSourceDir");
File srcdir = new File(tmpdir, "src");
File destdir = new File(tmpdir, "dest");
write(srcdir, "pkg/X.java", "package pkg; class X {}");
write(srcdir, "resources/file.txt", "hello");
destdir.mkdirs();
CompilationTask task = javac.getTask(null, null, null, Arrays.asList("-sourcepath", srcdir.toString(), "-d", destdir.toString()), Collections.singleton("pkg.X"), null);
task.setProcessors(Collections.singleton(new AnnoProc()));
boolean result = task.call();
System.err.println("javac result with single source dir: " + result);
expect(result, true);
}
use of javax.tools.JavaCompiler.CompilationTask in project com.revolsys.open by revolsys.
the class ScriptRunner method run.
public void run() {
if (this.scriptFile == null) {
} else if (this.scriptFile.exists() && this.scriptFile.isFile()) {
final String fileExtension = FileUtil.getFileNameExtension(this.scriptFile);
try {
if ("java".equals(fileExtension)) {
final String scriptClassName = FileUtil.getBaseName(this.scriptFile);
final URIJavaFileObject scriptJavaFile = new URIJavaFileObject(this.scriptFile, Kind.SOURCE);
final JavaCompiler javaCompiler = ToolProvider.getSystemJavaCompiler();
final JavaFileManager fileManager = new InMemoryJavaFileManager(scriptJavaFile);
final CompilationTask compilationTask = javaCompiler.getTask(null, fileManager, null, null, null, Collections.singleton(scriptJavaFile));
compilationTask.call();
final ClassLoader classLoader = fileManager.getClassLoader(null);
final Class<?> scriptClass;
try {
scriptClass = classLoader.loadClass(scriptClassName);
} catch (final ClassNotFoundException e) {
showErrorDialog(//
"must contain the class:<br />" + "<code>public class " + scriptClassName + //
"</code><br />" + "in the default package.");
return;
}
final Method mainMethod;
try {
mainMethod = scriptClass.getDeclaredMethod("main", String[].class);
} catch (final NoSuchMethodException e) {
showErrorDialog("must contain the method:<br />" + //
"<code>public static void main(String[] args)</code><br />" + //
" in the class:<br />" + "<code>" + scriptClassName + "</code>");
return;
}
try {
mainMethod.invoke(null, (Object) NULL_MAIN_ARGS);
} catch (final InvocationTargetException e) {
final Throwable targetException = e.getTargetException();
showErrorDialog(scriptClass, targetException);
} catch (final Throwable e) {
showErrorDialog(scriptClass, e);
return;
}
} else {
final Binding binding = new Binding();
final GroovyShell shell = new GroovyShell(binding);
shell.run(this.scriptFile, NULL_MAIN_ARGS);
}
} catch (final Throwable e) {
showErrorDialog(getClass(), e);
}
} else {
showErrorDialog("Does not exist");
}
}
use of javax.tools.JavaCompiler.CompilationTask in project mdw-designer by CenturyLinkCloud.
the class ProcessProxy method compile.
private void compile(IFile sourceFile) {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
// JavaFileObject file = new JavaSourceFromString(getClassName(), source);
//
// Iterable<? extends JavaFileObject> compilationUnits = Arrays.asList(file);
// CompilationTask task = compiler.getTask(null, null, diagnostics, null, null,
// compilationUnits);
StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
List<File> sourceFileList = new ArrayList<File>();
sourceFileList.add(sourceFile.getRawLocation().toFile());
Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromFiles(sourceFileList);
CompilationTask task = compiler.getTask(null, fileManager, null, null, null, compilationUnits);
boolean success = task.call();
for (Diagnostic<?> diagnostic : diagnostics.getDiagnostics()) {
System.out.println(diagnostic.getCode());
System.out.println(diagnostic.getKind());
System.out.println(diagnostic.getPosition());
System.out.println(diagnostic.getStartPosition());
System.out.println(diagnostic.getEndPosition());
System.out.println(diagnostic.getSource());
System.out.println(diagnostic.getMessage(null));
}
System.out.println("\nSUCCESS: " + success);
}
Aggregations