Search in sources :

Example 1 with DiagnosticListener

use of javax.tools.DiagnosticListener in project lombok by rzwitserloot.

the class TestClassFileMetaData method compile.

static byte[] compile(File file) {
    try {
        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
        File tempDir = getTempDir();
        tempDir.mkdirs();
        List<String> options = Arrays.asList("-proc:none", "-d", tempDir.getAbsolutePath());
        StringWriter captureWarnings = new StringWriter();
        final StringBuilder compilerErrors = new StringBuilder();
        DiagnosticListener<JavaFileObject> diagnostics = new DiagnosticListener<JavaFileObject>() {

            @Override
            public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
                compilerErrors.append(diagnostic.toString()).append("\n");
            }
        };
        CompilationTask task = compiler.getTask(captureWarnings, null, diagnostics, options, null, Collections.singleton(new ContentBasedJavaFileObject(file.getPath(), readFileAsString(file))));
        Boolean taskResult = task.call();
        assertTrue("Compilation task didn't succeed: \n<Warnings and Errors>\n" + compilerErrors.toString() + "\n</Warnings and Errors>", taskResult);
        return PostCompilerApp.readFile(new File(tempDir, file.getName().replaceAll("\\.java$", ".class")));
    } catch (Exception e) {
        throw Lombok.sneakyThrow(e);
    }
}
Also used : JavaCompiler(javax.tools.JavaCompiler) Diagnostic(javax.tools.Diagnostic) DiagnosticListener(javax.tools.DiagnosticListener) CompilationTask(javax.tools.JavaCompiler.CompilationTask) IOException(java.io.IOException) SimpleJavaFileObject(javax.tools.SimpleJavaFileObject) JavaFileObject(javax.tools.JavaFileObject) StringWriter(java.io.StringWriter) File(java.io.File)

Example 2 with DiagnosticListener

use of javax.tools.DiagnosticListener in project bazel by bazelbuild.

the class BazelJavaCompiler method newInstance.

private static JavaCompiler newInstance(final JavaCompiler delegate) {
    // We forward most operations to the JavaCompiler implementation in langtools.jar.
    return new JavaCompiler() {

        @Override
        public CompilationTask getTask(Writer out, JavaFileManager fileManager, DiagnosticListener<? super JavaFileObject> diagnosticListener, Iterable<String> options, Iterable<String> classes, Iterable<? extends JavaFileObject> compilationUnits) {
            // We prepend bazel's default javacopts to user javacopts,
            // so that the user can override them. javac supports this
            // "last option wins" style of option override.
            ImmutableList.Builder<String> fullOptions = ImmutableList.builder();
            fullOptions.addAll(getDefaultJavacopts());
            if (options != null) {
                fullOptions.addAll(options);
            }
            return delegate.getTask(out, fileManager, diagnosticListener, fullOptions.build(), classes, compilationUnits);
        }

        @Override
        public StandardJavaFileManager getStandardFileManager(DiagnosticListener<? super JavaFileObject> diagnosticListener, Locale locale, Charset charset) {
            StandardJavaFileManager fileManager = delegate.getStandardFileManager(diagnosticListener, locale, charset);
            try {
                fileManager.setLocation(// bootclasspath
                StandardLocation.PLATFORM_CLASS_PATH, JavacBootclasspath.asFiles());
            } catch (IOException e) {
                // Should never happen, according to javadocs for setLocation
                throw new RuntimeException(e);
            }
            return fileManager;
        }

        @Override
        public int run(InputStream in, OutputStream out, OutputStream err, String... arguments) {
            // prepend bazel's default javacopts to user arguments
            List<String> args = ImmutableList.<String>builder().addAll(getDefaultJavacopts()).add(arguments).build();
            return delegate.run(in, out, err, args.toArray(new String[0]));
        }

        @Override
        public Set<SourceVersion> getSourceVersions() {
            return delegate.getSourceVersions();
        }

        @Override
        public int isSupportedOption(String option) {
            return delegate.isSupportedOption(option);
        }
    };
}
Also used : Locale(java.util.Locale) JavaFileManager(javax.tools.JavaFileManager) StandardJavaFileManager(javax.tools.StandardJavaFileManager) ImmutableList(com.google.common.collect.ImmutableList) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) JavaCompiler(javax.tools.JavaCompiler) Charset(java.nio.charset.Charset) DiagnosticListener(javax.tools.DiagnosticListener) SourceVersion(javax.lang.model.SourceVersion) IOException(java.io.IOException) JavaFileObject(javax.tools.JavaFileObject) StandardJavaFileManager(javax.tools.StandardJavaFileManager) Writer(java.io.Writer)

