use of com.att.cdp.openstack.heat.model.Constraint in project AJSC by att.
the class Unmarshaller method determineImplementationClass.
/**
* This method checks the specified bean property to locate the implementation class to be used. If the
* implementation class supports polymorphic mappings, then the implementation class is further inspected for
* JsonTypeInfo annotations. If the annotation is present, then the annotation directs the processing. Because the
* processing of the annotation may be data-dependent, the mapped data to be unmarshalled must be provided as well.
*
* @param beanProperty
* The bean property we are mapping the contents into
* @param obj
* The object that contains the mappings
* @return The class to be constructed to contain the mappings
* @throws UnmarshallException
* If the json type info uses unsupported metadata determination
*/
@SuppressWarnings("unchecked")
private Class<? extends ModelObject> determineImplementationClass(BeanProperty beanProperty, Object obj) throws UnmarshallException {
Class<? extends ModelObject> implClass = null;
JsonTypeInfo typeInfo = null;
JsonSubTypes subTypes = null;
Class<?> defaultImplClass = null;
/*
* First, try to determine the implementation class to be created based on the bean property type. If the bean
* property is a scalar type, then use it as is. If it is a list, then use the first generic type. If it is a
* map, use the second generic type (maps are always assumed to be keyed by strings).
*/
implClass = (Class<? extends ModelObject>) beanProperty.getPropertyType();
if (beanProperty.isList()) {
implClass = (Class<? extends ModelObject>) beanProperty.getCollectionTypes()[0];
} else if (beanProperty.isMap()) {
implClass = (Class<? extends ModelObject>) beanProperty.getCollectionTypes()[1];
}
/*
* HACK: If the implClass is Constraint, then we have a special case. We need to examine the first key in the
* map to determine the type of constraint and return that name.
*/
if (implClass.equals(Constraint.class)) {
if (obj instanceof List) {
Map<String, Object> map = (Map<String, Object>) ((List<Object>) obj).get(0);
String constraintType = (String) map.keySet().toArray()[0];
for (int index = 0; index < CONSTRAINT_TYPES.length; index++) {
if (CONSTRAINT_TYPES[index].equals(constraintType)) {
implClass = CONSTRAINT_CLASSES[index];
return implClass;
}
}
}
}
/*
* If typeInfo annotation is present on the property type class, then check it to get the actual implementation
* class. Otherwise, we will instantiate the property defined.
*/
typeInfo = (JsonTypeInfo) ObjectHelper.getClassAnnotation(implClass, JsonTypeInfo.class);
if (typeInfo != null) {
subTypes = (JsonSubTypes) ObjectHelper.getClassAnnotation(implClass, JsonSubTypes.class);
defaultImplClass = typeInfo.defaultImpl();
JsonTypeInfo.Id use = typeInfo.use();
JsonTypeInfo.As include = typeInfo.include();
if (use.equals(JsonTypeInfo.Id.NAME) && include.equals(JsonTypeInfo.As.PROPERTY)) {
if (obj instanceof Map) {
Map<String, Object> map = (Map<String, Object>) obj;
String property = typeInfo.property();
String propertyValue = (String) map.get(property);
implClass = (Class<? extends ModelObject>) defaultImplClass;
if (propertyValue != null) {
JsonSubTypes.Type[] types = subTypes.value();
for (JsonSubTypes.Type type : types) {
if (type.name().equals(propertyValue)) {
implClass = (Class<? extends ModelObject>) type.value();
break;
}
}
}
}
} else if (use.equals(JsonTypeInfo.Id.CUSTOM)) {
JsonDeserialize deserializeAnnotation = (JsonDeserialize) ObjectHelper.getClassAnnotation(implClass, JsonDeserialize.class);
Class<? extends JsonDeserializer> deserializer = deserializeAnnotation.using();
} else {
throw new UnmarshallException(String.format("Only JsonTypeInfo use=\"NAME\" and include=\"PROPERTY\" " + " or use=\"CUSTOM\" with custom deserializer are supported. The mapping specified " + "use=\"%s\" and include=\"%s\"", use.name(), include.name()));
}
}
return implClass;
}
Aggregations