Search in sources :

Example 1 with JavaFileManager

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

the class LombokProcessor method placePostCompileAndDontMakeForceRoundDummiesHook.

private void placePostCompileAndDontMakeForceRoundDummiesHook() {
    stopJavacProcessingEnvironmentFromClosingOurClassloader();
    forceMultipleRoundsInNetBeansEditor();
    Context context = processingEnv.getContext();
    disablePartialReparseInNetBeansEditor(context);
    try {
        Method keyMethod = Context.class.getDeclaredMethod("key", Class.class);
        keyMethod.setAccessible(true);
        Object key = keyMethod.invoke(context, JavaFileManager.class);
        Field htField = Context.class.getDeclaredField("ht");
        htField.setAccessible(true);
        @SuppressWarnings("unchecked") Map<Object, Object> ht = (Map<Object, Object>) htField.get(context);
        final JavaFileManager originalFiler = (JavaFileManager) ht.get(key);
        if (!(originalFiler instanceof InterceptingJavaFileManager)) {
            final Messager messager = processingEnv.getMessager();
            DiagnosticsReceiver receiver = new MessagerDiagnosticsReceiver(messager);
            JavaFileManager newFiler = new InterceptingJavaFileManager(originalFiler, receiver);
            ht.put(key, newFiler);
            Field filerFileManagerField = JavacFiler.class.getDeclaredField("fileManager");
            filerFileManagerField.setAccessible(true);
            filerFileManagerField.set(processingEnv.getFiler(), newFiler);
        }
    } catch (Exception e) {
        throw Lombok.sneakyThrow(e);
    }
}
Also used : Context(com.sun.tools.javac.util.Context) Messager(javax.annotation.processing.Messager) JavaFileManager(javax.tools.JavaFileManager) Method(java.lang.reflect.Method) IOException(java.io.IOException) Field(java.lang.reflect.Field) DiagnosticsReceiver(lombok.core.DiagnosticsReceiver) JavaFileObject(javax.tools.JavaFileObject) Map(java.util.Map) IdentityHashMap(java.util.IdentityHashMap)

Example 2 with JavaFileManager

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

the class Processor method findServices.

private void findServices(StringBuilder message, Filer filer) {
    try {
        Field filerFileManagerField = JavacFiler.class.getDeclaredField("fileManager");
        filerFileManagerField.setAccessible(true);
        JavaFileManager jfm = (JavaFileManager) filerFileManagerField.get(filer);
        ClassLoader processorClassLoader = jfm.hasLocation(ANNOTATION_PROCESSOR_PATH) ? jfm.getClassLoader(ANNOTATION_PROCESSOR_PATH) : jfm.getClassLoader(CLASS_PATH);
        Enumeration<URL> resources = processorClassLoader.getResources("META-INF/services/javax.annotation.processing.Processor");
        if (!resources.hasMoreElements()) {
            message.append("No processors discovered\n\n");
            return;
        }
        message.append("Discovered processors:\n");
        while (resources.hasMoreElements()) {
            URL processorUrl = resources.nextElement();
            message.append("- '").append(processorUrl).append("'");
            InputStream content = (InputStream) processorUrl.getContent();
            if (content != null)
                try {
                    InputStreamReader reader = new InputStreamReader(content, "UTF-8");
                    StringWriter sw = new StringWriter();
                    char[] buffer = new char[8192];
                    int read = 0;
                    while ((read = reader.read(buffer)) != -1) {
                        sw.write(buffer, 0, read);
                    }
                    String wholeFile = sw.toString();
                    if (wholeFile.contains("lombok.javac.apt.Processor")) {
                        message.append(" <= problem\n");
                    } else {
                        message.append(" (ok)\n");
                    }
                    message.append("    ").append(wholeFile.replace("\n", "\n    ")).append("\n");
                } finally {
                    content.close();
                }
        }
    } catch (Exception e) {
        message.append("Filer information unavailable\n");
    }
    message.append("\n");
}
Also used : Field(java.lang.reflect.Field) InputStreamReader(java.io.InputStreamReader) StringWriter(java.io.StringWriter) JavaFileManager(javax.tools.JavaFileManager) InputStream(java.io.InputStream) URL(java.net.URL) IOException(java.io.IOException)

Example 3 with JavaFileManager

use of javax.tools.JavaFileManager in project zoj by licheng.

the class CustomJavaCompiler method main.

public static void main(String[] args) {
    if (args.length != 1) {
        System.err.println("Invalid arg length " + args.length);
        System.exit(-1);
    }
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    StandardJavaFileManager stdFileManager = compiler.getStandardFileManager(null, null, null);
    JavaFileManager fileManager = new CustomJavaFileManager(stdFileManager);
    System.out.println(new File(args[0]).getAbsolutePath());
    System.out.println(compiler.getTask(null, fileManager, null, null, null, stdFileManager.getJavaFileObjects(args[0])).call());
}
Also used : ForwardingJavaFileManager(javax.tools.ForwardingJavaFileManager) StandardJavaFileManager(javax.tools.StandardJavaFileManager) JavaFileManager(javax.tools.JavaFileManager) JavaCompiler(javax.tools.JavaCompiler) StandardJavaFileManager(javax.tools.StandardJavaFileManager) File(java.io.File)

Example 4 with JavaFileManager

use of javax.tools.JavaFileManager 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 5 with JavaFileManager

use of javax.tools.JavaFileManager in project error-prone by google.

the class ErrorPronePlugins method loadPlugins.

public static ScannerSupplier loadPlugins(ScannerSupplier scannerSupplier, Context context) {
    JavaFileManager fileManager = context.get(JavaFileManager.class);
    // discovery, we never search CLASS_PATH.
    if (!fileManager.hasLocation(StandardLocation.ANNOTATION_PROCESSOR_PATH)) {
        return scannerSupplier;
    }
    ClassLoader loader = fileManager.getClassLoader(StandardLocation.ANNOTATION_PROCESSOR_PATH);
    Iterable<BugChecker> extraBugCheckers = ServiceLoader.load(BugChecker.class, loader);
    if (Iterables.isEmpty(extraBugCheckers)) {
        return scannerSupplier;
    }
    return scannerSupplier.plus(ScannerSupplier.fromBugCheckerClasses(Iterables.transform(extraBugCheckers, GET_CLASS)));
}
Also used : JavaFileManager(javax.tools.JavaFileManager) BugChecker(com.google.errorprone.bugpatterns.BugChecker)

Aggregations

JavaFileManager (javax.tools.JavaFileManager)10 JavaCompiler (javax.tools.JavaCompiler)5 JavaFileObject (javax.tools.JavaFileObject)5 StandardJavaFileManager (javax.tools.StandardJavaFileManager)5 IOException (java.io.IOException)4 ForwardingJavaFileManager (javax.tools.ForwardingJavaFileManager)4 DiagnosticCollector (javax.tools.DiagnosticCollector)3 SimpleJavaFileObject (javax.tools.SimpleJavaFileObject)3 InputStream (java.io.InputStream)2 Field (java.lang.reflect.Field)2 ArrayList (java.util.ArrayList)2 Diagnostic (javax.tools.Diagnostic)2 ImmutableList (com.google.common.collect.ImmutableList)1 BugChecker (com.google.errorprone.bugpatterns.BugChecker)1 JavacFileManager (com.sun.tools.javac.file.JavacFileManager)1 Context (com.sun.tools.javac.util.Context)1 Log (com.sun.tools.javac.util.Log)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 InputStreamReader (java.io.InputStreamReader)1