use of com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper in project dhis2-core by dhis2.
the class Jackson2PropertyIntrospectorService method scanClass.
@Override
protected Map<String, Property> scanClass(Class<?> clazz) {
Map<String, Property> propertyMap = Maps.newHashMap();
Map<String, Property> hibernatePropertyMap = getPropertiesFromHibernate(clazz);
List<String> classFieldNames = ReflectionUtils.getAllFieldNames(clazz);
// TODO this is quite nasty, should find a better way of exposing properties at class-level
if (AnnotationUtils.isAnnotationPresent(clazz, JacksonXmlRootElement.class)) {
Property property = new Property();
JacksonXmlRootElement jacksonXmlRootElement = AnnotationUtils.getAnnotation(clazz, JacksonXmlRootElement.class);
if (!StringUtils.isEmpty(jacksonXmlRootElement.localName())) {
property.setName(jacksonXmlRootElement.localName());
}
if (!StringUtils.isEmpty(jacksonXmlRootElement.namespace())) {
property.setNamespace(jacksonXmlRootElement.namespace());
}
propertyMap.put("__self__", property);
}
List<Property> properties = collectProperties(clazz);
for (Property property : properties) {
Method getterMethod = property.getGetterMethod();
JsonProperty jsonProperty = AnnotationUtils.getAnnotation(getterMethod, JsonProperty.class);
String fieldName = getFieldName(getterMethod);
property.setName(!StringUtils.isEmpty(jsonProperty.value()) ? jsonProperty.value() : fieldName);
if (property.getGetterMethod() != null) {
property.setReadable(true);
}
if (property.getSetterMethod() != null) {
property.setWritable(true);
}
if (classFieldNames.contains(fieldName)) {
property.setFieldName(fieldName);
}
if (hibernatePropertyMap.containsKey(fieldName)) {
Property hibernateProperty = hibernatePropertyMap.get(fieldName);
property.setPersisted(true);
property.setWritable(true);
property.setUnique(hibernateProperty.isUnique());
property.setRequired(hibernateProperty.isRequired());
property.setLength(hibernateProperty.getLength());
property.setMax(hibernateProperty.getMax());
property.setMin(hibernateProperty.getMin());
property.setCollection(hibernateProperty.isCollection());
property.setCascade(hibernateProperty.getCascade());
property.setOwner(hibernateProperty.isOwner());
property.setManyToMany(hibernateProperty.isManyToMany());
property.setOneToOne(hibernateProperty.isOneToOne());
property.setManyToOne(hibernateProperty.isManyToOne());
property.setOwningRole(hibernateProperty.getOwningRole());
property.setInverseRole(hibernateProperty.getInverseRole());
property.setGetterMethod(hibernateProperty.getGetterMethod());
property.setSetterMethod(hibernateProperty.getSetterMethod());
}
if (AnnotationUtils.isAnnotationPresent(property.getGetterMethod(), Description.class)) {
Description description = AnnotationUtils.getAnnotation(property.getGetterMethod(), Description.class);
property.setDescription(description.value());
}
if (AnnotationUtils.isAnnotationPresent(property.getGetterMethod(), JacksonXmlProperty.class)) {
JacksonXmlProperty jacksonXmlProperty = AnnotationUtils.getAnnotation(getterMethod, JacksonXmlProperty.class);
if (StringUtils.isEmpty(jacksonXmlProperty.localName())) {
property.setName(property.getName());
} else {
property.setName(jacksonXmlProperty.localName());
}
if (!StringUtils.isEmpty(jacksonXmlProperty.namespace())) {
property.setNamespace(jacksonXmlProperty.namespace());
}
property.setAttribute(jacksonXmlProperty.isAttribute());
}
Class<?> returnType = property.getGetterMethod().getReturnType();
property.setKlass(Primitives.wrap(returnType));
if (Collection.class.isAssignableFrom(returnType)) {
property.setCollection(true);
property.setCollectionName(property.getName());
property.setOrdered(List.class.isAssignableFrom(returnType));
Type type = property.getGetterMethod().getGenericReturnType();
if (ParameterizedType.class.isInstance(type)) {
ParameterizedType parameterizedType = (ParameterizedType) type;
Class<?> klass = (Class<?>) parameterizedType.getActualTypeArguments()[0];
property.setItemKlass(Primitives.wrap(klass));
if (collectProperties(klass).isEmpty()) {
property.setSimple(true);
}
property.setIdentifiableObject(IdentifiableObject.class.isAssignableFrom(klass));
property.setNameableObject(NameableObject.class.isAssignableFrom(klass));
property.setEmbeddedObject(EmbeddedObject.class.isAssignableFrom(klass));
}
} else {
if (collectProperties(returnType).isEmpty()) {
property.setSimple(true);
}
}
if (property.isCollection()) {
if (AnnotationUtils.isAnnotationPresent(property.getGetterMethod(), JacksonXmlElementWrapper.class)) {
JacksonXmlElementWrapper jacksonXmlElementWrapper = AnnotationUtils.getAnnotation(getterMethod, JacksonXmlElementWrapper.class);
property.setCollectionWrapping(jacksonXmlElementWrapper.useWrapping());
// TODO what if element-wrapper have different namespace?
if (!StringUtils.isEmpty(jacksonXmlElementWrapper.localName())) {
property.setCollectionName(jacksonXmlElementWrapper.localName());
}
}
propertyMap.put(property.getCollectionName(), property);
} else {
propertyMap.put(property.getName(), property);
}
if (Enum.class.isAssignableFrom(property.getKlass())) {
Object[] enumConstants = property.getKlass().getEnumConstants();
List<String> enumValues = new ArrayList<>();
for (Object value : enumConstants) {
enumValues.add(value.toString());
}
property.setConstants(enumValues);
}
SchemaUtils.updatePropertyTypes(property);
}
return propertyMap;
}
Aggregations