Search in sources :

Example 6 with JavacTool

use of com.sun.tools.javac.api.JavacTool in project ceylon-compiler by ceylon.

the class TestSuppression method test.

void test(String src, WarningKind wk, int gen) throws Exception {
    count++;
    System.err.println("Test " + count + ": wk:" + wk + " gen:" + gen + " src:" + src);
    File testDir = new File("test" + count);
    File srcDir = createDir(testDir, "src");
    File gensrcDir = createDir(testDir, "gensrc");
    File classesDir = createDir(testDir, "classes");
    File x = writeFile(new File(srcDir, "X.java"), src);
    DiagListener dl = new DiagListener();
    JavacTool tool = JavacTool.create();
    StandardJavaFileManager fm = tool.getStandardFileManager(dl, null, null);
    fm.setLocation(StandardLocation.CLASS_PATH, Arrays.asList(classesDir, new File(System.getProperty("test.classes"))));
    fm.setLocation(StandardLocation.CLASS_OUTPUT, Collections.singleton(classesDir));
    fm.setLocation(StandardLocation.SOURCE_OUTPUT, Collections.singleton(gensrcDir));
    List<String> args = new ArrayList<String>();
    //        args.add("-XprintProcessorInfo");
    args.add("-XprintRounds");
    args.add("-Agen=" + gen);
    if (wk == WarningKind.YES)
        args.add("-Xlint:serial");
    Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(x);
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    JavacTask task = tool.getTask(pw, fm, dl, args, null, files);
    task.setProcessors(Arrays.asList(new AnnoProc()));
    boolean ok = task.call();
    pw.close();
    System.err.println("ok:" + ok + " diags:" + dl.counts);
    if (sw.toString().length() > 0) {
        System.err.println("output:\n" + sw.toString());
    }
    for (Diagnostic.Kind dk : Diagnostic.Kind.values()) {
        Integer v = dl.counts.get(dk);
        int found = (v == null) ? 0 : v;
        int expect = (dk == Diagnostic.Kind.WARNING && wk == WarningKind.YES) ? gen : 0;
        if (found != expect) {
            error("Unexpected value for " + dk + ": expected: " + expect + " found: " + found);
        }
    }
    System.err.println();
}
Also used : JavacTool(com.sun.tools.javac.api.JavacTool) JCDiagnostic(com.sun.tools.javac.util.JCDiagnostic) JavacTask(com.sun.source.util.JavacTask)

Example 7 with JavacTool

use of com.sun.tools.javac.api.JavacTool in project ceylon-compiler by ceylon.

the class T6993305 method run.

void run() throws Exception {
    File testSrc = new File(System.getProperty("test.src"));
    JavacTool tool = JavacTool.create();
    StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
    File f = new File(testSrc, T6993305.class.getSimpleName() + ".java");
    Iterable<? extends JavaFileObject> fos = fm.getJavaFileObjects(f);
    JavacTask task = tool.getTask(null, fm, null, null, null, fos);
    Iterable<? extends CompilationUnitTree> cus = task.parse();
    TestScanner s = new TestScanner();
    s.scan(cus, task);
    if (errors > 0)
        throw new Exception(errors + " errors occurred");
}
Also used : JavacTool(com.sun.tools.javac.api.JavacTool) StandardJavaFileManager(javax.tools.StandardJavaFileManager) JavacTask(com.sun.source.util.JavacTask) File(java.io.File) IOException(java.io.IOException)

Example 8 with JavacTool

use of com.sun.tools.javac.api.JavacTool in project bazel by bazelbuild.

the class JavacTurbineTest method compileLib.

private void compileLib(Path jar, Collection<Path> classpath, Iterable<? extends JavaFileObject> units) throws IOException {
    final Path outdir = temp.newFolder().toPath();
    JavacFileManager fm = new JavacFileManager(new Context(), false, UTF_8);
    fm.setLocationFromPaths(StandardLocation.CLASS_OUTPUT, Collections.singleton(outdir));
    fm.setLocationFromPaths(StandardLocation.CLASS_PATH, classpath);
    List<String> options = Arrays.asList("-d", outdir.toString());
    JavacTool tool = JavacTool.create();
    JavacTask task = tool.getTask(new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.err, UTF_8)), true), fm, null, options, null, units);
    assertThat(task.call()).isTrue();
    try (JarOutputStream jos = new JarOutputStream(Files.newOutputStream(jar))) {
        Files.walkFileTree(outdir, new SimpleFileVisitor<Path>() {

            @Override
            public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException {
                JarEntry je = new JarEntry(outdir.relativize(path).toString());
                jos.putNextEntry(je);
                Files.copy(path, jos);
                return FileVisitResult.CONTINUE;
            }
        });
    }
}
Also used : Path(java.nio.file.Path) Context(com.sun.tools.javac.util.Context) JavacTool(com.sun.tools.javac.api.JavacTool) JarOutputStream(java.util.jar.JarOutputStream) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) JarEntry(java.util.jar.JarEntry) BufferedWriter(java.io.BufferedWriter) JavacFileManager(com.sun.tools.javac.file.JavacFileManager) OutputStreamWriter(java.io.OutputStreamWriter) JavacTask(com.sun.source.util.JavacTask) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) PrintWriter(java.io.PrintWriter)

Example 9 with JavacTool

use of com.sun.tools.javac.api.JavacTool in project error-prone by google.

the class BugCheckerRefactoringTestHelper method doCompile.

