Search in sources :

Example 11 with FilerException

use of javax.annotation.processing.FilerException 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 12 with FilerException

use of javax.annotation.processing.FilerException in project service-proxy by membrane.

the class BlueprintParsers method writeParserDefinitior.

public void writeParserDefinitior(Model m) throws IOException {
    for (MainInfo main : m.getMains()) {
        List<Element> sources = new ArrayList<Element>();
        sources.addAll(main.getInterceptorElements());
        sources.add(main.getElement());
        try {
            FileObject o = processingEnv.getFiler().createSourceFile(main.getAnnotation().outputPackage() + ".blueprint" + ".BlueprintNamespaceParser", sources.toArray(new Element[0]));
            BufferedWriter bw = new BufferedWriter(o.openWriter());
            try {
                bw.write("/* Copyright 2014 predic8 GmbH, www.predic8.com\r\n" + "\r\n" + "   Licensed under the Apache License, Version 2.0 (the \"License\");\r\n" + "   you may not use this file except in compliance with the License.\r\n" + "   You may obtain a copy of the License at\r\n" + "\r\n" + "   http://www.apache.org/licenses/LICENSE-2.0\r\n" + "\r\n" + "   Unless required by applicable law or agreed to in writing, software\r\n" + "   distributed under the License is distributed on an \"AS IS\" BASIS,\r\n" + "   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r\n" + "   See the License for the specific language governing permissions and\r\n" + "   limitations under the License. */\r\n" + "\r\n" + "package " + main.getAnnotation().outputPackage() + ".blueprint;\r\n" + "\r\n" + "/**\r\n" + "  * Automatically generated by " + BlueprintParsers.class.getName() + ".\r\n" + "  */\r\n" + "public class BlueprintNamespaceParser extends com.predic8.membrane.annot.parser.BlueprintNamespaceParser {\r\n" + "\r\n" + "	public void init() {\r\n");
                for (ElementInfo i : main.getIis()) {
                    if (i.getAnnotation().topLevel()) {
                        bw.write("		registerGlobalBeanDefinitionParser(\"" + i.getAnnotation().name() + "\", new " + i.getParserClassSimpleName() + "());\r\n");
                    } else {
                        for (ChildElementDeclarationInfo cedi : i.getUsedBy()) {
                            for (ChildElementInfo cei : cedi.getUsedBy()) {
                                TypeElement element = cei.getEi().getElement();
                                String clazz = AnnotUtils.getRuntimeClassName(element);
                                bw.write("		registerLocalBeanDefinitionParser(\"" + clazz + "\", \"" + i.getAnnotation().name() + "\", new " + i.getParserClassSimpleName() + "());\r\n");
                            }
                        }
                    }
                }
                bw.write("	}\r\n" + "}\r\n" + "");
            } finally {
                bw.close();
            }
        } catch (FilerException e) {
            if (e.getMessage().contains("Source file already created"))
                return;
            throw e;
        }
    }
}
Also used : ElementInfo(com.predic8.membrane.annot.model.ElementInfo) ChildElementInfo(com.predic8.membrane.annot.model.ChildElementInfo) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) ArrayList(java.util.ArrayList) ChildElementInfo(com.predic8.membrane.annot.model.ChildElementInfo) BufferedWriter(java.io.BufferedWriter) MainInfo(com.predic8.membrane.annot.model.MainInfo) FileObject(javax.tools.FileObject) FilerException(javax.annotation.processing.FilerException) ChildElementDeclarationInfo(com.predic8.membrane.annot.model.ChildElementDeclarationInfo)

Example 13 with FilerException

use of javax.annotation.processing.FilerException in project service-proxy by membrane.

the class Parsers method writeParserDefinitior.

public void writeParserDefinitior(Model m) throws IOException {
    for (MainInfo main : m.getMains()) {
        List<Element> sources = new ArrayList<Element>();
        sources.addAll(main.getInterceptorElements());
        sources.add(main.getElement());
        try {
            FileObject o = processingEnv.getFiler().createSourceFile(main.getAnnotation().outputPackage() + ".NamespaceHandlerAutoGenerated", sources.toArray(new Element[0]));
            BufferedWriter bw = new BufferedWriter(o.openWriter());
            try {
                bw.write("/* Copyright 2012,2013 predic8 GmbH, www.predic8.com\r\n" + "\r\n" + "   Licensed under the Apache License, Version 2.0 (the \"License\");\r\n" + "   you may not use this file except in compliance with the License.\r\n" + "   You may obtain a copy of the License at\r\n" + "\r\n" + "   http://www.apache.org/licenses/LICENSE-2.0\r\n" + "\r\n" + "   Unless required by applicable law or agreed to in writing, software\r\n" + "   distributed under the License is distributed on an \"AS IS\" BASIS,\r\n" + "   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r\n" + "   See the License for the specific language governing permissions and\r\n" + "   limitations under the License. */\r\n" + "\r\n" + "package " + main.getAnnotation().outputPackage() + ";\r\n" + "\r\n" + "/**\r\n" + "  * Automatically generated by " + Parsers.class.getName() + ".\r\n" + "  */\r\n" + "public class NamespaceHandlerAutoGenerated {\r\n" + "\r\n" + "	public static void registerBeanDefinitionParsers(NamespaceHandler nh) {\r\n");
                for (ElementInfo i : main.getIis()) {
                    if (i.getAnnotation().topLevel()) {
                        bw.write("		nh.registerGlobalBeanDefinitionParser(\"" + i.getAnnotation().name() + "\", new " + i.getParserClassSimpleName() + "());\r\n");
                    } else {
                        for (ChildElementDeclarationInfo cedi : i.getUsedBy()) {
                            for (ChildElementInfo cei : cedi.getUsedBy()) {
                                TypeElement element = cei.getEi().getElement();
                                String clazz = AnnotUtils.getRuntimeClassName(element);
                                bw.write("		nh.registerLocalBeanDefinitionParser(\"" + clazz + "\", \"" + i.getAnnotation().name() + "\", new " + i.getParserClassSimpleName() + "());\r\n");
                            }
                        }
                    }
                }
                bw.write("	}\r\n" + "}\r\n" + "");
            } finally {
                bw.close();
            }
        } catch (FilerException e) {
            if (e.getMessage().contains("Source file already created"))
                return;
            throw e;
        }
    }
}
Also used : ElementInfo(com.predic8.membrane.annot.model.ElementInfo) ChildElementInfo(com.predic8.membrane.annot.model.ChildElementInfo) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) ArrayList(java.util.ArrayList) ChildElementInfo(com.predic8.membrane.annot.model.ChildElementInfo) BufferedWriter(java.io.BufferedWriter) MainInfo(com.predic8.membrane.annot.model.MainInfo) FileObject(javax.tools.FileObject) FilerException(javax.annotation.processing.FilerException) ChildElementDeclarationInfo(com.predic8.membrane.annot.model.ChildElementDeclarationInfo)

