Search in sources :

Example 1 with MCOtherAttributes

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

the class MCUtil method clone.

@SuppressWarnings("unchecked")
public static <T> T clone(T object, boolean deep) {
    try {
        if (object == null)
            throw new InvalidParameterException("'object' must not be null.");
        Class<? extends Object> clazz = object.getClass();
        MCElement e = clazz.getAnnotation(MCElement.class);
        if (e == null)
            throw new IllegalArgumentException("'object' must be @MCElement-annotated.");
        BeanWrapperImpl dst = new BeanWrapperImpl(clazz);
        BeanWrapperImpl src = new BeanWrapperImpl(object);
        for (Method m : clazz.getMethods()) {
            if (!m.getName().startsWith("set"))
                continue;
            String propertyName = AnnotUtils.dejavaify(m.getName().substring(3));
            MCAttribute a = m.getAnnotation(MCAttribute.class);
            if (a != null) {
                dst.setPropertyValue(propertyName, src.getPropertyValue(propertyName));
            }
            MCChildElement c = m.getAnnotation(MCChildElement.class);
            if (c != null) {
                if (deep) {
                    dst.setPropertyValue(propertyName, cloneInternal(src.getPropertyValue(propertyName), deep));
                } else {
                    dst.setPropertyValue(propertyName, src.getPropertyValue(propertyName));
                }
            }
            MCOtherAttributes o = m.getAnnotation(MCOtherAttributes.class);
            if (o != null) {
                dst.setPropertyValue(propertyName, src.getPropertyValue(propertyName));
            }
            MCTextContent t = m.getAnnotation(MCTextContent.class);
            if (t != null) {
                dst.setPropertyValue(propertyName, src.getPropertyValue(propertyName));
            }
        }
        return (T) dst.getRootInstance();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : MCChildElement(com.predic8.membrane.annot.MCChildElement) MCTextContent(com.predic8.membrane.annot.MCTextContent) BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) Method(java.lang.reflect.Method) InvalidParameterException(java.security.InvalidParameterException) XMLStreamException(javax.xml.stream.XMLStreamException) IOException(java.io.IOException) InvalidParameterException(java.security.InvalidParameterException) MCOtherAttributes(com.predic8.membrane.annot.MCOtherAttributes) MCElement(com.predic8.membrane.annot.MCElement) MCAttribute(com.predic8.membrane.annot.MCAttribute)

Example 2 with MCOtherAttributes

use of com.predic8.membrane.annot.MCOtherAttributes 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 3 with MCOtherAttributes

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

the class MCUtil method addXML.

private static void addXML(Object object, String id, XMLStreamWriter xew, SerializationContext sc) throws XMLStreamException {
    if (object == null)
        throw new InvalidParameterException("'object' must not be null.");
    Class<? extends Object> clazz = object.getClass();
    MCElement e = clazz.getAnnotation(MCElement.class);
    if (e == null)
        throw new IllegalArgumentException("'object' must be @MCElement-annotated.");
    BeanWrapperImpl src = new BeanWrapperImpl(object);
    xew.writeStartElement(e.name());
    if (id != null)
        xew.writeAttribute("id", id);
    HashSet<String> attributes = new HashSet<String>();
    for (Method m : clazz.getMethods()) {
        if (!m.getName().startsWith("set"))
            continue;
        String propertyName = AnnotUtils.dejavaify(m.getName().substring(3));
        MCAttribute a = m.getAnnotation(MCAttribute.class);
        if (a != null) {
            Object value = src.getPropertyValue(propertyName);
            String str;
            if (value == null)
                continue;
            else if (value instanceof String)
                str = (String) value;
            else if (value instanceof Boolean)
                str = ((Boolean) value).toString();
            else if (value instanceof Integer)
                str = ((Integer) value).toString();
            else if (value instanceof Long)
                str = ((Long) value).toString();
            else if (value instanceof Enum<?>)
                str = value.toString();
            else {
                MCElement el = value.getClass().getAnnotation(MCElement.class);
                if (el != null) {
                    str = defineBean(sc, value, null, true);
                } else {
                    str = "?";
                    sc.incomplete = true;
                }
            }
            if (a.attributeName().length() > 0)
                propertyName = a.attributeName();
            attributes.add(propertyName);
            xew.writeAttribute(propertyName, str);
        }
    }
    for (Method m : clazz.getMethods()) {
        if (!m.getName().startsWith("set"))
            continue;
        String propertyName = AnnotUtils.dejavaify(m.getName().substring(3));
        MCOtherAttributes o = m.getAnnotation(MCOtherAttributes.class);
        if (o != null) {
            Object value = src.getPropertyValue(propertyName);
            if (value instanceof Map<?, ?>) {
                Map<?, ?> map = (Map<?, ?>) value;
                for (Map.Entry<?, ?> entry : map.entrySet()) {
                    Object key = entry.getKey();
                    Object val = entry.getValue();
                    if (!(key instanceof String) || !(val instanceof String)) {
                        sc.incomplete = true;
                        key = "incompleteAttributes";
                        val = "?";
                    }
                    if (attributes.contains(key))
                        continue;
                    attributes.add((String) key);
                    xew.writeAttribute((String) key, (String) val);
                }
            } else {
                xew.writeAttribute("incompleteAttributes", "?");
                sc.incomplete = true;
            }
        }
    }
    List<Method> childElements = new ArrayList<Method>();
    for (Method m : clazz.getMethods()) {
        if (!m.getName().startsWith("set"))
            continue;
        String propertyName = AnnotUtils.dejavaify(m.getName().substring(3));
        MCChildElement c = m.getAnnotation(MCChildElement.class);
        if (c != null) {
            childElements.add(m);
        }
        MCTextContent t = m.getAnnotation(MCTextContent.class);
        if (t != null) {
            Object value = src.getPropertyValue(propertyName);
            if (value == null) {
                continue;
            } else if (value instanceof String) {
                xew.writeCharacters((String) value);
            } else {
                xew.writeCharacters("?");
                sc.incomplete = true;
            }
        }
    }
    Collections.sort(childElements, new Comparator<Method>() {

        @Override
        public int compare(Method o1, Method o2) {
            MCChildElement c1 = o1.getAnnotation(MCChildElement.class);
            MCChildElement c2 = o2.getAnnotation(MCChildElement.class);
            return c1.order() - c2.order();
        }
    });
    for (Method m : childElements) {
        String propertyName = AnnotUtils.dejavaify(m.getName().substring(3));
        Object value = src.getPropertyValue(propertyName);
        if (value != null) {
            if (value instanceof Collection<?>) {
                for (Object item : (Collection<?>) value) addXML(item, null, xew, sc);
            } else {
                addXML(value, null, xew, sc);
            }
        }
    }
    xew.writeEndElement();
}
Also used : MCChildElement(com.predic8.membrane.annot.MCChildElement) BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) ArrayList(java.util.ArrayList) InvalidParameterException(java.security.InvalidParameterException) MCElement(com.predic8.membrane.annot.MCElement) MCAttribute(com.predic8.membrane.annot.MCAttribute) HashSet(java.util.HashSet) MCTextContent(com.predic8.membrane.annot.MCTextContent) Method(java.lang.reflect.Method) MCOtherAttributes(com.predic8.membrane.annot.MCOtherAttributes) Collection(java.util.Collection) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

MCAttribute (com.predic8.membrane.annot.MCAttribute)2 MCChildElement (com.predic8.membrane.annot.MCChildElement)2 MCElement (com.predic8.membrane.annot.MCElement)2 MCOtherAttributes (com.predic8.membrane.annot.MCOtherAttributes)2 MCTextContent (com.predic8.membrane.annot.MCTextContent)2 Method (java.lang.reflect.Method)2 InvalidParameterException (java.security.InvalidParameterException)2 BeanWrapperImpl (org.springframework.beans.BeanWrapperImpl)2 AttributeInfo (com.predic8.membrane.annot.model.AttributeInfo)1 ChildElementDeclarationInfo (com.predic8.membrane.annot.model.ChildElementDeclarationInfo)1 ChildElementInfo (com.predic8.membrane.annot.model.ChildElementInfo)1 OtherAttributesInfo (com.predic8.membrane.annot.model.OtherAttributesInfo)1 TextContentInfo (com.predic8.membrane.annot.model.TextContentInfo)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 DeclaredType (javax.lang.model.type.DeclaredType)1