Search in sources :

Example 21 with FileObject

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

the class MatchProcessor method getLog.

/**
 * Logging facility for debugging the annotation processor.
 */
private PrintWriter getLog() {
    if (log == null) {
        try {
            // Create the log file within the generated source directory so it's easy to find.
            // /tmp isn't platform independent and java.io.tmpdir can map anywhere, particularly
            // on the mac.
            FileObject file = processingEnv.getFiler().createResource(StandardLocation.SOURCE_OUTPUT, "", getClass().getSimpleName() + "log");
            log = new PrintWriter(new FileWriter(file.toUri().getPath(), true));
        } catch (IOException e) {
        // Do nothing
        }
    }
    return log;
}
Also used : FileWriter(java.io.FileWriter) FileObject(javax.tools.FileObject) JavaFileObject(javax.tools.JavaFileObject) IOException(java.io.IOException) PrintWriter(java.io.PrintWriter)

Example 22 with FileObject

use of javax.tools.FileObject in project syndesis by syndesisio.

the class ActionProcessor method obtainResourceFile.

// *****************************************
// Helpers
// *****************************************
/**
 * Helper method to produce class output text file using the given handler
 */
@SuppressFBWarnings("RV_RETURN_VALUE_IGNORED_BAD_PRACTICE")
private File obtainResourceFile(Element element) throws IOException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
    TypeElement classElement;
    if (element instanceof TypeElement) {
        classElement = (TypeElement) element;
    } else if (element instanceof ExecutableElement) {
        classElement = (TypeElement) element.getEnclosingElement();
    } else {
        warning("Unsupported element kind: " + element.getKind());
        return null;
    }
    final String javaTypeName = canonicalClassName(classElement.getQualifiedName().toString());
    final String packageName = javaTypeName.substring(0, javaTypeName.lastIndexOf('.'));
    final Annotation annotation = element.getAnnotation(annotationClass);
    if (annotation == null) {
        error("Annotation SyndesisExtensionAction not found processing element " + element);
    }
    final String actionId = (String) annotationClass.getMethod("id").invoke(annotation);
    final String fileName = new StringBuilder().append(classElement.getSimpleName().toString()).append('-').append(Names.sanitize(actionId)).append(".json").toString();
    File result = null;
    Filer filer = processingEnv.getFiler();
    FileObject resource;
    try {
        resource = filer.getResource(StandardLocation.SOURCE_OUTPUT, packageName, fileName);
    } catch (Exception e) {
        resource = filer.createResource(StandardLocation.SOURCE_OUTPUT, packageName, fileName);
    }
    URI uri = resource.toUri();
    if (uri != null) {
        try {
            result = new File(uri.getPath());
        } catch (Exception e) {
            warning("Cannot convert output directory resource URI to a file " + e);
        }
    }
    if (result == null) {
        warning("No class output directory could be found!");
    } else {
        result.getParentFile().mkdirs();
    }
    return result;
}
Also used : TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) FileObject(javax.tools.FileObject) File(java.io.File) Filer(javax.annotation.processing.Filer) URI(java.net.URI) Annotation(java.lang.annotation.Annotation) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 23 with FileObject

use of javax.tools.FileObject in project fabric8 by fabric8io.

the class AbstractKubernetesAnnotationProcessor method getFileObject.

private FileObject getFileObject(String fileName) throws IOException {
    FileObject fileObject = processingEnv.getFiler().getResource(StandardLocation.CLASS_OUTPUT, "", fileName);
    Path path = Paths.get(fileObject.toUri());
    File file = path.toFile();
    if (file.exists() && !file.delete()) {
        throw new IOException("Failed to delete old kubernetes json file: " + fileName);
    }
    fileObject = processingEnv.getFiler().createResource(StandardLocation.CLASS_OUTPUT, "", fileName);
    return fileObject;
}
Also used : Path(java.nio.file.Path) FileObject(javax.tools.FileObject) IOException(java.io.IOException) File(java.io.File)

Example 24 with FileObject

use of javax.tools.FileObject in project mule by mulesoft.

the class AnnotationProcessorResourceGenerator method write.

@Override
protected void write(GeneratedResource resource) {
    FileObject file;
    try {
        file = processingEnv.getFiler().createResource(SOURCE_OUTPUT, EMPTY, resource.getPath());
    } catch (IOException e) {
        throw wrapException(e, resource);
    }
    try (OutputStream out = file.openOutputStream()) {
        out.write(resource.getContent());
        out.flush();
    } catch (IOException e) {
        throw wrapException(e, resource);
    }
}
Also used : OutputStream(java.io.OutputStream) FileObject(javax.tools.FileObject) IOException(java.io.IOException)

Example 25 with FileObject

use of javax.tools.FileObject in project mule by mulesoft.

the class AnnotationProcessorResourceGeneratorTestCase method write.

@Test
public void write() throws Exception {
    FileObject file = mock(FileObject.class);
    when(processingEnvironment.getFiler().createResource(SOURCE_OUTPUT, EMPTY, RESOURCE_PATH)).thenReturn(file);
    OutputStream out = mock(OutputStream.class, RETURNS_DEEP_STUBS);
    when(file.openOutputStream()).thenReturn(out);
    generator.generateFor(extensionModel);
    verify(out).write(RESOURCE_CONTENT);
    verify(out).flush();
}
Also used : OutputStream(java.io.OutputStream) FileObject(javax.tools.FileObject) Test(org.junit.Test) SmallTest(org.mule.tck.size.SmallTest)

Aggregations

FileObject (javax.tools.FileObject)91 IOException (java.io.IOException)57 TypeElement (javax.lang.model.element.TypeElement)19 File (java.io.File)18 PrintWriter (java.io.PrintWriter)16 Element (javax.lang.model.element.Element)14 Writer (java.io.Writer)13 Filer (javax.annotation.processing.Filer)13 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 Properties (java.util.Properties)10 InputStream (java.io.InputStream)8 URI (java.net.URI)8 FilerException (javax.annotation.processing.FilerException)7 MainInfo (com.predic8.membrane.annot.model.MainInfo)6 BufferedReader (java.io.BufferedReader)6 FileWriter (java.io.FileWriter)6