Search in sources :

Example 56 with JavacTask

use of com.sun.source.util.JavacTask 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 57 with JavacTask

use of com.sun.source.util.JavacTask in project jdk8u_jdk by JetBrains.

the class FDTest method run.

void run(JavaCompiler tool, StandardJavaFileManager fm) throws Exception {
    JavacTask ct = (JavacTask) tool.getTask(null, fm, diagChecker, null, null, Arrays.asList(source));
    try {
        ct.analyze();
    } catch (Throwable ex) {
        fail("Error thrown when analyzing the following source:\n" + source.getCharContent(true));
    }
    check();
}
Also used : JavacTask(com.sun.source.util.JavacTask)

Example 58 with JavacTask

use of com.sun.source.util.JavacTask in project j2objc by google.

the class JavacParser method parse.

@Override
public CompilationUnit parse(String mainType, String path, String source) {
    try {
        JavacEnvironment parserEnv = createEnvironment(path, source);
        JavacTask task = parserEnv.task();
        CompilationUnitTree unit = task.parse().iterator().next();
        task.analyze();
        processDiagnostics(parserEnv.diagnostics());
        return TreeConverter.convertCompilationUnit(options, parserEnv, unit);
    } catch (IOException e) {
        ErrorUtil.fatalError(e, path);
    }
    return null;
}
Also used : CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) IOException(java.io.IOException) JavacTask(com.sun.source.util.JavacTask)

Example 59 with JavacTask

use of com.sun.source.util.JavacTask in project error-prone by google.

the class ErrorProneJavacPluginTest method noPolicyGiven.

@Test
public void noPolicyGiven() throws IOException {
    FileSystem fileSystem = Jimfs.newFileSystem(Configuration.unix());
    Path source = fileSystem.getPath("Test.java");
    Files.write(source, "class Test {}".getBytes(UTF_8));
    JavacFileManager fileManager = new JavacFileManager(new Context(), false, UTF_8);
    DiagnosticCollector<JavaFileObject> diagnosticCollector = new DiagnosticCollector<>();
    StringWriter sw = new StringWriter();
    JavacTask task = JavacTool.create().getTask(new PrintWriter(sw, true), fileManager, diagnosticCollector, ImmutableList.of("-Xplugin:ErrorProne"), ImmutableList.of(), fileManager.getJavaFileObjects(source));
    try {
        task.call();
        fail();
    } catch (Throwable expected) {
        assertThat(expected).hasMessageThat().contains("The default compilation policy (by-todo) is not supported");
    }
}
Also used : Path(java.nio.file.Path) JavacFileManager(com.sun.tools.javac.file.JavacFileManager) Context(com.sun.tools.javac.util.Context) JavaFileObject(javax.tools.JavaFileObject) StringWriter(java.io.StringWriter) FileSystem(java.nio.file.FileSystem) DiagnosticCollector(javax.tools.DiagnosticCollector) JavacTask(com.sun.source.util.JavacTask) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 60 with JavacTask

use of com.sun.source.util.JavacTask in project error-prone by google.

the class ErrorProneJavacPluginTest method applyToPatchFile.

