Search in sources :

Example 26 with JavacTask

use of com.sun.source.util.JavacTask in project ceylon-compiler by ceylon.

the class Warn5 method test.

static void test(SourceLevel sourceLevel, XlintOption xlint, TrustMe trustMe, SuppressLevel suppressLevel, ModifierKind modKind, MethodKind methKind, SignatureKind sig, BodyKind body) throws Exception {
    final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
    JavaSource source = new JavaSource(trustMe, suppressLevel, modKind, methKind, sig, body);
    DiagnosticChecker dc = new DiagnosticChecker();
    JavacTask ct = (JavacTask) tool.getTask(null, fm, dc, Arrays.asList(xlint.getXlintOption(), "-source", sourceLevel.sourceKey), null, Arrays.asList(source));
    ct.analyze();
    check(sourceLevel, dc, source, xlint, trustMe, suppressLevel, modKind, methKind, sig, body);
}
Also used : JavaCompiler(javax.tools.JavaCompiler) JavacTask(com.sun.source.util.JavacTask)

Example 27 with JavacTask

use of com.sun.source.util.JavacTask in project bazel by bazelbuild.

the class BlazeJavacMain method compile.

public static BlazeJavacResult compile(BlazeJavacArguments arguments) {
    List<String> javacArguments = arguments.javacOptions();
    try {
        javacArguments = processPluginArgs(arguments.plugins(), javacArguments);
    } catch (InvalidCommandLineException e) {
        return BlazeJavacResult.error(e.getMessage());
    }
    Context context = new Context();
    setupBlazeJavaCompiler(arguments.plugins(), context);
    boolean ok = false;
    StringWriter errOutput = new StringWriter();
    // TODO(cushon): where is this used when a diagnostic listener is registered? Consider removing
    // it and handling exceptions directly in callers.
    PrintWriter errWriter = new PrintWriter(errOutput);
    Listener diagnostics = new Listener(context);
    BlazeJavaCompiler compiler;
    try (JavacFileManager fileManager = new ClassloaderMaskingFileManager()) {
        JavacTask task = JavacTool.create().getTask(errWriter, fileManager, diagnostics, javacArguments, ImmutableList.of(), /*classes*/
        fileManager.getJavaFileObjectsFromPaths(arguments.sourceFiles()), context);
        if (arguments.processors() != null) {
            task.setProcessors(arguments.processors());
        }
        fileManager.setContext(context);
        setLocations(fileManager, arguments);
        try {
            ok = task.call();
        } catch (PropagatedException e) {
            throw e.getCause();
        }
    } catch (Throwable t) {
        t.printStackTrace(errWriter);
        ok = false;
    } finally {
        compiler = (BlazeJavaCompiler) JavaCompiler.instance(context);
        if (ok) {
            // or empty source files.
            if (compiler.skippedFlowEvents() > 0 && compiler.flowEvents() == 0) {
                errWriter.println("Expected at least one FLOW event");
                ok = false;
            }
        }
    }
    errWriter.flush();
    return new BlazeJavacResult(ok, filterDiagnostics(diagnostics.build()), errOutput.toString(), compiler);
}
Also used : Context(com.sun.tools.javac.util.Context) Listener(com.google.devtools.build.buildjar.javac.FormattedDiagnostic.Listener) PropagatedException(com.sun.tools.javac.util.PropagatedException) JavacFileManager(com.sun.tools.javac.file.JavacFileManager) StringWriter(java.io.StringWriter) InvalidCommandLineException(com.google.devtools.build.buildjar.InvalidCommandLineException) JavacTask(com.sun.source.util.JavacTask) PrintWriter(java.io.PrintWriter)

Example 28 with JavacTask

use of com.sun.source.util.JavacTask in project bazel by bazelbuild.

the class JavacTurbineCompiler method compile.

static JavacTurbineCompileResult compile(JavacTurbineCompileRequest request) throws IOException {
    Map<String, OutputFileObject> files = new LinkedHashMap<>();
    Status status;
    StringWriter sw = new StringWriter();
    Context context = new Context();
    try (PrintWriter pw = new PrintWriter(sw)) {
        setupContext(context, request.strictJavaDepsPlugin());
        CacheFSInfo.preRegister(context);
        try (ZipOutputFileManager fm = new ZipOutputFileManager(files)) {
            JavacTask task = JavacTool.create().getTask(pw, fm, null, /*diagnostics*/
            request.javacOptions(), ImmutableList.of(), /*classes*/
            fm.getJavaFileObjectsFromPaths(request.sources()), context);
            fm.setContext(context);
            fm.setLocationFromPaths(StandardLocation.SOURCE_PATH, Collections.<Path>emptyList());
            fm.setLocationFromPaths(StandardLocation.CLASS_PATH, request.classPath());
            fm.setLocationFromPaths(StandardLocation.PLATFORM_CLASS_PATH, request.bootClassPath());
            fm.setLocationFromPaths(StandardLocation.ANNOTATION_PROCESSOR_PATH, request.processorClassPath());
            status = task.call() ? Status.OK : Status.ERROR;
        } catch (Throwable t) {
            t.printStackTrace(pw);
            status = Status.ERROR;
        }
    }
    return new JavacTurbineCompileResult(ImmutableMap.copyOf(files), status, sw, context);
}
Also used : Status(com.google.devtools.build.java.turbine.javac.JavacTurbineCompileResult.Status) Context(com.sun.tools.javac.util.Context) OutputFileObject(com.google.devtools.build.java.turbine.javac.ZipOutputFileManager.OutputFileObject) StringWriter(java.io.StringWriter) JavacTask(com.sun.source.util.JavacTask) LinkedHashMap(java.util.LinkedHashMap) PrintWriter(java.io.PrintWriter)

Example 29 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 30 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)

Aggregations

JavacTask (com.sun.source.util.JavacTask)46 JavaCompiler (javax.tools.JavaCompiler)20 JavacTool (com.sun.tools.javac.api.JavacTool)12 CompilationUnitTree (com.sun.source.tree.CompilationUnitTree)10 JavaFileObject (javax.tools.JavaFileObject)9 StandardJavaFileManager (javax.tools.StandardJavaFileManager)7 SimpleJavaFileObject (javax.tools.SimpleJavaFileObject)6 Context (com.sun.tools.javac.util.Context)5 File (java.io.File)5 IOException (java.io.IOException)5 PrintWriter (java.io.PrintWriter)5 StringWriter (java.io.StringWriter)5 JavacFileManager (com.sun.tools.javac.file.JavacFileManager)4 ClassTree (com.sun.source.tree.ClassTree)3 Trees (com.sun.source.util.Trees)3 JCCompilationUnit (com.sun.tools.javac.tree.JCTree.JCCompilationUnit)3 Path (java.nio.file.Path)3 ArrayList (java.util.ArrayList)3 CeyloncTool (com.redhat.ceylon.compiler.java.tools.CeyloncTool)2 Tree (com.sun.source.tree.Tree)2