Search in sources :

Example 6 with ElementInfo

use of com.predic8.membrane.annot.model.ElementInfo in project service-proxy by membrane.

the class SpringConfigurationXSDGeneratingAnnotationProcessor method scan.

private void scan(Model m, MainInfo main, ElementInfo ii, TypeElement te) {
    TypeMirror superclass = te.getSuperclass();
    if (superclass instanceof DeclaredType)
        scan(m, main, ii, (TypeElement) ((DeclaredType) superclass).asElement());
    for (Element e2 : te.getEnclosedElements()) {
        MCAttribute a = e2.getAnnotation(MCAttribute.class);
        if (a != null) {
            AttributeInfo ai = new AttributeInfo();
            ai.setAnnotation(a);
            ai.setE((ExecutableElement) e2);
            ai.setRequired(isRequired(e2));
            ii.getAis().add(ai);
            ii.setHasIdField(ii.isHasIdField() || ai.getXMLName().equals("id"));
        }
        MCOtherAttributes d = e2.getAnnotation(MCOtherAttributes.class);
        if (d != null) {
            OtherAttributesInfo oai = new OtherAttributesInfo();
            oai.setOtherAttributesSetter((ExecutableElement) e2);
            ii.setOai(oai);
        }
        MCChildElement b = e2.getAnnotation(MCChildElement.class);
        if (b != null) {
            ChildElementInfo cei = new ChildElementInfo();
            cei.setEi(ii);
            cei.setAnnotation(b);
            cei.setE((ExecutableElement) e2);
            List<? extends VariableElement> parameters = cei.getE().getParameters();
            if (parameters.size() == 0)
                throw new ProcessingException("Setter must have exactly one parameter.", e2);
            TypeMirror setterArgType = parameters.get(0).asType();
            if (!(setterArgType instanceof DeclaredType))
                throw new ProcessingException("Setter argument must be of an @MCElement-annotated type.", parameters.get(0));
            cei.setTypeDeclaration((TypeElement) ((DeclaredType) setterArgType).asElement());
            cei.setPropertyName(AnnotUtils.dejavaify(e2.getSimpleName().toString().substring(3)));
            cei.setRequired(isRequired(e2));
            ii.getCeis().add(cei);
            // unwrap "java.util.List<?>" and "java.util.Collection<?>"
            if (cei.getTypeDeclaration().getQualifiedName().toString().startsWith("java.util.List") || cei.getTypeDeclaration().getQualifiedName().toString().startsWith("java.util.Collection")) {
                cei.setTypeDeclaration((TypeElement) ((DeclaredType) ((DeclaredType) setterArgType).getTypeArguments().get(0)).asElement());
                cei.setList(true);
            }
            ChildElementDeclarationInfo cedi;
            if (!main.getChildElementDeclarations().containsKey(cei.getTypeDeclaration())) {
                cedi = new ChildElementDeclarationInfo();
                cedi.setTarget(cei.getTypeDeclaration());
                cedi.setRaiseErrorWhenNoSpecimen(!cei.getAnnotation().allowForeign());
                main.getChildElementDeclarations().put(cei.getTypeDeclaration(), cedi);
            } else {
                cedi = main.getChildElementDeclarations().get(cei.getTypeDeclaration());
                cedi.setRaiseErrorWhenNoSpecimen(cedi.isRaiseErrorWhenNoSpecimen() || !cei.getAnnotation().allowForeign());
            }
            cedi.addUsedBy(cei);
        }
        MCTextContent c = e2.getAnnotation(MCTextContent.class);
        if (c != null) {
            TextContentInfo tci = new TextContentInfo();
            tci.setPropertyName(AnnotUtils.dejavaify(e2.getSimpleName().toString().substring(3)));
            ii.setTci(tci);
        }
    }
    HashSet<Integer> childOrders = new HashSet<Integer>();
    for (ChildElementInfo cei : ii.getCeis()) {
        if (!childOrders.add(cei.getAnnotation().order()))
            throw new ProcessingException("@MCChildElement(order=...) must be unique.", cei.getE());
    }
    Collections.sort(ii.getCeis());
}
Also used : OtherAttributesInfo(com.predic8.membrane.annot.model.OtherAttributesInfo) TextContentInfo(com.predic8.membrane.annot.model.TextContentInfo) ChildElementInfo(com.predic8.membrane.annot.model.ChildElementInfo) AttributeInfo(com.predic8.membrane.annot.model.AttributeInfo) TypeMirror(javax.lang.model.type.TypeMirror) ChildElementDeclarationInfo(com.predic8.membrane.annot.model.ChildElementDeclarationInfo) DeclaredType(javax.lang.model.type.DeclaredType)

