Search in sources :

Example 36 with CompilationTask

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);
}
Also used : CompilationTask(javax.tools.JavaCompiler.CompilationTask)

Example 37 with CompilationTask

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");
}
Also used : CompilationTask(javax.tools.JavaCompiler.CompilationTask)

Example 38 with CompilationTask

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");
}
Also used : CompilationTask(javax.tools.JavaCompiler.CompilationTask)

Example 39 with CompilationTask

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();
}
Also used : JavaFileObject(javax.tools.JavaFileObject) JavaCompiler(javax.tools.JavaCompiler) StandardJavaFileManager(javax.tools.StandardJavaFileManager) DiagnosticCollector(javax.tools.DiagnosticCollector) IOException(java.io.IOException) CompilationTask(javax.tools.JavaCompiler.CompilationTask)

Example 40 with CompilationTask

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();
}
Also used : JavaFileObject(javax.tools.JavaFileObject) JavaFileManager(javax.tools.JavaFileManager) JavaCompiler(javax.tools.JavaCompiler) DiagnosticCollector(javax.tools.DiagnosticCollector) JavacTask(com.sun.source.util.JavacTask) CompilationTask(javax.tools.JavaCompiler.CompilationTask)

Aggregations

CompilationTask (javax.tools.JavaCompiler.CompilationTask)84 JavaCompiler (javax.tools.JavaCompiler)38 File (java.io.File)33 JavaFileObject (javax.tools.JavaFileObject)32 StandardJavaFileManager (javax.tools.StandardJavaFileManager)28 DiagnosticCollector (javax.tools.DiagnosticCollector)27 IOException (java.io.IOException)20 ArrayList (java.util.ArrayList)18 SimpleJavaFileObject (javax.tools.SimpleJavaFileObject)14 URLClassLoader (java.net.URLClassLoader)10 StringWriter (java.io.StringWriter)9 URL (java.net.URL)6 JavaFileManager (javax.tools.JavaFileManager)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 Test (org.junit.Test)5 OutputStream (java.io.OutputStream)4 PrintWriter (java.io.PrintWriter)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 FileInputStream (java.io.FileInputStream)3 FileOutputStream (java.io.FileOutputStream)3