Search in sources :

Example 1 with XmlElementRef

use of javax.xml.bind.annotation.XmlElementRef in project camel by apache.

the class SpringAnnotationProcessor method findClassProperties.

protected void findClassProperties(ProcessingEnvironment processingEnv, PrintWriter writer, RoundEnvironment roundEnv, Set<EipOption> eipOptions, TypeElement originalClassType, TypeElement classElement, String prefix, String modelName) {
    while (true) {
        List<VariableElement> fieldElements = ElementFilter.fieldsIn(classElement.getEnclosedElements());
        for (VariableElement fieldElement : fieldElements) {
            String fieldName = fieldElement.getSimpleName().toString();
            XmlAttribute attribute = fieldElement.getAnnotation(XmlAttribute.class);
            if (attribute != null) {
                boolean skip = processAttribute(processingEnv, roundEnv, originalClassType, classElement, fieldElement, fieldName, attribute, eipOptions, prefix, modelName);
                if (skip) {
                    continue;
                }
            }
            XmlElements elements = fieldElement.getAnnotation(XmlElements.class);
            if (elements != null) {
                processElements(processingEnv, roundEnv, classElement, elements, fieldElement, eipOptions, prefix);
            }
            XmlElementRef elementRef = fieldElement.getAnnotation(XmlElementRef.class);
            if (elementRef != null) {
                processElement(processingEnv, roundEnv, classElement, null, elementRef, fieldElement, eipOptions, prefix);
            }
            XmlElement element = fieldElement.getAnnotation(XmlElement.class);
            if (element != null) {
                if ("rests".equals(fieldName)) {
                    processRests(roundEnv, classElement, element, fieldElement, fieldName, eipOptions, prefix);
                } else if ("routes".equals(fieldName)) {
                    processRoutes(roundEnv, classElement, element, fieldElement, fieldName, eipOptions, prefix);
                } else {
                    processElement(processingEnv, roundEnv, classElement, element, null, fieldElement, eipOptions, prefix);
                }
            }
        }
        // check super classes which may also have fields
        TypeElement baseTypeElement = null;
        TypeMirror superclass = classElement.getSuperclass();
        if (superclass != null) {
            String superClassName = canonicalClassName(superclass.toString());
            baseTypeElement = findTypeElement(processingEnv, roundEnv, superClassName);
        }
        if (baseTypeElement != null) {
            classElement = baseTypeElement;
        } else {
            break;
        }
    }
}
Also used : XmlElementRef(javax.xml.bind.annotation.XmlElementRef) XmlElements(javax.xml.bind.annotation.XmlElements) XmlAttribute(javax.xml.bind.annotation.XmlAttribute) TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) AnnotationProcessorHelper.findTypeElement(org.apache.camel.tools.apt.AnnotationProcessorHelper.findTypeElement) XmlElement(javax.xml.bind.annotation.XmlElement) VariableElement(javax.lang.model.element.VariableElement)

Example 2 with XmlElementRef

use of javax.xml.bind.annotation.XmlElementRef in project camel by apache.

the class CoreEipAnnotationProcessor method findClassProperties.

