本文整理汇总了Java中java.lang.reflect.AnnotatedElement.getDeclaredAnnotations方法的典型用法代码示例。如果您正苦于以下问题:Java AnnotatedElement.getDeclaredAnnotations方法的具体用法?Java AnnotatedElement.getDeclaredAnnotations怎么用?Java AnnotatedElement.getDeclaredAnnotations使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.lang.reflect.AnnotatedElement
的用法示例。
在下文中一共展示了AnnotatedElement.getDeclaredAnnotations方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getTesterAnnotations
import java.lang.reflect.AnnotatedElement; //导入方法依赖的package包/类
/**
* Find all the tester annotations declared on a tester class or method.
* @param classOrMethod a class or method whose tester annotations to find
* @return an iterable sequence of tester annotations on the class
*/
public static Iterable<Annotation> getTesterAnnotations(AnnotatedElement classOrMethod) {
synchronized (annotationCache) {
List<Annotation> annotations = annotationCache.get(classOrMethod);
if (annotations == null) {
annotations = new ArrayList<Annotation>();
for (Annotation a : classOrMethod.getDeclaredAnnotations()) {
if (a.annotationType().isAnnotationPresent(TesterAnnotation.class)) {
annotations.add(a);
}
}
annotations = Collections.unmodifiableList(annotations);
annotationCache.put(classOrMethod, annotations);
}
return annotations;
}
}
示例2: findAnnotation
import java.lang.reflect.AnnotatedElement; //导入方法依赖的package包/类
/**
* The default implementation performs a simple search for a declared annotation matching the search type.
* Spring provides a more sophisticated annotation search utility that matches on meta-annotations as well.
*
* @param annotatedElement The element to search.
* @param annotationType The annotation type class.
* @param <A> Annotation type to search for.
* @return
*/
@SuppressWarnings("unchecked")
default <A extends Annotation> A findAnnotation(AnnotatedElement annotatedElement, Class<A> annotationType) {
Annotation[] anns = annotatedElement.getDeclaredAnnotations();
for (Annotation ann : anns) {
if (ann.annotationType() == annotationType) {
return (A) ann;
}
}
return null;
}