use of org.infinispan.protostream.config.AnnotationAttributeConfiguration in project protostream by infinispan.
the class AnnotatedDescriptorImpl method validateAttributes.
private void validateAttributes(AnnotationElement.Annotation annotation, AnnotationConfiguration annotationConfig) {
for (Map.Entry<String, AnnotationElement.Attribute> entry : annotation.getAttributes().entrySet()) {
AnnotationElement.Attribute attribute = entry.getValue();
final AnnotationAttributeConfiguration attributeConfig = annotationConfig.attributes().get(attribute.getName());
if (attributeConfig == null) {
throw new AnnotationParserException("Unexpected attribute '" + attribute.getName() + "' in annotation '" + annotation.getName() + "' on " + getFullName());
}
AnnotationElement.Value value = attribute.getValue();
if (!attributeConfig.multiple() && value instanceof AnnotationElement.Array) {
throw new AnnotationParserException("Annotation attribute '" + attribute.getName() + "' in annotation '" + annotation.getName() + "' on " + getFullName() + " does not accept array values");
}
if (value instanceof AnnotationElement.Array) {
for (AnnotationElement.Value v : ((AnnotationElement.Array) value).getValues()) {
validateAttribute(annotation, attribute, attributeConfig, v);
}
} else {
validateAttribute(annotation, attribute, attributeConfig, value);
}
}
}
use of org.infinispan.protostream.config.AnnotationAttributeConfiguration in project protostream by infinispan.
the class AnnotatedDescriptorImpl method normalizeValues.
private void normalizeValues(AnnotationElement.Annotation annotation, AnnotationConfiguration annotationConfig) {
for (AnnotationAttributeConfiguration attributeConfig : annotationConfig.attributes().values()) {
AnnotationElement.Attribute attribute = annotation.getAttributes().get(attributeConfig.name());
if (attribute != null) {
AnnotationElement.Value value = attribute.getValue();
if (attributeConfig.multiple() && !(value instanceof AnnotationElement.Array)) {
// a single value will be automatically wrapped in an array node if the attribute was defined as 'multiple'
value = new AnnotationElement.Array(value.position, Collections.singletonList(value));
attribute = new AnnotationElement.Attribute(attribute.position, attributeConfig.name(), value);
annotation.getAttributes().put(attributeConfig.name(), attribute);
}
} else if (attributeConfig.defaultValue() != null) {
AnnotationElement.Value value = attributeConfig.type() == AnnotationElement.AttributeType.IDENTIFIER ? new AnnotationElement.Identifier(AnnotationElement.UNKNOWN_POSITION, (String) attributeConfig.defaultValue()) : new AnnotationElement.Literal(AnnotationElement.UNKNOWN_POSITION, attributeConfig.defaultValue());
if (attributeConfig.multiple()) {
value = new AnnotationElement.Array(value.position, Collections.singletonList(value));
}
attribute = new AnnotationElement.Attribute(AnnotationElement.UNKNOWN_POSITION, attributeConfig.name(), value);
annotation.getAttributes().put(attributeConfig.name(), attribute);
} else {
throw new AnnotationParserException("Attribute '" + attributeConfig.name() + "' of annotation '" + annotation.getName() + "' on " + getFullName() + " is required");
}
}
}
Aggregations