Example 3 with DiagnosticListener

use of javax.tools.DiagnosticListener in project ceylon-compiler by ceylon.

the class JavapFileManager method create.

public static JavapFileManager create(final DiagnosticListener<? super JavaFileObject> dl, PrintWriter log) {
    Context javac_context = new Context();
    if (dl != null)
        javac_context.put(DiagnosticListener.class, dl);
    javac_context.put(com.sun.tools.javac.util.Log.outKey, log);
    return new JavapFileManager(javac_context, null);
}
Also used : Context(com.sun.tools.javac.util.Context) DiagnosticListener(javax.tools.DiagnosticListener)

Example 4 with DiagnosticListener

use of javax.tools.DiagnosticListener in project ceylon-compiler by ceylon.

the class JavahTask method getDiagnosticListenerForWriter.

private DiagnosticListener<JavaFileObject> getDiagnosticListenerForWriter(Writer w) {
    final PrintWriter pw = getPrintWriterForWriter(w);
    return new DiagnosticListener<JavaFileObject>() {

        public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
            if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
                pw.print(getMessage("err.prefix"));
                pw.print(" ");
            }
            pw.println(diagnostic.getMessage(null));
        }
    };
}
Also used : JavaFileObject(javax.tools.JavaFileObject) Diagnostic(javax.tools.Diagnostic) DiagnosticListener(javax.tools.DiagnosticListener) PrintWriter(java.io.PrintWriter)

Example 5 with DiagnosticListener

use of javax.tools.DiagnosticListener in project ceylon-compiler by ceylon.

the class CeyloncTool method getTask.

public CeyloncTaskImpl getTask(Writer out, JavaFileManager fileManager, DiagnosticListener<? super JavaFileObject> diagnosticListener, Iterable<String> options, Iterable<String> classes, Iterable<? extends JavaFileObject> compilationUnits) {
    //        final String kindMsg = "All compilation units must be of SOURCE kind";
    if (options != null)
        for (String option : options) // null check
        option.getClass();
    if (classes != null) {
        for (String cls : classes) if (// implicit null check
        !SourceVersion.isName(cls) && // FIX for ceylon because default is not a valid name for Java
        !"default".equals(cls))
            throw new IllegalArgumentException("Not a valid class name: " + cls);
    }
    if (fileManager == null)
        fileManager = getStandardFileManager(out, diagnosticListener, null, null);
    Context context = ((CeyloncFileManager) fileManager).getContext();
    if (diagnosticListener != null && context.get(DiagnosticListener.class) == null)
        context.put(DiagnosticListener.class, diagnosticListener);
    context.put(JavaFileManager.class, fileManager);
    JavacTool.processOptions(context, fileManager, options);
    Main compiler = new Main("ceyloncTask", context.get(Log.outKey));
    return new CeyloncTaskImpl(compiler, options, context, classes, compilationUnits);
}
Also used : Context(com.sun.tools.javac.util.Context) DiagnosticListener(javax.tools.DiagnosticListener) Main(com.redhat.ceylon.compiler.java.launcher.Main)

Aggregations

DiagnosticListener (javax.tools.DiagnosticListener)10 JavaFileObject (javax.tools.JavaFileObject)6 Context (com.sun.tools.javac.util.Context)4 PrintWriter (java.io.PrintWriter)4 Diagnostic (javax.tools.Diagnostic)4 IOException (java.io.IOException)3 JavaCompiler (javax.tools.JavaCompiler)3 OutputStream (java.io.OutputStream)2 SimpleJavaFileObject (javax.tools.SimpleJavaFileObject)2 ImmutableList (com.google.common.collect.ImmutableList)1 Main (com.redhat.ceylon.compiler.java.launcher.Main)1 CompilationUnitTree (com.sun.source.tree.CompilationUnitTree)1 ErroneousTree (com.sun.source.tree.ErroneousTree)1 JavacTask (com.sun.source.util.JavacTask)1 TreeScanner (com.sun.source.util.TreeScanner)1 Trees (com.sun.source.util.Trees)1 File (java.io.File)1 InputStream (java.io.InputStream)1 StringWriter (java.io.StringWriter)1 Writer (java.io.Writer)1