コード例 #1
0
ファイル: Class.java プロジェクト: justinsb/robovm
  /**
   * 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[] declaredAnnotations = getDeclaredAnnotations();

    for (int i = declaredAnnotations.length - 1; i >= 0; --i) {
      map.put(declaredAnnotations[i].annotationType(), declaredAnnotations[i]);
    }
    for (Class<?> sup = getSuperclass(); sup != null; sup = sup.getSuperclass()) {
      declaredAnnotations = sup.getDeclaredAnnotations();
      for (int i = declaredAnnotations.length - 1; i >= 0; --i) {
        Class<?> clazz = declaredAnnotations[i].annotationType();
        if (!map.containsKey(clazz) && clazz.isAnnotationPresent(Inherited.class)) {
          map.put(clazz, declaredAnnotations[i]);
        }
      }
    }

    /* convert annotation values from HashMap to array */
    Collection<Annotation> coll = map.values();
    return coll.toArray(new Annotation[coll.size()]);
  }