Search in sources :

Example 51 with FileObject

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

the class SpiProcessorPersistence method determineOutputLocation.

private File determineOutputLocation() {
    FileObject resource;
    try {
        resource = filer.createResource(StandardLocation.CLASS_OUTPUT, "META-INF", "locator.tmp");
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        // Could happen
        return null;
    } catch (IOException e) {
        e.printStackTrace();
        logger.printMessage(Kind.NOTE, "IOException while determining output location: " + e.getMessage());
        return null;
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
        return null;
    }
    URI uri = resource.toUri();
    return new File(new File(uri).getParentFile(), "services");
}
Also used : FileNotFoundException(java.io.FileNotFoundException) FileObject(javax.tools.FileObject) IOException(java.io.IOException) URI(java.net.URI) File(java.io.File)

Example 52 with FileObject

use of javax.tools.FileObject in project buck by facebook.

the class ClassUsageTrackerTest method readingNonJavaFileFromGetFileForOutputShouldBeTracked.

@Test
public void readingNonJavaFileFromGetFileForOutputShouldBeTracked() throws IOException {
    final FileObject fileObject = fileManager.getFileForOutput(null, null, SINGLE_NON_JAVA_FILE_NAME, null);
    fileObject.openInputStream();
    assertTrue(fileWasRead(TEST_JAR_PATH, SINGLE_NON_JAVA_FILE_NAME));
}
Also used : FileObject(javax.tools.FileObject) JavaFileObject(javax.tools.JavaFileObject) Test(org.junit.Test)

Example 53 with FileObject

use of javax.tools.FileObject in project buck by facebook.

the class MyAnnotationProcessor method process.

@Override
public boolean process(final Set<? extends TypeElement> annotations, final RoundEnvironment roundEnv) {
    if (runCount > 0) {
        return false;
    }
    try {
        List<String> required_configs = Arrays.asList("res1.json");
        List<String> maybe_configs = Arrays.asList("res2.json");
        OutputStream outputStream = mFiler.createResource(StandardLocation.CLASS_OUTPUT, "com.facebook.example.config", "collected_configs.json", mElementUtils.getTypeElement("com.facebook.example.MyAnnotationProcessor")).openOutputStream();
        StringBuilder sb = new StringBuilder();
        sb.append("{\n");
        byte[] buffer = new byte[1024];
        for (String config : required_configs) {
            try {
                FileObject inputFile = mFiler.getResource(StandardLocation.CLASS_PATH, "", "META-INF/" + config);
                int len = inputFile.openInputStream().read(buffer);
                String contents = new String(buffer, 0, len);
                sb.append("    \"").append(config).append("\": ");
                sb.append("\"").append(contents).append("\"");
                sb.append(",\n");
            } catch (Exception e) {
                mMessager.printMessage(Diagnostic.Kind.ERROR, "Could not open " + config + " because of " + e.getLocalizedMessage());
            }
        }
        for (String config : maybe_configs) {
            try {
                FileObject inputFile = mFiler.getResource(StandardLocation.CLASS_PATH, "", "META-INF/" + config);
                int len = inputFile.openInputStream().read(buffer);
                String contents = new String(buffer, 0, len);
                sb.append("    \"").append(config).append("\": ");
                sb.append("\"").append(contents).append("\"");
                sb.append(",\n");
            } catch (Exception e) {
                mMessager.printMessage(Diagnostic.Kind.NOTE, config + " not found");
            }
        }
        sb.append("\"version\": 1");
        sb.append("\n}");
        outputStream.write(sb.toString().getBytes());
        outputStream.close();
    } catch (Exception e) {
        mMessager.printMessage(Diagnostic.Kind.ERROR, "" + e);
        throw new RuntimeException(e);
    }
    runCount += 1;
    return true;
}
Also used : OutputStream(java.io.OutputStream) FileObject(javax.tools.FileObject)

Example 54 with FileObject

use of javax.tools.FileObject in project camel by apache.

the class AnnotationProcessorHelper method processFile.

/**
     * Helper method to produce class output text file using the given handler
     */
public static void processFile(ProcessingEnvironment processingEnv, String packageName, String fileName, Func1<PrintWriter, Void> handler) {
    PrintWriter writer = null;
    try {
        Writer out;
        Filer filer = processingEnv.getFiler();
        FileObject resource;
        try {
            resource = filer.getResource(StandardLocation.CLASS_OUTPUT, packageName, fileName);
        } catch (Throwable e) {
            resource = filer.createResource(StandardLocation.CLASS_OUTPUT, packageName, fileName);
        }
        URI uri = resource.toUri();
        File file = null;
        if (uri != null) {
            try {
                file = new File(uri.getPath());
            } catch (Exception e) {
                warning(processingEnv, "Cannot convert output directory resource URI to a file " + e);
            }
        }
        if (file == null) {
            warning(processingEnv, "No class output directory could be found!");
        } else {
            file.getParentFile().mkdirs();
            out = new FileWriter(file);
            writer = new PrintWriter(out);
            handler.call(writer);
        }
    } catch (IOException e) {
        log(processingEnv, e);
    } finally {
        if (writer != null) {
            writer.close();
        }
    }
}
Also used : FileWriter(java.io.FileWriter) FileObject(javax.tools.FileObject) IOException(java.io.IOException) Filer(javax.annotation.processing.Filer) URI(java.net.URI) File(java.io.File) PrintWriter(java.io.PrintWriter) StringWriter(java.io.StringWriter) FileWriter(java.io.FileWriter) Writer(java.io.Writer) IOException(java.io.IOException) PrintWriter(java.io.PrintWriter)

Example 55 with FileObject

use of javax.tools.FileObject in project druid by druid-io.

the class LocalDataSegmentPuller method buildFileObject.

public static FileObject buildFileObject(final URI uri) {
    final Path path = Paths.get(uri);
    final File file = path.toFile();
    return new FileObject() {

        @Override
        public URI toUri() {
            return uri;
        }

        @Override
        public String getName() {
            return path.getFileName().toString();
        }

        @Override
        public InputStream openInputStream() throws IOException {
            return new FileInputStream(file);
        }

        @Override
        public OutputStream openOutputStream() throws IOException {
            return new FileOutputStream(file);
        }

        @Override
        public Reader openReader(boolean ignoreEncodingErrors) throws IOException {
            return new FileReader(file);
        }

        @Override
        public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
            throw new UOE("CharSequence not supported");
        }

        @Override
        public Writer openWriter() throws IOException {
            return new FileWriter(file);
        }

        @Override
        public long getLastModified() {
            return file.lastModified();
        }

        @Override
        public boolean delete() {
            return file.delete();
        }
    };
}
Also used : Path(java.nio.file.Path) FileOutputStream(java.io.FileOutputStream) FileWriter(java.io.FileWriter) FileReader(java.io.FileReader) UOE(io.druid.java.util.common.UOE) FileObject(javax.tools.FileObject) File(java.io.File) FileInputStream(java.io.FileInputStream)

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