Java Reflection Annotation getAnnotationInClass( final Class annotationClass, final Class clazz)

Here you can find the source of getAnnotationInClass( final Class annotationClass, final Class clazz)

Description

Checks if class is noted by annotationClass.

License

Open Source License

Parameter

Parameter Description
T a parameter
annotationClass a parameter
clazz a parameter

Declaration

public static <T extends Annotation> T getAnnotationInClass(
        final Class<T> annotationClass, final Class<?> clazz) 

Method Source Code

//package com.java2s;
/**/*from w w  w . j av  a  2s  . c o m*/
 * Vulpe Framework - Quick and Smart ;)
 * Copyright (C) 2011 Active Thread
 *
 * Este programa ? software livre; voc? pode redistribu?-lo e/ou
 * modific?-lo sob os termos da Licen?a P?blica Geral GNU, conforme
 * publicada pela Free Software Foundation; tanto a vers?o 2 da
 * Licen?a como (a seu crit?rio) qualquer vers?o mais nova.
 *
 * Este programa ? distribu?do na expectativa de ser ?til, mas SEM
 * QUALQUER GARANTIA; sem mesmo a garantia impl?cita de
 * COMERCIALIZA??O ou de ADEQUA??O A QUALQUER PROP?SITO EM
 * PARTICULAR. Consulte a Licen?a P?blica Geral GNU para obter mais
 * detalhes.
 *
 * Voc? deve ter recebido uma c?pia da Licen?a P?blica Geral GNU
 * junto com este programa; se n?o, escreva para a Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
 */

import java.lang.annotation.Annotation;

public class Main {
    /**
     * Checks if class is noted by <code>annotationClass</code>.
     *
     * @param <T>
     * @param annotationClass
     * @param clazz
     * @return
     */
    public static <T extends Annotation> T getAnnotationInClass(
            final Class<T> annotationClass, final Class<?> clazz) {
        Class<?> baseClass = clazz;
        while (baseClass != null && !baseClass.equals(Object.class)) {
            if (baseClass.isAnnotationPresent(annotationClass)) {
                return baseClass.getAnnotation(annotationClass);
            }

            for (Class<?> iClass : baseClass.getInterfaces()) {
                final T tAnnotation = getAnnotationInClass(annotationClass,
                        iClass);
                if (tAnnotation != null) {
                    return tAnnotation;
                }
            }

            baseClass = baseClass.getSuperclass();
        }

        return null;
    }
}

Related

  1. getAnnotationFromField(Object object, Class annotationClass)
  2. getAnnotationFromJoinpointMethod(ProceedingJoinPoint joinpoint, Class annotationClass)
  3. getAnnotationFromStackTrace(Class annotationClass)
  4. getAnnotationFromWeldBean(Object target, Class annotation)
  5. getAnnotationFullname(Annotation annotation)
  6. getAnnotationInHeirarchy(Class annotationClass, Class aClass)
  7. getAnnotationInherited(Class type, Class annotationClass)
  8. getAnnotationInstances(Class clazz, Class annotationClass)
  9. getAnnotationMemberDefaults(Annotation annotation)