private JCCompilationUnit doCompile(final JavaFileObject input, Iterable<JavaFileObject> files, Context context) throws IOException {
    JavacTool tool = JavacTool.create();
    DiagnosticCollector<JavaFileObject> diagnosticsCollector = new DiagnosticCollector<>();
    context.put(ErrorProneOptions.class, ErrorProneOptions.empty());
    JavacTaskImpl task = (JavacTaskImpl) tool.getTask(CharStreams.nullWriter(), fileManager, diagnosticsCollector, options, /*classes=*/
    null, files, context);
    Iterable<? extends CompilationUnitTree> trees = task.parse();
    task.analyze();
    JCCompilationUnit tree = Iterables.getOnlyElement(Iterables.filter(Iterables.filter(trees, JCCompilationUnit.class), compilationUnit -> compilationUnit.getSourceFile() == input));
    Iterable<Diagnostic<? extends JavaFileObject>> errorDiagnostics = Iterables.filter(diagnosticsCollector.getDiagnostics(), d -> d.getKind() == Diagnostic.Kind.ERROR);
    if (!Iterables.isEmpty(errorDiagnostics)) {
        fail("compilation failed unexpectedly: " + errorDiagnostics);
    }
    return tree;
}
Also used : JCCompilationUnit(com.sun.tools.javac.tree.JCTree.JCCompilationUnit) Iterables(com.google.common.collect.Iterables) JavacTaskImpl(com.sun.tools.javac.api.JavacTaskImpl) HashMap(java.util.HashMap) DescriptionBasedDiff(com.google.errorprone.apply.DescriptionBasedDiff) ErrorProneScannerTransformer(com.google.errorprone.scanner.ErrorProneScannerTransformer) ErrorProneScanner(com.google.errorprone.scanner.ErrorProneScanner) ImmutableList(com.google.common.collect.ImmutableList) CharStreams(com.google.common.io.CharStreams) JavacTool(com.sun.tools.javac.api.JavacTool) Diagnostic(javax.tools.Diagnostic) Map(java.util.Map) Assert.fail(org.junit.Assert.fail) Fix(com.google.errorprone.fixes.Fix) DiagnosticCollector(javax.tools.DiagnosticCollector) Truth.assertAbout(com.google.common.truth.Truth.assertAbout) JavaFileObjects(com.google.testing.compile.JavaFileObjects) TreePath(com.sun.source.util.TreePath) BugChecker(com.google.errorprone.bugpatterns.BugChecker) IOException(java.io.IOException) Truth.assertThat(com.google.common.truth.Truth.assertThat) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) JCCompilationUnit(com.sun.tools.javac.tree.JCTree.JCCompilationUnit) JavaSourceSubjectFactory.javaSource(com.google.testing.compile.JavaSourceSubjectFactory.javaSource) JavaFileObject(javax.tools.JavaFileObject) SourceFile(com.google.errorprone.apply.SourceFile) List(java.util.List) Description(com.google.errorprone.matchers.Description) Context(com.sun.tools.javac.util.Context) JCClassDecl(com.sun.tools.javac.tree.JCTree.JCClassDecl) JavaFileObject(javax.tools.JavaFileObject) JavacTaskImpl(com.sun.tools.javac.api.JavacTaskImpl) JavacTool(com.sun.tools.javac.api.JavacTool) Diagnostic(javax.tools.Diagnostic) DiagnosticCollector(javax.tools.DiagnosticCollector)

Example 10 with JavacTool

use of com.sun.tools.javac.api.JavacTool in project ceylon-compiler by ceylon.

the class VerifyingTaskListener method main.

public static void main(String[] args) throws IOException {
    JavacTool tool = JavacTool.create();
    StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
    Iterable<? extends JavaFileObject> files = fm.getJavaFileObjectsFromFiles(Arrays.asList(new File(testSrcDir, self + ".java")));
    Iterable<String> options = Arrays.asList("-processorpath", testClassDir, "-processor", self, "-s", ".", "-d", ".");
    JavacTask task = tool.getTask(out, fm, null, options, null, files);
    VerifyingTaskListener vtl = new VerifyingTaskListener(new File(testSrcDir, self + ".out"));
    task.setTaskListener(vtl);
    if (!task.call())
        throw new AssertionError("compilation failed");
    if (vtl.iter.hasNext() || vtl.errors)
        throw new AssertionError("comparison against golden file failed.");
}
Also used : JavacTool(com.sun.tools.javac.api.JavacTool)

Aggregations

JavacTool (com.sun.tools.javac.api.JavacTool)18 JavacTask (com.sun.source.util.JavacTask)11 CompilationUnitTree (com.sun.source.tree.CompilationUnitTree)5 JCCompilationUnit (com.sun.tools.javac.tree.JCTree.JCCompilationUnit)4 File (java.io.File)3 IOException (java.io.IOException)3 StandardJavaFileManager (javax.tools.StandardJavaFileManager)3 JCClassDecl (com.sun.tools.javac.tree.JCTree.JCClassDecl)2 Context (com.sun.tools.javac.util.Context)2 PrintWriter (java.io.PrintWriter)2 ImmutableList (com.google.common.collect.ImmutableList)1 Iterables (com.google.common.collect.Iterables)1 CharStreams (com.google.common.io.CharStreams)1 Truth.assertAbout (com.google.common.truth.Truth.assertAbout)1 Truth.assertThat (com.google.common.truth.Truth.assertThat)1 DescriptionBasedDiff (com.google.errorprone.apply.DescriptionBasedDiff)1 SourceFile (com.google.errorprone.apply.SourceFile)1 BugChecker (com.google.errorprone.bugpatterns.BugChecker)1 Fix (com.google.errorprone.fixes.Fix)1 Description (com.google.errorprone.matchers.Description)1