Java Reflection Annotation getAnnotations(AnnotatedElement annotated)

Here you can find the source of getAnnotations(AnnotatedElement annotated)

Description

Returns all annotations present on this element.

License

Open Source License

Parameter

Parameter Description
annotated - the AnnotatedElement

Return

this element's annotation for the specified annotation type

Declaration

public static Annotation[] getAnnotations(AnnotatedElement annotated) 

Method Source Code

//package com.java2s;
/**// ww w  . ja va2  s.com
 * Syncnapsis Framework - Copyright (c) 2012-2014 ultimate
 * This program is free software; you can redistribute it and/or modify it under the terms of
 * the GNU General Public License as published by the Free Software Foundation; either version
 * 3 of the License, or any later version.
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MECHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 * You should have received a copy of the GNU General Plublic License along with this program;
 * if not, see <http://www.gnu.org/licenses/>.
 */

import java.lang.annotation.Annotation;

import java.lang.reflect.AnnotatedElement;

import java.lang.reflect.Method;

import java.util.Arrays;

import java.util.LinkedList;
import java.util.List;

public class Main {
    /**
     * Returns all annotations present on this element. (Returns an array of length zero if this
     * element has no annotations.) The caller of this method is free to modify the returned array;
     * it will have no effect on the arrays returned to other callers. Also includes inherited
     * Annotations from Super-Classes or Interfaces.
     * 
     * @see AnnotatedElement#getAnnotations()
     * @param annotated - the AnnotatedElement
     * @return this element's annotation for the specified annotation type
     */
    public static Annotation[] getAnnotations(AnnotatedElement annotated) {
        List<Annotation> results = new LinkedList<Annotation>();

        results.addAll(Arrays.asList(annotated.getAnnotations()));
        if (annotated instanceof Class && !Object.class.equals(annotated)) {
            results.addAll(Arrays.asList(getAnnotations(((Class<?>) annotated).getSuperclass())));

            for (Class<?> i : ((Class<?>) annotated).getInterfaces()) {
                results.addAll(Arrays.asList(getAnnotations(i)));
            }
        } else if (annotated instanceof Method && !Object.class.equals(((Method) annotated).getDeclaringClass())) {
            Class<?> superClass;
            Method superMethod;

            superClass = ((Method) annotated).getDeclaringClass().getSuperclass();
            if (superClass != null) {
                try {
                    superMethod = superClass.getMethod(((Method) annotated).getName(),
                            ((Method) annotated).getParameterTypes());
                    results.addAll(Arrays.asList(getAnnotations(superMethod)));
                } catch (NoSuchMethodException e) {
                    // ignore
                }
            }

            for (Class<?> i : ((Method) annotated).getDeclaringClass().getInterfaces()) {
                try {
                    superMethod = i.getMethod(((Method) annotated).getName(),
                            ((Method) annotated).getParameterTypes());
                    results.addAll(Arrays.asList(getAnnotations(superMethod)));
                } catch (NoSuchMethodException e) {
                    // ignore
                }
            }
        } else
        // Field
        {
            return annotated.getAnnotations();
        }
        return results.toArray(new Annotation[results.size()]);
    }
}

Related

  1. getAnnotationOfField(Field field, Class clazz)
  2. getAnnotationOuterIndecies(Class annotationClass, Annotation[][] annotations)
  3. getAnnotationPropertyValue(Annotation a, String annotationPropertyName)
  4. getAnnotationRecursive(Class cls, Class annotationClass)
  5. getAnnotations(AnnotatedElement ae, Class annotationType)
  6. getAnnotations(Annotation[][] annotations)
  7. getAnnotations(Class cls)
  8. getAnnotations(Class annotationClass, Annotation[] annotations)
  9. getAnnotations(Class c, Class annotationClass)