protected void findClassProperties(ProcessingEnvironment processingEnv, PrintWriter writer, RoundEnvironment roundEnv, Set<EipOption> eipOptions, TypeElement originalClassType, TypeElement classElement, String prefix, String modelName) {
    while (true) {
        List<VariableElement> fieldElements = ElementFilter.fieldsIn(classElement.getEnclosedElements());
        for (VariableElement fieldElement : fieldElements) {
            String fieldName = fieldElement.getSimpleName().toString();
            XmlAttribute attribute = fieldElement.getAnnotation(XmlAttribute.class);
            if (attribute != null) {
                boolean skip = processAttribute(processingEnv, roundEnv, originalClassType, classElement, fieldElement, fieldName, attribute, eipOptions, prefix, modelName);
                if (skip) {
                    continue;
                }
            }
            XmlValue value = fieldElement.getAnnotation(XmlValue.class);
            if (value != null) {
                processValue(processingEnv, roundEnv, originalClassType, classElement, fieldElement, fieldName, value, eipOptions, prefix, modelName);
            }
            XmlElements elements = fieldElement.getAnnotation(XmlElements.class);
            if (elements != null) {
                processElements(processingEnv, roundEnv, classElement, elements, fieldElement, eipOptions, prefix);
            }
            XmlElement element = fieldElement.getAnnotation(XmlElement.class);
            if (element != null) {
                processElement(processingEnv, roundEnv, classElement, element, fieldElement, eipOptions, prefix);
            }
            // special for eips which has outputs or requires an expressions
            XmlElementRef elementRef = fieldElement.getAnnotation(XmlElementRef.class);
            if (elementRef != null) {
                // special for routes
                processRoutes(roundEnv, originalClassType, elementRef, fieldElement, fieldName, eipOptions, prefix);
                // special for outputs
                processOutputs(processingEnv, roundEnv, originalClassType, elementRef, fieldElement, fieldName, eipOptions, prefix);
                // special for when clauses (choice eip)
                processRefWhenClauses(processingEnv, roundEnv, originalClassType, elementRef, fieldElement, fieldName, eipOptions, prefix);
                // special for rests (rest-dsl)
                processRests(roundEnv, originalClassType, elementRef, fieldElement, fieldName, eipOptions, prefix);
                // special for verbs (rest-dsl)
                processVerbs(processingEnv, roundEnv, originalClassType, elementRef, fieldElement, fieldName, eipOptions, prefix);
                // special for expression
                processRefExpression(processingEnv, roundEnv, originalClassType, classElement, elementRef, fieldElement, fieldName, eipOptions, prefix);
            }
        }
        // special when we process these nodes as they do not use JAXB annotations on fields, but on methods
        if ("OptionalIdentifiedDefinition".equals(classElement.getSimpleName().toString())) {
            processIdentified(processingEnv, roundEnv, originalClassType, classElement, eipOptions, prefix);
        } else if ("RouteDefinition".equals(classElement.getSimpleName().toString())) {
            processRoute(processingEnv, roundEnv, originalClassType, classElement, eipOptions, prefix);
        }
        // check super classes which may also have fields
        TypeElement baseTypeElement = null;
        TypeMirror superclass = classElement.getSuperclass();
        if (superclass != null) {
            String superClassName = canonicalClassName(superclass.toString());
            baseTypeElement = findTypeElement(processingEnv, roundEnv, superClassName);
        }
        if (baseTypeElement != null) {
            classElement = baseTypeElement;
        } else {
            break;
        }
    }
}
Also used : XmlElementRef(javax.xml.bind.annotation.XmlElementRef) XmlElements(javax.xml.bind.annotation.XmlElements) XmlAttribute(javax.xml.bind.annotation.XmlAttribute) TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) AnnotationProcessorHelper.findTypeElement(org.apache.camel.tools.apt.AnnotationProcessorHelper.findTypeElement) XmlValue(javax.xml.bind.annotation.XmlValue) XmlElement(javax.xml.bind.annotation.XmlElement) VariableElement(javax.lang.model.element.VariableElement)

Example 3 with XmlElementRef

use of javax.xml.bind.annotation.XmlElementRef in project zm-mailbox by Zimbra.

the class NameInfo method setWrappedInfo.

