Java Reflection Annotation getAnnotation(Class type, Class annotationType)

Here you can find the source of getAnnotation(Class type, Class annotationType)

Description

Returns the first annotation of the specified annotation type for the given type.
If no annotation is found for the type, the hierarchical ancestors are examined.

License

Open Source License

Parameter

Parameter Description
type the type which might be annotated
annotationType the annotation type

Return

the annotation or null

Declaration

public static Annotation getAnnotation(Class<?> type, Class<? extends Annotation> annotationType) 

Method Source Code


//package com.java2s;
/*//from  w  w  w .ja  va 2  s. com
 * DuDe - The Duplicate Detection Toolkit
 * 
 * Copyright (C) 2010  Hasso-Plattner-Institut f?r Softwaresystemtechnik GmbH,
 *                     Potsdam, Germany 
 *
 * This file is part of DuDe.
 * 
 * DuDe 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
 * (at your option) any later version.
 *
 * DuDe is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY 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 Public License
 * along with DuDe.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

import java.lang.annotation.Annotation;

public class Main {
    /**
     * Returns the first annotation of the specified annotation type for the given type.<br>
     * If no annotation is found for the type, the hierarchical ancestors are examined.
     * 
     * @param type
     *            the type which might be annotated
     * @param annotationType
     *            the annotation type
     * @return the annotation or null
     */
    public static Annotation getAnnotation(Class<?> type, Class<? extends Annotation> annotationType) {
        Annotation annotation = null;
        for (Class<?> t = type; annotation == null && t != null; t = t.getSuperclass())
            annotation = t.getAnnotation(annotationType);
        return annotation;
    }
}

Related

  1. getAnnotation(Class objectClass, Class annotationClass)
  2. getAnnotation(Class onClass, Class desiredAnnotationClass)
  3. getAnnotation(Class target, Class annoCls)
  4. getAnnotation(Class target, Class annotationClass)
  5. getAnnotation(Class theClass, Class theAnnotation)
  6. getAnnotation(Class type, Class ann)
  7. getAnnotation(Class type, Class annotationType)
  8. getAnnotation(Class type, Class annotationClass)
  9. getAnnotation(Class ac, AnnotatedElement m, Annotation... directAnnotations)