本文整理匯總了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;
}