Java Reflection Annotation getAnnotation(Class type, Class annotationClass)

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

Description

Return the provided Class' annotation for the specified type if such an annotation is present, else null.

License

Apache License

Parameter

Parameter Description
type Class to test
annotationClass The Class object corresponding to the annotation type

Return

The annotation for the specified annotation type if present on the given Class, or any of its implemented interfaces, else null

Declaration

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

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2012 Geoscience Australia/*from   w  ww.j  a  v a 2 s  .c  o  m*/
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class Main {
    /**
     * Return the provided Class' annotation for the specified type if such an
     * annotation is present, else null. Searches super-interfaces, and returns
     * the first annotation found.
     * 
     * @param type
     *            Class to test
     * @param annotationClass
     *            The Class object corresponding to the annotation type
     * @return The annotation for the specified annotation type if present on
     *         the given Class, or any of its implemented interfaces, else null
     * @see Class#getAnnotation(Class)
     */
    public static <T extends Annotation> T getAnnotation(Class<?> type, Class<T> annotationClass) {
        T t = type.getAnnotation(annotationClass);
        if (t != null) {
            return t;
        }
        for (Class<?> i : type.getInterfaces()) {
            if ((t = getAnnotation(i, annotationClass)) != null) {
                return t;
            }
        }
        return null;
    }

    /**
     * Return the provided Method's annotation for the specified type if such an
     * annotation is present, else null. Also searches super-classes and
     * super-interfaces for a matching method signature with the annotation, and
     * returns the first annotation found.
     * 
     * @param method
     *            Method to test
     * @param annotationClass
     *            The Class object corresponding to the annotation type
     * @return The annotation for the specified annotation type if present on
     *         the given Method, or any super-methods, else null.
     * @see Method#getAnnotation(Class)
     */
    public static <T extends Annotation> T getAnnotation(Method method, Class<T> annotationClass) {
        T t = method.getAnnotation(annotationClass);
        if (t != null) {
            return t;
        }

        Class<?> type = method.getDeclaringClass();
        for (Class<?> i : type.getInterfaces()) {
            try {
                Method m = i.getDeclaredMethod(method.getName(), method.getParameterTypes());
                if (m.getReturnType().equals(method.getReturnType())
                        && (t = getAnnotation(m, annotationClass)) != null) {
                    return t;
                }
            } catch (NoSuchMethodException e) {
            }
        }
        return null;
    }

    /**
     * Return the provided Field's annotation for the specified type if such an
     * annotation is present, else null.
     * 
     * @param field
     *            Field to test
     * @param annotationClass
     *            The Class object corresponding to the annotation type
     * @return The annotation for the specified annotation type if present on
     *         the given Field, else null.
     * @see Field#getAnnotation(Class)
     */
    public static <T extends Annotation> T getAnnotation(Field field, Class<T> annotationClass) {
        return field.getAnnotation(annotationClass);
    }
}

Related

  1. getAnnotation(Class target, Class annotationClass)
  2. getAnnotation(Class theClass, Class theAnnotation)
  3. getAnnotation(Class type, Class annotationType)
  4. getAnnotation(Class type, Class ann)
  5. getAnnotation(Class type, Class annotationType)
  6. getAnnotation(Class ac, AnnotatedElement m, Annotation... directAnnotations)
  7. getAnnotation(Class annotationType, Class classType)
  8. getAnnotation(Class at, Enum enu)
  9. getAnnotation(Class realClass, String pAttributeName, Class annotationClass)