@Test
public void applyToPatchFile() throws IOException {
    // TODO(b/63064865): Test is broken on Windows.  Disable for now.
    Assume.assumeFalse(StandardSystemProperty.OS_NAME.value().startsWith("Windows"));
    Path tmp = temporaryFolder.newFolder().toPath();
    Path patchDir = temporaryFolder.newFolder().toPath();
    Files.createDirectories(patchDir);
    Path patchFile = patchDir.resolve("error-prone.patch");
    // verify that any existing content in the patch file is deleted
    Files.write(patchFile, ImmutableList.of("--- C.java", "--- D.java"), UTF_8);
    Path fileA = tmp.resolve("A.java");
    Path fileB = tmp.resolve("B.java");
    Files.write(fileA, ImmutableList.of(// 
    "class A implements Runnable {", "  public void run() {}", "}"), UTF_8);
    Files.write(fileB, ImmutableList.of(// 
    "class B implements Runnable {", "  public void run() {}", "}"), UTF_8);
    JavacFileManager fileManager = new JavacFileManager(new Context(), false, UTF_8);
    DiagnosticCollector<JavaFileObject> diagnosticCollector = new DiagnosticCollector<>();
    JavacTask task = JavacTool.create().getTask(null, fileManager, diagnosticCollector, ImmutableList.of("-Xplugin:ErrorProne" + " -XepPatchChecks:MissingOverride -XepPatchLocation:" + patchDir.toString(), "-XDcompilePolicy=byfile"), ImmutableList.of(), fileManager.getJavaFileObjects(fileA, fileB));
    assertThat(task.call()).named(Joiner.on('\n').join(diagnosticCollector.getDiagnostics())).isTrue();
    assertThat(Files.readAllLines(patchFile, UTF_8).stream().filter(l -> l.startsWith("--- ")).map(l -> Paths.get(l.substring("--- ".length())).getFileName().toString()).collect(toImmutableList())).containsExactly("A.java", "B.java");
}
Also used : Path(java.nio.file.Path) JavacFileManager(com.sun.tools.javac.file.JavacFileManager) Context(com.sun.tools.javac.util.Context) RunWith(org.junit.runner.RunWith) MoreCollectors.onlyElement(com.google.common.collect.MoreCollectors.onlyElement) StandardSystemProperty(com.google.common.base.StandardSystemProperty) ImmutableList(com.google.common.collect.ImmutableList) JavacTool(com.sun.tools.javac.api.JavacTool) Jimfs(com.google.common.jimfs.Jimfs) Diagnostic(javax.tools.Diagnostic) Assert.fail(org.junit.Assert.fail) Assume(org.junit.Assume) ENGLISH(java.util.Locale.ENGLISH) Path(java.nio.file.Path) DiagnosticCollector(javax.tools.DiagnosticCollector) PrintWriter(java.io.PrintWriter) Configuration(com.google.common.jimfs.Configuration) JavacFileManager(com.sun.tools.javac.file.JavacFileManager) JavacTask(com.sun.source.util.JavacTask) Files(java.nio.file.Files) UTF_8(java.nio.charset.StandardCharsets.UTF_8) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) StringWriter(java.io.StringWriter) IOException(java.io.IOException) Test(org.junit.Test) JUnit4(org.junit.runners.JUnit4) Truth.assertThat(com.google.common.truth.Truth.assertThat) FileSystem(java.nio.file.FileSystem) JavaFileObject(javax.tools.JavaFileObject) Rule(org.junit.Rule) Paths(java.nio.file.Paths) Context(com.sun.tools.javac.util.Context) TemporaryFolder(org.junit.rules.TemporaryFolder) Joiner(com.google.common.base.Joiner) JavaFileObject(javax.tools.JavaFileObject) DiagnosticCollector(javax.tools.DiagnosticCollector) JavacTask(com.sun.source.util.JavacTask) Test(org.junit.Test)

Aggregations

JavacTask (com.sun.source.util.JavacTask)97 JavaCompiler (javax.tools.JavaCompiler)45 JavaFileObject (javax.tools.JavaFileObject)27 JavacTool (com.sun.tools.javac.api.JavacTool)24 CompilationUnitTree (com.sun.source.tree.CompilationUnitTree)23 StandardJavaFileManager (javax.tools.StandardJavaFileManager)17 SimpleJavaFileObject (javax.tools.SimpleJavaFileObject)16 IOException (java.io.IOException)14 DiagnosticCollector (javax.tools.DiagnosticCollector)13 File (java.io.File)12 StringWriter (java.io.StringWriter)12 Context (com.sun.tools.javac.util.Context)11 PrintWriter (java.io.PrintWriter)11 JavacFileManager (com.sun.tools.javac.file.JavacFileManager)9 Path (java.nio.file.Path)8 ArrayList (java.util.ArrayList)8 JCCompilationUnit (com.sun.tools.javac.tree.JCTree.JCCompilationUnit)7 Diagnostic (javax.tools.Diagnostic)7 ClassTree (com.sun.source.tree.ClassTree)6 Trees (com.sun.source.util.Trees)6