Java Reflection Annotation Find findAnnotation(Class klass, Class annotationClass)

Here you can find the source of findAnnotation(Class klass, Class annotationClass)

Description

Returns the annotation on the given class or the package of the class.

License

Apache License

Parameter

Parameter Description
klass The class to search for the annotation.
annotationClass The Class of the annotation.

Return

The annotation or null.

Declaration

public static <T extends Annotation> T findAnnotation(Class<?> klass, Class<T> annotationClass) 

Method Source Code


//package com.java2s;
/*/*from   w  w  w  .java2s. c  om*/
 * Copyright 2002-2006,2009 The Apache Software Foundation.
 * 
 * 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 {
    /**
     * Returns the annotation on the given class or the package of the class. This searchs up the
     * class hierarchy and the package hierarchy for the closest match.
     *
     * @param   klass The class to search for the annotation.
     * @param   annotationClass The Class of the annotation.
     * @return  The annotation or null.
     */
    public static <T extends Annotation> T findAnnotation(Class<?> klass, Class<T> annotationClass) {
        T ann = klass.getAnnotation(annotationClass);
        while (ann == null && klass != null) {
            ann = klass.getAnnotation(annotationClass);
            if (ann == null)
                ann = klass.getPackage().getAnnotation(annotationClass);
            if (ann == null) {
                klass = klass.getSuperclass();
                if (klass != null) {
                    ann = klass.getAnnotation(annotationClass);
                }
            }
        }

        return ann;
    }

    public static Annotation getAnnotation(Class<? extends Annotation> anno, Class<?> cl) {
        return cl.getAnnotation(anno);
    }

    public static Annotation getAnnotation(Class<? extends Annotation> anno, Object obj) {
        return obj.getClass().getAnnotation(anno);
    }

    public static Annotation getAnnotation(Class<? extends Annotation> anno, Method meth) {
        return meth.getAnnotation(anno);
    }

    public static Annotation getAnnotation(Class<? extends Annotation> anno, Field field) {
        return field.getAnnotation(anno);
    }
}

Related

  1. findAnnotation(Class clazz, Class annotationType)
  2. findAnnotation(Class clazz, Class annotationClass)
  3. findAnnotation(Class clazz, Class annotationClass)
  4. findAnnotation(Class clazz, Class annotationClass)
  5. findAnnotation(Class clazz, Class annotationType)
  6. findAnnotation(final Class clazz, final Class type)
  7. findAnnotation(final Class clazz, final Class annotation)
  8. findAnnotation(final Class type, final Class annotationType)
  9. findAnnotation(final Class targetAnnotation, final Class annotatedType)