Search in sources :

Example 61 with FileObject

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

the class InstrumentRegistrationProcessor method generateFile.

private void generateFile(List<TypeElement> instruments) {
    String filename = "META-INF/truffle/instrument";
    Properties p = new SortedProperties();
    int numInstruments = loadIfFileAlreadyExists(filename, p);
    for (TypeElement l : instruments) {
        Registration annotation = l.getAnnotation(Registration.class);
        if (annotation == null) {
            continue;
        }
        int instNum = findInstrument(annotation.id(), p);
        if (instNum == 0) {
            // not found
            numInstruments += 1;
            instNum = numInstruments;
        }
        String prefix = "instrument" + instNum + ".";
        String className = processingEnv.getElementUtils().getBinaryName(l).toString();
        p.setProperty(prefix + "id", annotation.id());
        p.setProperty(prefix + "name", annotation.name());
        p.setProperty(prefix + "version", annotation.version());
        p.setProperty(prefix + "className", className);
        p.setProperty(prefix + "internal", Boolean.toString(annotation.internal()));
        int serviceCounter = 0;
        for (AnnotationMirror anno : l.getAnnotationMirrors()) {
            final String annoName = anno.getAnnotationType().asElement().toString();
            if (Registration.class.getCanonicalName().equals(annoName)) {
                for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry : anno.getElementValues().entrySet()) {
                    final Name attrName = entry.getKey().getSimpleName();
                    if (attrName.contentEquals("services")) {
                        AnnotationValue attrValue = entry.getValue();
                        List<?> classes = (List<?>) attrValue.getValue();
                        for (Object clazz : classes) {
                            AnnotationValue clazzValue = (AnnotationValue) clazz;
                            p.setProperty(prefix + "service" + serviceCounter++, clazzValue.getValue().toString());
                        }
                    }
                }
            }
        }
    }
    if (numInstruments > 0) {
        try {
            FileObject file = processingEnv.getFiler().createResource(StandardLocation.CLASS_OUTPUT, "", filename);
            try (OutputStream os = file.openOutputStream()) {
                p.store(os, "Generated by " + InstrumentRegistrationProcessor.class.getName());
            }
        } catch (IOException e) {
            processingEnv.getMessager().printMessage(Kind.ERROR, e.getMessage(), instruments.get(0));
        }
    }
}
Also used : TypeElement(javax.lang.model.element.TypeElement) OutputStream(java.io.OutputStream) IOException(java.io.IOException) Properties(java.util.Properties) SortedProperties(com.oracle.truffle.dsl.processor.LanguageRegistrationProcessor.SortedProperties) SortedProperties(com.oracle.truffle.dsl.processor.LanguageRegistrationProcessor.SortedProperties) Name(javax.lang.model.element.Name) AnnotationMirror(javax.lang.model.element.AnnotationMirror) Registration(com.oracle.truffle.api.instrumentation.TruffleInstrument.Registration) AnnotationValue(javax.lang.model.element.AnnotationValue) ArrayList(java.util.ArrayList) List(java.util.List) FileObject(javax.tools.FileObject) FileObject(javax.tools.FileObject) Map(java.util.Map)

Example 62 with FileObject

use of javax.tools.FileObject in project service-proxy by membrane.

the class SpringConfigurationXSDGeneratingAnnotationProcessor method read.

@SuppressWarnings("unchecked")
private void read() {
    if (cache != null)
        return;
    cache = new HashMap<Class<? extends Annotation>, HashSet<Element>>();
    try {
        FileObject o = processingEnv.getFiler().getResource(StandardLocation.CLASS_OUTPUT, "", "META-INF/membrane.cache");
        BufferedReader r = new BufferedReader(o.openReader(false));
        try {
            if (!CACHE_FILE_FORMAT_VERSION.equals(r.readLine()))
                return;
            HashSet<Element> currentSet = null;
            Class<? extends Annotation> annotationClass = null;
            while (true) {
                String line = r.readLine();
                if (line == null)
                    break;
                if (line.startsWith(" ")) {
                    line = line.substring(1);
                    TypeElement element = null;
                    try {
                        element = processingEnv.getElementUtils().getTypeElement(line);
                    } catch (RuntimeException e) {
                    // do nothing (Eclipse)
                    }
                    if (element != null) {
                        if (element.getAnnotation(annotationClass) != null)
                            currentSet.add(element);
                    }
                } else {
                    try {
                        annotationClass = (Class<? extends Annotation>) getClass().getClassLoader().loadClass(line);
                    } catch (ClassNotFoundException e) {
                        throw new RuntimeException(e);
                    }
                    currentSet = new HashSet<Element>();
                    cache.put(annotationClass, currentSet);
                }
            }
        } finally {
            r.close();
        }
    } catch (FileNotFoundException e) {
    // do nothing (Maven)
    } catch (IOException e) {
    // do nothing (Eclipse)
    }
    for (Set<Element> e : cache.values()) {
        String status = "read " + e.size();
        log(status);
    }
}
Also used : FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) Annotation(java.lang.annotation.Annotation) BufferedReader(java.io.BufferedReader) FileObject(javax.tools.FileObject)

Example 63 with FileObject

use of javax.tools.FileObject 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 64 with FileObject

use of javax.tools.FileObject 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 65 with FileObject

use of javax.tools.FileObject 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)

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