Search in sources :

Example 56 with FileObject

use of javax.tools.FileObject in project hibernate-orm by hibernate.

the class ClassWriter method writeFile.

public static void writeFile(MetaEntity entity, Context context) {
    try {
        String metaModelPackage = entity.getPackageName();
        // need to generate the body first, since this will also update the required imports which need to
        // be written out first
        String body = generateBody(entity, context).toString();
        FileObject fo = context.getProcessingEnvironment().getFiler().createSourceFile(getFullyQualifiedClassName(entity, metaModelPackage));
        OutputStream os = fo.openOutputStream();
        PrintWriter pw = new PrintWriter(os);
        if (!metaModelPackage.isEmpty()) {
            pw.println("package " + metaModelPackage + ";");
            pw.println();
        }
        pw.println(entity.generateImports());
        pw.println(body);
        pw.flush();
        pw.close();
    } catch (FilerException filerEx) {
        context.logMessage(Diagnostic.Kind.ERROR, "Problem with Filer: " + filerEx.getMessage());
    } catch (IOException ioEx) {
        context.logMessage(Diagnostic.Kind.ERROR, "Problem opening file to write MetaModel for " + entity.getSimpleName() + ioEx.getMessage());
    }
}
Also used : OutputStream(java.io.OutputStream) FileObject(javax.tools.FileObject) IOException(java.io.IOException) FilerException(javax.annotation.processing.FilerException) PrintWriter(java.io.PrintWriter)

Example 57 with FileObject

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

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();
    }
    try (PrintWriter writer = newFilePrintWriter(file, StandardCharsets.UTF_8)) {
        writer.append(line).append("\n");
    }
}
Also used : FileObject(javax.tools.FileObject) URI(java.net.URI) File(java.io.File) IOException(java.io.IOException) PrintWriter(java.io.PrintWriter) FileUtils.newFilePrintWriter(org.neo4j.io.fs.FileUtils.newFilePrintWriter)

Example 58 with FileObject

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

the class ServiceLoaderGenerator method generate.

@Override
public void generate(String shadowPackage) {
    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.println(shadowPackage + '.' + GEN_CLASS);
        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 59 with FileObject

use of javax.tools.FileObject in project graal by oracle.

the class ServiceProviderProcessor method writeProviderFile.

private void writeProviderFile(TypeElement serviceProvider, String interfaceName) {
    String filename = "META-INF/providers/" + serviceProvider.getQualifiedName();
    try {
        FileObject file = processingEnv.getFiler().createResource(StandardLocation.CLASS_OUTPUT, "", filename, serviceProvider);
        PrintWriter writer = new PrintWriter(new OutputStreamWriter(file.openOutputStream(), "UTF-8"));
        writer.println(interfaceName);
        writer.close();
    } catch (IOException e) {
        processingEnv.getMessager().printMessage(isBug367599(e) ? Kind.NOTE : Kind.ERROR, e.getMessage(), serviceProvider);
    }
}
Also used : OutputStreamWriter(java.io.OutputStreamWriter) FileObject(javax.tools.FileObject) IOException(java.io.IOException) PrintWriter(java.io.PrintWriter)

Example 60 with FileObject

use of javax.tools.FileObject in project graal by oracle.

the class InstrumentRegistrationProcessor method loadIfFileAlreadyExists.

private int loadIfFileAlreadyExists(String filename, Properties p) {
    try {
        FileObject file = processingEnv.getFiler().getResource(StandardLocation.CLASS_OUTPUT, "", filename);
        p.load(file.openInputStream());
        return p.keySet().size() / NUMBER_OF_PROPERTIES_PER_ENTRY;
    } catch (IOException e) {
        // Ignore error. It is ok if the file does not exist
        return 0;
    }
}
Also used : FileObject(javax.tools.FileObject) 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