Search in sources :

Example 61 with CompilationTask

use of javax.tools.JavaCompiler.CompilationTask in project ceylon by eclipse.

the class T6458823 method main.

public static void main(String[] args) throws Exception {
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    if (compiler == null) {
        throw new RuntimeException("can't get javax.tools.JavaCompiler!");
    }
    DiagnosticCollector<JavaFileObject> diagColl = new DiagnosticCollector<JavaFileObject>();
    StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null);
    List<String> options = new ArrayList<String>();
    options.add("-processor");
    options.add("MyProcessor");
    options.add("-proc:only");
    List<File> files = new ArrayList<File>();
    files.add(new File(T6458823.class.getResource("TestClass.java").toURI()));
    final CompilationTask task = compiler.getTask(null, fm, diagColl, options, null, fm.getJavaFileObjectsFromFiles(files));
    task.call();
    int diagCount = 0;
    for (Diagnostic<? extends JavaFileObject> diag : diagColl.getDiagnostics()) {
        if (diag.getKind() != Diagnostic.Kind.WARNING) {
            throw new AssertionError("Only warnings expected");
        }
        System.out.println(diag);
        if (diag.getPosition() == Diagnostic.NOPOS) {
            throw new AssertionError("No position info in message");
        }
        if (diag.getSource() == null) {
            throw new AssertionError("No source info in message");
        }
        diagCount++;
    }
    if (diagCount != 2) {
        throw new AssertionError("unexpected number of warnings: " + diagCount + ", expected: 2");
    }
}
Also used : ArrayList(java.util.ArrayList) JavaCompiler(javax.tools.JavaCompiler) CompilationTask(javax.tools.JavaCompiler.CompilationTask) JavaFileObject(javax.tools.JavaFileObject) StandardJavaFileManager(javax.tools.StandardJavaFileManager) DiagnosticCollector(javax.tools.DiagnosticCollector) File(java.io.File)

Example 62 with CompilationTask

use of javax.tools.JavaCompiler.CompilationTask in project ceylon by eclipse.

the class T7031108 method compile.

void compile(JavaCompiler comp, JavaFileManager fm, DiagnosticListener<JavaFileObject> dl, String processor, JavaFileObject... files) throws Exception {
    System.err.println("compile processor:" + processor + ", files:" + Arrays.asList(files));
    List<String> opts = new ArrayList<String>();
    if (processor != null) {
        // opts.add("-verbose");
        opts.addAll(Arrays.asList("-processor", processor));
    }
    CompilationTask task = comp.getTask(null, fm, dl, opts, null, Arrays.asList(files));
    boolean ok = task.call();
    if (dl == null && !ok)
        throw new Exception("compilation failed");
}
Also used : CompilationTask(javax.tools.JavaCompiler.CompilationTask)

Example 63 with CompilationTask

use of javax.tools.JavaCompiler.CompilationTask in project ceylon by eclipse.

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 64 with CompilationTask

use of javax.tools.JavaCompiler.CompilationTask in project ceylon by eclipse.

the class EagerInterfaceCompletionTest method compile.

void compile(DiagnosticChecker dc, JavaSource... sources) {
    try {
        CompilationTask ct = javacTool.getTask(null, null, dc, Arrays.asList("-d", testDir.getAbsolutePath(), "-cp", testDir.getAbsolutePath()), null, Arrays.asList(sources));
        ct.call();
    } catch (Exception e) {
        e.printStackTrace();
        error("Internal compilation error");
    }
}
Also used : CompilationTask(javax.tools.JavaCompiler.CompilationTask)

Example 65 with CompilationTask

use of javax.tools.JavaCompiler.CompilationTask in project ceylon by eclipse.

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)

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