Search in sources :

Example 11 with XmlAttribute

use of javax.xml.bind.annotation.XmlAttribute 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 12 with XmlAttribute

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

the class PrismBeanInspector method findPropertyGetterUncached.

private <T> Method findPropertyGetterUncached(Class<T> classType, String propName) {
    if (propName.startsWith("_")) {
        propName = propName.substring(1);
    }
    for (Method method : classType.getDeclaredMethods()) {
        XmlElement xmlElement = method.getAnnotation(XmlElement.class);
        if (xmlElement != null && xmlElement.name().equals(propName)) {
            return method;
        }
        XmlAttribute xmlAttribute = method.getAnnotation(XmlAttribute.class);
        if (xmlAttribute != null && xmlAttribute.name().equals(propName)) {
            return method;
        }
    }
    String getterName = "get" + StringUtils.capitalize(propName);
    try {
        return classType.getDeclaredMethod(getterName);
    } catch (NoSuchMethodException e) {
    // nothing found
    }
    getterName = "is" + StringUtils.capitalize(propName);
    try {
        return classType.getDeclaredMethod(getterName);
    } catch (NoSuchMethodException e) {
    // nothing found
    }
    Class<? super T> superclass = classType.getSuperclass();
    if (superclass == null || superclass.equals(Object.class)) {
        return null;
    }
    return findPropertyGetter(superclass, propName);
}
Also used : XmlAttribute(javax.xml.bind.annotation.XmlAttribute) XmlElement(javax.xml.bind.annotation.XmlElement)

Example 13 with XmlAttribute

use of javax.xml.bind.annotation.XmlAttribute in project winery by eclipse.

the class FieldValidator method setDeclaredFields.

private void setDeclaredFields(Class base, Class parent) {
    if (!this.declaredFields.containsKey(base)) {
        this.declaredFields.put(base, new HashSet<>());
    }
    if (parent.equals(TArtifactDefinition.class)) {
        this.declaredFields.get(base).add("file");
    }
    if (!parent.equals(Object.class)) {
        this.declaredFields.get(base).addAll(Arrays.stream(parent.getDeclaredFields()).map(field -> {
            XmlAttribute xmlAttribute = field.getAnnotation(XmlAttribute.class);
            XmlElement xmlElement = field.getAnnotation(XmlElement.class);
            if (Objects.nonNull(xmlAttribute) && !xmlAttribute.name().equals("##default")) {
                return xmlAttribute.name();
            } else if (Objects.nonNull(xmlElement) && !xmlElement.name().equals("##default")) {
                return xmlElement.name();
            } else {
                return field.getName();
            }
        }).collect(Collectors.toList()));
        setDeclaredFields(base, parent.getSuperclass());
    }
}
Also used : XmlAttribute(javax.xml.bind.annotation.XmlAttribute) XmlElement(javax.xml.bind.annotation.XmlElement)

Example 14 with XmlAttribute

use of javax.xml.bind.annotation.XmlAttribute in project siesta by cadenzauk.

the class FieldUtilTest method annotationNotPresent.

@Test
void annotationNotPresent() {
    Field field = ClassUtil.getDeclaredField(ClassWithStringField.class, "stringField");
    Optional<XmlAttribute> result = FieldUtil.annotation(XmlAttribute.class, field);
    assertThat(result, is(Optional.empty()));
}
Also used : Field(java.lang.reflect.Field) XmlAttribute(javax.xml.bind.annotation.XmlAttribute) Test(org.junit.jupiter.api.Test)

Example 15 with XmlAttribute

use of javax.xml.bind.annotation.XmlAttribute in project groovity by disney.

the class ModelXmlWriter method visitObjectField.

