AnnotationUtils.java 文件源码

java
阅读 35 收藏 0 点赞 0 评论 0

项目:holon-core 作者:
/**
 * Get all the annotations of given <code>annotationType</code> present in given annotations list, including any
 * meta-annotation and supporting repeatable annotations.
 * @param <A> Annotation type
 * @param annotations Annotation list element to inspect
 * @param annotationType Annotation type to lookup
 * @return List of detected annotation of given <code>annotationType</code>, an empty List if none found
 */
@SuppressWarnings("unchecked")
public static <A extends Annotation> List<A> getAnnotations(List<Annotation> annotations, Class<A> annotationType) {
    ObjectUtils.argumentNotNull(annotationType, "Annotation type must be not null");

    if (annotations == null || annotations.isEmpty()) {
        return Collections.emptyList();
    }

    Class<? extends Annotation> repeatableContainerType = null;
    if (annotationType.isAnnotationPresent(Repeatable.class)) {
        repeatableContainerType = annotationType.getAnnotation(Repeatable.class).value();
    }

    List<A> ans = new LinkedList<>();
    for (Annotation annotation : annotations) {
        if (annotationType.equals(annotation.annotationType())) {
            ans.add((A) annotation);
        }
        if (!isInJavaLangAnnotationPackage(annotation) && !annotation.annotationType().equals(annotationType)
                && (repeatableContainerType == null
                        || !annotation.annotationType().equals(repeatableContainerType))) {
            findAnnotations(ans, annotation.annotationType(), annotationType, repeatableContainerType);
        }
    }
    return ans;
}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号