use of javax.tools.JavaCompiler.CompilationTask in project ceylon-compiler by ceylon.
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-compiler by ceylon.
the class TestGetResource2 method testMissingResource.
private void testMissingResource(JavaCompiler javac) throws Exception {
System.err.println("testMissingResource");
File tmpdir = new File("testMissingResource");
File srcdir = new File(tmpdir, "src");
File destdir = new File(tmpdir, "dest");
write(srcdir, "pkg/X.java", "package pkg; class X {}");
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 when missing resource: " + result);
expect(result, false);
if (errors > 0)
throw new Exception(errors + " errors occurred");
}
use of javax.tools.JavaCompiler.CompilationTask in project ceylon-compiler by ceylon.
the class TreePosRoundsTest method main.
public static void main(String... args) throws Exception {
String testSrc = System.getProperty("test.src");
String testClasses = System.getProperty("test.classes");
JavaCompiler c = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fm = c.getStandardFileManager(null, null, null);
String thisName = TreePosRoundsTest.class.getName();
File thisFile = new File(testSrc, thisName + ".java");
Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(thisFile);
List<String> options = Arrays.asList("-proc:only", "-processor", thisName, "-processorpath", testClasses);
CompilationTask t = c.getTask(null, fm, null, options, null, files);
boolean ok = t.call();
if (!ok)
throw new Exception("processing failed");
}
use of javax.tools.JavaCompiler.CompilationTask in project kie-wb-common by kiegroup.
the class AbstractProcessorTest method compile.
/**
* Compile a unit of source code with the specified annotation processor
* @param annotationProcessor
* @param compilationUnits
* @return
*/
public List<Diagnostic<? extends JavaFileObject>> compile(final Processor annotationProcessor, final String... compilationUnits) {
final DiagnosticCollector<JavaFileObject> diagnosticListener = new DiagnosticCollector<JavaFileObject>();
try {
final JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
final StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnosticListener, null, null);
String[] convertedCompilationUnits = convertCompilationUnitToFilePaths(compilationUnits);
final Iterable<? extends JavaFileObject> compilationUnitsJavaObjects = fileManager.getJavaFileObjects(convertedCompilationUnits);
// Compile with provide annotation processor
final CompilationTask task = compiler.getTask(null, fileManager, diagnosticListener, null, null, compilationUnitsJavaObjects);
task.setProcessors(Arrays.asList(annotationProcessor));
task.call();
fileManager.close();
} catch (IOException ioe) {
fail(ioe.getMessage());
}
return diagnosticListener.getDiagnostics();
}
use of javax.tools.JavaCompiler.CompilationTask in project checker-framework by typetools.
the class InsertAjavaAnnotations method createElements.
/**
* Gets an instance of {@code Elements} from the current Java compiler.
*
* @return the Element utilities
*/
private static Elements createElements() {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
if (compiler == null) {
System.err.println("Could not get compiler instance");
System.exit(1);
}
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
JavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null);
if (fileManager == null) {
System.err.println("Could not get file manager");
System.exit(1);
}
CompilationTask cTask = compiler.getTask(null, fileManager, diagnostics, Collections.emptyList(), null, Collections.emptyList());
if (!(cTask instanceof JavacTask)) {
System.err.println("Could not get a valid JavacTask: " + cTask.getClass());
System.exit(1);
}
return ((JavacTask) cTask).getElements();
}
Aggregations