public void visitObjectField(String name, Object value) throws Exception {
    if (inAttribute) {
        writer.write(" ");
        writer.write(name);
        writer.write("=\"");
        super.visitObjectField(name, value);
        writer.write("\"");
        return;
    }
    boolean writeTag = true;
    Object currentObject = getCurrentObject();
    MetaClass mc = GroovySystem.getMetaClassRegistry().getMetaClass(currentObject.getClass());
    MetaProperty mp = mc.hasProperty(currentObject, name);
    XmlAttribute xa = getAnnotation(mp, XmlAttribute.class);
    if (xa != null) {
        // attributes are written with open tag
        return;
    }
    String listElementName = null;
    if (value instanceof List || (value != null && value.getClass().isArray()) || (mp != null && (mp.getType().isArray() || List.class.isAssignableFrom(mp.getType())))) {
        writeTag = false;
        listElementName = name;
        Map<Class<?>, String> listTypedNames = NO_NAMES;
        XmlElementWrapper xew = getAnnotation(mp, XmlElementWrapper.class);
        if (xew != null) {
            writeTag = true;
            if (!"##default".equals(xew.name())) {
                name = xew.name();
            }
            name = getTagName(xew.namespace(), name);
        }
        XmlElement xe = getAnnotation(mp, XmlElement.class);
        if (xe != null) {
            if (!"##default".equals(xe.name())) {
                listElementName = xe.name();
            }
            listElementName = getTagName(xe.namespace(), listElementName);
        }
        if (xe == null) {
            XmlElements xes = getAnnotation(mp, XmlElements.class);
            if (xes != null) {
                listTypedNames = new HashMap<>();
                for (int i = 0; i < xes.value().length; i++) {
                    XmlElement e = xes.value()[i];
                    listTypedNames.put(e.type(), e.name());
                }
            }
        }
        XmlList xel = getAnnotation(mp, XmlList.class);
        if (xel != null) {
            writeTag = true;
            name = listElementName;
            value = transformField(mp, value);
        }
        XmlMixed xm = getAnnotation(mp, XmlMixed.class);
        if (xm != null) {
            listTypedNames = SKIP_TAG;
        }
        listElementNames.push(listElementName);
        listTypedElementNames.push(listTypedNames);
    } else {
        XmlElement xe = getAnnotation(mp, XmlElement.class);
        if (xe != null) {
            if (!"##default".equals(xe.name())) {
                name = xe.name();
            }
            name = getTagName(xe.namespace(), name);
        }
    }
    doDelimit = true;
    if (writeTag) {
        final String n = name;
        writeTag(name, value, o -> {
            try {
                super.visitObjectField(n, o);
            } catch (RuntimeException e) {
                throw e;
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        });
    } else {
        super.visitObjectField(name, value);
    }
    if (listElementName != null) {
        listElementNames.pop();
        listTypedElementNames.pop();
    }
}
Also used : XmlAttribute(javax.xml.bind.annotation.XmlAttribute) XmlMixed(javax.xml.bind.annotation.XmlMixed) IOException(java.io.IOException) XmlList(javax.xml.bind.annotation.XmlList) XmlElements(javax.xml.bind.annotation.XmlElements) MetaClass(groovy.lang.MetaClass) XmlElement(javax.xml.bind.annotation.XmlElement) ArrayList(java.util.ArrayList) List(java.util.List) XmlList(javax.xml.bind.annotation.XmlList) MetaClass(groovy.lang.MetaClass) MetaProperty(groovy.lang.MetaProperty) XmlElementWrapper(javax.xml.bind.annotation.XmlElementWrapper)

Aggregations

XmlAttribute (javax.xml.bind.annotation.XmlAttribute)20 XmlElement (javax.xml.bind.annotation.XmlElement)12 Field (java.lang.reflect.Field)4 XmlElementRef (javax.xml.bind.annotation.XmlElementRef)4 XmlElementWrapper (javax.xml.bind.annotation.XmlElementWrapper)4 XmlElements (javax.xml.bind.annotation.XmlElements)4 Method (java.lang.reflect.Method)3 ArrayList (java.util.ArrayList)3 XmlValue (javax.xml.bind.annotation.XmlValue)3 MetaProperty (groovy.lang.MetaProperty)2 Xml (io.swagger.models.Xml)2 Member (java.lang.reflect.Member)2 PrivilegedActionException (java.security.PrivilegedActionException)2 TypeElement (javax.lang.model.element.TypeElement)2 VariableElement (javax.lang.model.element.VariableElement)2 TypeMirror (javax.lang.model.type.TypeMirror)2 JAXBException (javax.xml.bind.JAXBException)2 XmlAccessType (javax.xml.bind.annotation.XmlAccessType)2 XmlAccessorOrder (javax.xml.bind.annotation.XmlAccessorOrder)2 XmlType (javax.xml.bind.annotation.XmlType)2