Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package org.infinispan.protostream.impl;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.infinispan.protostream.AnnotationMetadataCreator;
import org.infinispan.protostream.AnnotationParserException;
import org.infinispan.protostream.DescriptorParserException;
import org.infinispan.protostream.config.AnnotationAttributeConfiguration;
import org.infinispan.protostream.config.AnnotationConfiguration;
import org.infinispan.protostream.config.Configuration;
import org.infinispan.protostream.descriptors.AnnotatedDescriptor;
import org.infinispan.protostream.descriptors.AnnotationElement;
import org.infinispan.protostream.impl.parser.AnnotationParser;
/**
* @author [email protected]
* @since 2.0
*/
public abstract class AnnotatedDescriptorImpl implements AnnotatedDescriptor {
private static final Log log = Log.LogFactory.getLog(AnnotatedDescriptorImpl.class);
protected final String name;
protected String fullName;
/**
* The (optional) documentation comment.
*/
protected final String documentation;
/**
* The annotations found in the documentation.
*/
protected Map annotations = null;
/**
* The annotation metadata objects created by the {@link org.infinispan.protostream.AnnotationMetadataCreator} based
* on the annotations found in the documentation text.
*/
protected Map processedAnnotations = null;
protected AnnotatedDescriptorImpl(String name, String fullName, String documentation) {
if (name.indexOf('.') != -1) {
throw new DescriptorParserException("Definition names must not be qualified : " + name);
}
this.name = name;
this.fullName = fullName;
this.documentation = documentation;
}
@Override
public final String getName() {
return name;
}
//todo [anistor] make a clear distinction between full field name and path
@Override
public final String getFullName() {
return fullName;
}
@Override
public final String getDocumentation() {
return documentation;
}
/**
* Extract annotations by parsing the documentation comment and run the configured {@link
* AnnotationMetadataCreator}s.
*
* @throws AnnotationParserException if annotation parsing fails
*/
private void processAnnotations() throws AnnotationParserException {
// we are lazily processing the annotations, if there is a documentation text attached to this element
if (annotations == null) {
if (documentation != null) {
AnnotationParser parser = new AnnotationParser(documentation, true);
List parsedAnnotations = parser.parse();
Map _annotations = new LinkedHashMap<>();
Map _containers = new LinkedHashMap<>();
for (AnnotationElement.Annotation annotation : parsedAnnotations) {
AnnotationConfiguration annotationConfig = getAnnotationConfig(annotation);
if (annotationConfig == null) {
// unknown annotations are ignored
if (getAnnotationsConfig().logUndefinedAnnotations()) {
log.debugf("Ignoring an unknown annotation \"%s\" on %s", annotation.getName(), fullName);
}
} else {
validateAttributes(annotation, annotationConfig);
// convert single values to arrays if needed and set the default values for missing attributes
normalizeValues(annotation, annotationConfig);
if (_annotations.containsKey(annotation.getName()) || _containers.containsKey(annotation.getName())) {
// did we just find a repeatable annotation?
if (annotationConfig.repeatable() != null) {
AnnotationElement.Annotation container = _containers.get(annotation.getName());
if (container == null) {
List values = new LinkedList<>();
values.add(_annotations.remove(annotation.getName()));
values.add(annotation);
AnnotationElement.Attribute value = new AnnotationElement.Attribute(annotation.position, AnnotationElement.Annotation.VALUE_DEFAULT_ATTRIBUTE, new AnnotationElement.Array(annotation.position, values));
container = new AnnotationElement.Annotation(annotation.position, annotationConfig.repeatable(), Collections.singletonMap(value.getName(), value));
_containers.put(annotation.getName(), container);
_annotations.put(container.getName(), container);
} else {
AnnotationElement.Array value = (AnnotationElement.Array) container.getAttributeValue(AnnotationElement.Annotation.VALUE_DEFAULT_ATTRIBUTE);
value.getValues().add(annotation);
}
} else {
// it's just a duplicate, not a proper 'repeated' annotation
throw new AnnotationParserException(String.format("Error: %s: duplicate annotation definition \"%s\" on %s",
AnnotationElement.positionToString(annotation.position), annotation.getName(), fullName));
}
} else {
_annotations.put(annotation.getName(), annotation);
}
}
}
// annotations are now completely parsed and validated
annotations = _annotations.isEmpty() ? Collections.emptyMap() : Collections.unmodifiableMap(_annotations);
// create metadata based on the annotations
processedAnnotations = new LinkedHashMap<>();
for (AnnotationElement.Annotation annotation : annotations.values()) {
AnnotationConfiguration annotationConfig = getAnnotationConfig(annotation);
AnnotationMetadataCreator