/**
* Returns all the annotations of this class. If there are no annotations
* then an empty array is returned.
*
* @return a copy of the array containing this class' annotations.
* @see #getDeclaredAnnotations()
*/
public Annotation[] getAnnotations() {
/*
* We need to get the annotations declared on this class, plus the
* annotations from superclasses that have the "@Inherited" annotation
* set. We create a temporary map to use while we accumulate the
* annotations and convert it to an array at the end.
*
* It's possible to have duplicates when annotations are inherited.
* We use a Map to filter those out.
*
* HashMap might be overkill here.
*/
HashMap<Class, Annotation> map = new HashMap<Class, Annotation>();
Annotation[] annos = getDeclaredAnnotations();
for (int i = annos.length-1; i >= 0; --i)
map.put(annos[i].annotationType(), annos[i]);
for (Class sup = getSuperclass(); sup != null;
sup = sup.getSuperclass()) {
annos = sup.getDeclaredAnnotations();
for (int i = annos.length-1; i >= 0; --i) {
Class clazz = annos[i].annotationType();
if (!map.containsKey(clazz) &&
clazz.isAnnotationPresent(Inherited.class)) {
map.put(clazz, annos[i]);
}
}
}
/* convert annotation values from HashMap to array */
Collection<Annotation> coll = map.values();
return coll.toArray(new Annotation[coll.size()]);
}
Class.java 文件源码
java
阅读 34
收藏 0
点赞 0
评论 0
项目:In-the-Box-Fork
作者:
评论列表
文章目录