Example 7 with ElementInfo

use of com.predic8.membrane.annot.model.ElementInfo 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 8 with ElementInfo

use of com.predic8.membrane.annot.model.ElementInfo in project service-proxy by membrane.

the class HelpReference method handle.

/*
	private int getId(String xsdTypeName) {
		if (ids.containsKey(xsdTypeName))
			return ids.get(xsdTypeName);
		int id = Math.abs(xsdTypeName.hashCode());
		if (idsReverse.containsKey(id))
			throw new ProcessingException("ID-assigning algorithm failed (two XSD types got the same ID)");
		ids.put(xsdTypeName, id);
		idsReverse.put(id, xsdTypeName);
		return id;
	}
	 */
private void handle(Model m, MainInfo main, ChildElementInfo cei) throws XMLStreamException {
    xew.writeStartElement("child");
    xew.writeAttribute("min", cei.isRequired() ? "1" : "0");
    xew.writeAttribute("max", cei.isList() ? "unbounded" : "1");
    handleDoc(cei);
    SortedSet<String> possibilities = new TreeSet<String>();
    for (ElementInfo ei : main.getChildElementDeclarations().get(cei.getTypeDeclaration()).getElementInfo()) {
        possibilities.add(ei.getId());
    }
    for (String id : possibilities) {
        xew.writeStartElement("possibility");
        xew.writeAttribute("refId", id);
        xew.writeEndElement();
    }
    if (cei.getAnnotation().allowForeign()) {
        xew.writeStartElement("possibility");
        xew.writeAttribute("foreign", "true");
        xew.writeEndElement();
    }
    xew.writeEndElement();
}
Also used : ElementInfo(com.predic8.membrane.annot.model.ElementInfo) ChildElementInfo(com.predic8.membrane.annot.model.ChildElementInfo) TreeSet(java.util.TreeSet)

Example 9 with ElementInfo

use of com.predic8.membrane.annot.model.ElementInfo in project service-proxy by membrane.

the class HelpReference method handle.

private void handle(Model m, MainInfo main) throws XMLStreamException {
    xew.writeStartElement("namespace");
    xew.writeAttribute("package", main.getAnnotation().outputPackage());
    xew.writeAttribute("targetNamespace", main.getAnnotation().targetNamespace());
    Collections.sort(main.getIis(), new Comparator<ElementInfo>() {

        @Override
        public int compare(ElementInfo o1, ElementInfo o2) {
            int res = o1.getAnnotation().name().compareTo(o2.getAnnotation().name());
            if (res == 0)
                res = o1.getElement().getQualifiedName().toString().compareTo(o2.getElement().getQualifiedName().toString());
            return res;
        }
    });
    for (ElementInfo ei : main.getIis()) handle(m, main, ei);
    xew.writeEndElement();
}
Also used : ElementInfo(com.predic8.membrane.annot.model.ElementInfo) ChildElementInfo(com.predic8.membrane.annot.model.ChildElementInfo)

Example 10 with ElementInfo

use of com.predic8.membrane.annot.model.ElementInfo 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)

Aggregations

ChildElementInfo (com.predic8.membrane.annot.model.ChildElementInfo)11 ElementInfo (com.predic8.membrane.annot.model.ElementInfo)9 AttributeInfo (com.predic8.membrane.annot.model.AttributeInfo)5 ChildElementDeclarationInfo (com.predic8.membrane.annot.model.ChildElementDeclarationInfo)5 MainInfo (com.predic8.membrane.annot.model.MainInfo)5 TypeElement (javax.lang.model.element.TypeElement)5 BufferedWriter (java.io.BufferedWriter)4 ArrayList (java.util.ArrayList)4 FilerException (javax.annotation.processing.FilerException)4 Element (javax.lang.model.element.Element)4 FileObject (javax.tools.FileObject)4 OtherAttributesInfo (com.predic8.membrane.annot.model.OtherAttributesInfo)2 Model (com.predic8.membrane.annot.model.Model)1 TextContentInfo (com.predic8.membrane.annot.model.TextContentInfo)1 IOException (java.io.IOException)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 TreeSet (java.util.TreeSet)1 DeclaredType (javax.lang.model.type.DeclaredType)1 TypeMirror (javax.lang.model.type.TypeMirror)1