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);
}
}
}
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);
}
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());
}
}
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()));
}
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();
}
}
Aggregations