cn.gitlab.virtualcry.sapjco.spring.util.AnnotationUtils Maven / Gradle / Ivy
package cn.gitlab.virtualcry.sapjco.spring.util;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.util.MultiValueMap;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
/**
* AnnotationUtils
*/
public class AnnotationUtils extends AnnotatedElementUtils {
public static Set attributesForRepeatable(AnnotationMetadata metadata, Class> containerClass, Class> annotationClass) {
return attributesForRepeatable(metadata, containerClass.getName(), annotationClass.getName());
}
@SuppressWarnings("unchecked")
private static Set attributesForRepeatable(AnnotationMetadata metadata, String containerClassName, String annotationClassName) {
Set result = new LinkedHashSet<>();
MultiValueMap tmp = metadata.getAllAnnotationAttributes(annotationClassName, false);
// Direct annotation present?
addAttributesIfNotNull(result, metadata.getAnnotationAttributes(annotationClassName, false));
// Container annotation present?
Map container = metadata.getAnnotationAttributes(containerClassName, false);
if (container != null && container.containsKey("value")) {
for (Map containedAttributes : (Map[]) container.get("value")) {
addAttributesIfNotNull(result, containedAttributes);
}
}
// Return merged result
return Collections.unmodifiableSet(result);
}
private static void addAttributesIfNotNull(Set result, Map attributes) {
if (attributes != null) {
result.add(AnnotationAttributes.fromMap(attributes));
}
}
}