private void setWrappedInfo(AnnotationIntrospector ai, AnnotatedMember prop, String defaultWrappedName) {
    ZimbraUniqueElement uniqueElemAnnot = prop.getAnnotation(ZimbraUniqueElement.class);
    if (uniqueElemAnnot != null) {
        treatAsUniqueElement = uniqueElemAnnot.value();
    }
    ZimbraJsonArrayForWrapper arrayForWrapperAnnot = prop.getAnnotation(ZimbraJsonArrayForWrapper.class);
    if (arrayForWrapperAnnot != null) {
        wrapperIsArray = arrayForWrapperAnnot.value();
    }
    ZimbraKeyValuePairs kvpAnnot = prop.getAnnotation(ZimbraKeyValuePairs.class);
    if (kvpAnnot != null) {
        keyValuePairs = kvpAnnot.value();
        return;
    }
    ZimbraJsonAttribute jsonAttributeAnnot = prop.getAnnotation(ZimbraJsonAttribute.class);
    if (jsonAttributeAnnot != null) {
        treatAsAttribute = jsonAttributeAnnot.value();
    }
    mixedAllowed = (prop.getAnnotation(XmlMixed.class) != null);
    anyElementAllowed = (prop.getAnnotation(XmlAnyElement.class) != null);
    anyAttributeAllowed = (prop.getAnnotation(XmlAnyAttribute.class) != null);
    XmlElement elemAnnot = prop.getAnnotation(XmlElement.class);
    if (elemAnnot != null) {
        wrappedName = getQName(prop.getName(), elemAnnot.namespace(), elemAnnot.name());
        return;
    }
    XmlElementRef elemRefAnnot = prop.getAnnotation(XmlElementRef.class);
    wrappedName = getElementRefName(elemRefAnnot);
    if (wrappedName != null) {
        return;
    }
    XmlElements elemsAnnot = prop.getAnnotation(XmlElements.class);
    if (elemsAnnot != null) {
        XmlElement[] elems = elemsAnnot.value();
        wrappedNameMap = Maps.newHashMapWithExpectedSize(elems.length);
        for (XmlElement elem : elems) {
            QName qn = getQName(prop.getName(), elem.namespace(), elem.name());
            Class<?> kls = elem.type();
            getWrappedNameMap().put(kls, qn);
        }
        return;
    }
    XmlElementRefs elemRefsAnnot = prop.getAnnotation(XmlElementRefs.class);
    if (elemRefsAnnot != null) {
        XmlElementRef[] elems = elemRefsAnnot.value();
        wrappedNameMap = Maps.newHashMapWithExpectedSize(elems.length);
        for (XmlElementRef elem : elems) {
            QName qn = getElementRefName(elem);
            Class<?> kls = elem.type();
            getWrappedNameMap().put(kls, qn);
        }
        return;
    }
    if (wrapperName != null) {
        // We have a wrapper but nothing to tell us what the wrapped name should be, so use default
        wrappedName = new QName("", defaultWrappedName);
    }
}
Also used : XmlElementRef(javax.xml.bind.annotation.XmlElementRef) ZimbraKeyValuePairs(com.zimbra.soap.json.jackson.annotate.ZimbraKeyValuePairs) ZimbraJsonAttribute(com.zimbra.soap.json.jackson.annotate.ZimbraJsonAttribute) QName(javax.xml.namespace.QName) ZimbraJsonArrayForWrapper(com.zimbra.soap.json.jackson.annotate.ZimbraJsonArrayForWrapper) XmlAnyElement(javax.xml.bind.annotation.XmlAnyElement) XmlElementRefs(javax.xml.bind.annotation.XmlElementRefs) XmlMixed(javax.xml.bind.annotation.XmlMixed) XmlElements(javax.xml.bind.annotation.XmlElements) ZimbraUniqueElement(com.zimbra.soap.json.jackson.annotate.ZimbraUniqueElement) XmlAnyAttribute(javax.xml.bind.annotation.XmlAnyAttribute) XmlElement(javax.xml.bind.annotation.XmlElement)

Example 4 with XmlElementRef

use of javax.xml.bind.annotation.XmlElementRef in project camel by apache.

the class ModelSanityCheckerTest method testSanity.