Example 14 with FilerException

use of javax.annotation.processing.FilerException in project service-proxy by membrane.

the class Schemas method writeXSD.

public void writeXSD(Model m) throws IOException {
    try {
        for (MainInfo main : m.getMains()) {
            List<Element> sources = new ArrayList<Element>();
            sources.add(main.getElement());
            sources.addAll(main.getInterceptorElements());
            FileObject o = processingEnv.getFiler().createResource(StandardLocation.CLASS_OUTPUT, main.getAnnotation().outputPackage(), main.getAnnotation().outputName(), sources.toArray(new Element[0]));
            BufferedWriter bw = new BufferedWriter(o.openWriter());
            try {
                assembleXSD(bw, m, main);
            } finally {
                bw.close();
            }
        }
    } catch (FilerException e) {
        if (e.getMessage().contains("Source file already created"))
            return;
        throw e;
    }
}
Also used : MainInfo(com.predic8.membrane.annot.model.MainInfo) Element(javax.lang.model.element.Element) ArrayList(java.util.ArrayList) FileObject(javax.tools.FileObject) FilerException(javax.annotation.processing.FilerException) BufferedWriter(java.io.BufferedWriter)

Example 15 with FilerException

use of javax.annotation.processing.FilerException in project androidannotations by androidannotations.

the class SourceCodeWriter method openBinary.

@Override
public OutputStream openBinary(JPackage pkg, String fileName) throws IOException {
    String qualifiedClassName = toQualifiedClassName(pkg, fileName);
    LOGGER.debug("Generating class: {}", qualifiedClassName);
    Element[] classOriginatingElements = originatingElements.getClassOriginatingElements(qualifiedClassName);
    try {
        JavaFileObject sourceFile;
        if (classOriginatingElements.length == 0) {
            LOGGER.info("Generating class with no originating element: {}", qualifiedClassName);
        }
        sourceFile = filer.createSourceFile(qualifiedClassName, classOriginatingElements);
        return sourceFile.openOutputStream();
    } catch (FilerException e) {
        LOGGER.error("Could not generate source file for {} due to error: {}", qualifiedClassName, e.getMessage());
        /*
			 * This exception is expected, when some files are created twice. We cannot
			 * delete existing files, unless using a dirty hack. Files a created twice when
			 * the same file is created from different annotation rounds. Happens when
			 * renaming classes, and for Background executor. It also probably means I
			 * didn't fully understand how annotation processing works. If anyone can point
			 * me out...
			 */
        return VOID_OUTPUT_STREAM;
    }
}
Also used : JavaFileObject(javax.tools.JavaFileObject) Element(javax.lang.model.element.Element) FilerException(javax.annotation.processing.FilerException)

Aggregations

FilerException (javax.annotation.processing.FilerException)15 Element (javax.lang.model.element.Element)12 TypeElement (javax.lang.model.element.TypeElement)11 ArrayList (java.util.ArrayList)8 IOException (java.io.IOException)7 FileObject (javax.tools.FileObject)7 MainInfo (com.predic8.membrane.annot.model.MainInfo)5 BufferedWriter (java.io.BufferedWriter)5 ExecutableElement (javax.lang.model.element.ExecutableElement)5 ChildElementInfo (com.predic8.membrane.annot.model.ChildElementInfo)4 ElementInfo (com.predic8.membrane.annot.model.ElementInfo)4 ClassName (com.squareup.javapoet.ClassName)3 CodeBlock (com.squareup.javapoet.CodeBlock)3 JavaFile (com.squareup.javapoet.JavaFile)3 MethodSpec (com.squareup.javapoet.MethodSpec)3 TypeSpec (com.squareup.javapoet.TypeSpec)3 VariableElement (javax.lang.model.element.VariableElement)3 AttributeInfo (com.predic8.membrane.annot.model.AttributeInfo)2 ChildElementDeclarationInfo (com.predic8.membrane.annot.model.ChildElementDeclarationInfo)2 OutputStream (java.io.OutputStream)2