Search in sources :

Example 71 with FileObject

use of javax.tools.FileObject in project spring-boot by spring-projects.

the class AutoConfigureAnnotationProcessor method writeProperties.

private void writeProperties() throws IOException {
    if (!this.properties.isEmpty()) {
        Filer filer = this.processingEnv.getFiler();
        FileObject file = filer.createResource(StandardLocation.CLASS_OUTPUT, "", PROPERTIES_PATH);
        try (Writer writer = new OutputStreamWriter(file.openOutputStream(), StandardCharsets.UTF_8)) {
            for (Map.Entry<String, String> entry : this.properties.entrySet()) {
                writer.append(entry.getKey());
                writer.append("=");
                writer.append(entry.getValue());
                writer.append(System.lineSeparator());
            }
        }
    }
}
Also used : OutputStreamWriter(java.io.OutputStreamWriter) FileObject(javax.tools.FileObject) Filer(javax.annotation.processing.Filer) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer)

Example 72 with FileObject

use of javax.tools.FileObject in project suite by stupidsing.

the class JdkUtilTest method compile.

private <I> Class<? extends I> compile(Class<I> clazz, String className, String source) {
    var filename = className.replace('.', '/') + ".java";
    var baos_ = new ByteArrayOutputStream();
    try (var baos = baos_) {
        var sjfo = new SimpleJavaFileObject(URI.create(filename), Kind.SOURCE) {

            public CharSequence getCharContent(boolean ignoreEncodingErrors) {
                return source;
            }

            public OutputStream openOutputStream() {
                return baos;
            }
        };
        var sfm = ToolProvider.getSystemJavaCompiler().getStandardFileManager(null, null, null);
        var jfm = new ForwardingJavaFileManager<>(sfm) {

            public JavaFileObject getJavaFileForOutput(Location loc, String clazz, Kind kind, FileObject sibling) {
                return sjfo;
            }
        };
        ToolProvider.getSystemJavaCompiler().getTask(null, jfm, null, null, null, List.of(sjfo)).call();
    } catch (IOException ex) {
        fail(ex);
    }
    return new UnsafeUtil().defineClass(clazz, filename, baos_.toByteArray());
}
Also used : SimpleJavaFileObject(javax.tools.SimpleJavaFileObject) ForwardingJavaFileManager(javax.tools.ForwardingJavaFileManager) Kind(javax.tools.JavaFileObject.Kind) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SimpleJavaFileObject(javax.tools.SimpleJavaFileObject) FileObject(javax.tools.FileObject) JavaFileObject(javax.tools.JavaFileObject) IOException(java.io.IOException) UnsafeUtil(primal.jdk.UnsafeUtil)

Example 73 with FileObject

use of javax.tools.FileObject in project classindex by atteo.

the class ClassIndexProcessor method writeSimpleNameIndexFile.

private void writeSimpleNameIndexFile(Set<String> elementList, String resourceName) throws IOException {
    FileObject file = readOldIndexFile(elementList, resourceName);
    if (file != null) {
        /**
         * Ugly hack for Eclipse JDT incremental compilation.
         * Eclipse JDT can't createResource() after successful getResource().
         * But we can file.openWriter().
         */
        try {
            writeIndexFile(elementList, resourceName, file);
            return;
        } catch (IllegalStateException e) {
        // Thrown by HotSpot Java Compiler
        }
    }
    writeIndexFile(elementList, resourceName, null);
}
Also used : FileObject(javax.tools.FileObject)

Example 74 with FileObject

use of javax.tools.FileObject in project robolectric by robolectric.

the class ServiceLoaderGenerator method generate.

@Override
public void generate() {
    final String fileName = "org.robolectric.internal.ShadowProvider";
    try {
        FileObject file = filer.createResource(StandardLocation.CLASS_OUTPUT, "", "META-INF/services/" + fileName);
        PrintWriter pw = new PrintWriter(new OutputStreamWriter(file.openOutputStream(), "UTF-8"));
        pw.print(shadowPackage + '.' + GEN_CLASS + '\n');
        pw.close();
    } catch (IOException e) {
        messager.printMessage(Diagnostic.Kind.ERROR, "Failed to write service loader metadata file: " + e);
        throw new RuntimeException(e);
    }
}
Also used : OutputStreamWriter(java.io.OutputStreamWriter) FileObject(javax.tools.FileObject) IOException(java.io.IOException) PrintWriter(java.io.PrintWriter)

Example 75 with FileObject

use of javax.tools.FileObject in project neo4j-mobile-android by neo4j-contrib.

the class AnnotationProcessor method addTo.

void addTo(String line, String... path) throws IOException {
    FileObject fo = processingEnv.getFiler().getResource(StandardLocation.CLASS_OUTPUT, "", path(path));
    URI uri = fo.toUri();
    File file;
    try {
        file = new File(uri);
    } catch (Exception e) {
        file = new File(uri.toString());
    }
    if (file.exists()) {
        for (String previous : nl.split(fo.getCharContent(true), 0)) {
            if (line.equals(previous))
                return;
        }
    } else {
        file.getParentFile().mkdirs();
    }
    new FileWriter(file, true).append(line).append("\n").close();
}
Also used : FileWriter(java.io.FileWriter) FileObject(javax.tools.FileObject) URI(java.net.URI) File(java.io.File) IOException(java.io.IOException)

Aggregations

FileObject (javax.tools.FileObject)98 IOException (java.io.IOException)61 File (java.io.File)21 TypeElement (javax.lang.model.element.TypeElement)19 PrintWriter (java.io.PrintWriter)17 Writer (java.io.Writer)16 Filer (javax.annotation.processing.Filer)14 Element (javax.lang.model.element.Element)14 BufferedWriter (java.io.BufferedWriter)12 ArrayList (java.util.ArrayList)12 OutputStream (java.io.OutputStream)11 JavaFileObject (javax.tools.JavaFileObject)11 OutputStreamWriter (java.io.OutputStreamWriter)10 URI (java.net.URI)10 Properties (java.util.Properties)10 InputStream (java.io.InputStream)8 FileWriter (java.io.FileWriter)7 FilerException (javax.annotation.processing.FilerException)7 MainInfo (com.predic8.membrane.annot.model.MainInfo)6 BufferedReader (java.io.BufferedReader)6