public void testSanity() throws Exception {
    Set<Class<?>> classes = discoverJaxbClasses();
    assertNotNull(classes);
    assertTrue("There should be > 140 classes, was: " + classes.size(), classes.size() > 140);
    // check each class is okay
    for (Class<?> clazz : classes) {
        // skip ProcessorDefinition as its special
        if (clazz == ProcessorDefinition.class) {
            continue;
        }
        // skip RouteDefinition as its special
        if (clazz == RouteDefinition.class) {
            continue;
        }
        // check each declared field in the class
        for (Field field : clazz.getDeclaredFields()) {
            LOG.debug("Class {} has field {}", clazz.getName(), field.getName());
            // does the field have a jaxb annotation?
            boolean attribute = field.getAnnotation(XmlAttribute.class) != null;
            boolean element = field.getAnnotation(XmlElement.class) != null;
            boolean elementRef = field.getAnnotation(XmlElementRef.class) != null;
            // only one of those 3 is allowed, so check that we don't have 2+ of them
            if ((attribute && element) || (attribute && elementRef) || (element && elementRef)) {
                fail("Class " + clazz.getName() + " has field " + field.getName() + " which has 2+ annotations that are not allowed together.");
            }
            // check getter/setter
            if (attribute || element || elementRef) {
                // check for getter/setter
                Method getter = IntrospectionSupport.getPropertyGetter(clazz, field.getName());
                Method setter = IntrospectionSupport.getPropertySetter(clazz, field.getName());
                assertNotNull("Getter " + field.getName() + " on class " + clazz.getName() + " is missing", getter);
                assertNotNull("Setter " + field.getName() + " on class " + clazz.getName() + " is missing", setter);
            }
        }
        // we do not expect any JAXB annotations on methods
        for (Method method : clazz.getDeclaredMethods()) {
            LOG.debug("Class {} has method {}", clazz.getName(), method.getName());
            // special for OptionalIdentifiedDefinition as it has setter, so we should skip it
            if (clazz.getCanonicalName().equals(OptionalIdentifiedDefinition.class.getCanonicalName())) {
                continue;
            }
            // does the method have a jaxb annotation?
            boolean attribute = method.getAnnotation(XmlAttribute.class) != null;
            boolean element = method.getAnnotation(XmlElement.class) != null;
            boolean elementRef = method.getAnnotation(XmlElementRef.class) != null;
            assertFalse("Class " + clazz.getName() + " has method " + method.getName() + " should not have @XmlAttribute annotation", attribute);
            assertFalse("Class " + clazz.getName() + " has method " + method.getName() + " should not have @XmlElement annotation", element);
            assertFalse("Class " + clazz.getName() + " has method " + method.getName() + " should not have @XmlElementRef annotation", elementRef);
        }
    }
}
Also used : XmlElementRef(javax.xml.bind.annotation.XmlElementRef) Field(java.lang.reflect.Field) XmlAttribute(javax.xml.bind.annotation.XmlAttribute) XmlElement(javax.xml.bind.annotation.XmlElement) Method(java.lang.reflect.Method)

Example 5 with XmlElementRef

use of javax.xml.bind.annotation.XmlElementRef in project midpoint by Evolveum.

the class PrismBeanInspector method lookupSubstitutionUncached.

private Field lookupSubstitutionUncached(Class beanClass, Method elementMethodInObjectFactory) {
    XmlElementDecl xmlElementDecl = elementMethodInObjectFactory.getAnnotation(XmlElementDecl.class);
    if (xmlElementDecl == null) {
        return null;
    }
    final String substitutionHeadName = xmlElementDecl.substitutionHeadName();
    return findField(beanClass, field -> {
        XmlElementRef xmlElementRef = field.getAnnotation(XmlElementRef.class);
        return xmlElementRef != null && xmlElementRef.name().equals(substitutionHeadName);
    });
}
Also used : XmlElementRef(javax.xml.bind.annotation.XmlElementRef) XmlElementDecl(javax.xml.bind.annotation.XmlElementDecl)

Aggregations

XmlElementRef (javax.xml.bind.annotation.XmlElementRef)6 XmlElement (javax.xml.bind.annotation.XmlElement)5 XmlAttribute (javax.xml.bind.annotation.XmlAttribute)4 XmlElements (javax.xml.bind.annotation.XmlElements)4 TypeElement (javax.lang.model.element.TypeElement)2 VariableElement (javax.lang.model.element.VariableElement)2 TypeMirror (javax.lang.model.type.TypeMirror)2 XmlElementRefs (javax.xml.bind.annotation.XmlElementRefs)2 XmlValue (javax.xml.bind.annotation.XmlValue)2 AnnotationProcessorHelper.findTypeElement (org.apache.camel.tools.apt.AnnotationProcessorHelper.findTypeElement)2 ZimbraJsonArrayForWrapper (com.zimbra.soap.json.jackson.annotate.ZimbraJsonArrayForWrapper)1 ZimbraJsonAttribute (com.zimbra.soap.json.jackson.annotate.ZimbraJsonAttribute)1 ZimbraKeyValuePairs (com.zimbra.soap.json.jackson.annotate.ZimbraKeyValuePairs)1 ZimbraUniqueElement (com.zimbra.soap.json.jackson.annotate.ZimbraUniqueElement)1 Annotation (java.lang.annotation.Annotation)1 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 XmlAnyAttribute (javax.xml.bind.annotation.XmlAnyAttribute)1 XmlAnyElement (javax.xml.bind.annotation.XmlAnyElement)1 XmlElementDecl (javax.xml.bind.annotation.XmlElementDecl)1