Java Reflection Annotation Find findAnnotationDeclaringClass(Class annotationType, Class clazz)

Here you can find the source of findAnnotationDeclaringClass(Class annotationType, Class clazz)

Description

Find the first Class in the inheritance hierarchy of the specified clazz (including the specified clazz itself) which declares an annotation for the specified annotationType , or null if not found.

License

Apache License

Parameter

Parameter Description
annotationType the annotation type to look for, both locally and as a meta-annotation
clazz the class on which to check for the annotation (may be null )

Return

the first in the inheritance hierarchy of the specified clazz which declares an annotation for the specified annotationType , or null if not found

Declaration

public static Class<?> findAnnotationDeclaringClass(Class<? extends Annotation> annotationType,
        Class<?> clazz) 

Method Source Code


//package com.java2s;
/*/*from w ww . j a  va2  s. c  om*/
 * Copyright 2002-2014 the original author or authors.
 *
 * 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;

public class Main {
    /**
     * Find the first {@link Class} in the inheritance hierarchy of the specified {@code clazz}
     * (including the specified {@code clazz} itself) which declares an annotation for the
     * specified {@code annotationType}, or {@code null} if not found. If the supplied
     * {@code clazz} is {@code null}, {@code null} will be returned.
     * <p>If the supplied {@code clazz} is an interface, only the interface itself will be checked;
     * the inheritance hierarchy for interfaces will not be traversed.
     * <p>The standard {@link Class} API does not provide a mechanism for determining which class
     * in an inheritance hierarchy actually declares an {@link Annotation}, so we need to handle
     * this explicitly.
     * @param annotationType the annotation type to look for, both locally and as a meta-annotation
     * @param clazz the class on which to check for the annotation (may be {@code null})
     * @return the first {@link Class} in the inheritance hierarchy of the specified {@code clazz}
     * which declares an annotation for the specified {@code annotationType}, or {@code null}
     * if not found
     * @see Class#isAnnotationPresent(Class)
     * @see Class#getDeclaredAnnotations()
     * @see #findAnnotationDeclaringClassForTypes(List, Class)
     * @see #isAnnotationDeclaredLocally(Class, Class)
     */
    public static Class<?> findAnnotationDeclaringClass(Class<? extends Annotation> annotationType,
            Class<?> clazz) {
        if (clazz == null || clazz.equals(Object.class)) {
            return null;
        }
        if (isAnnotationDeclaredLocally(annotationType, clazz)) {
            return clazz;
        }
        return findAnnotationDeclaringClass(annotationType, clazz.getSuperclass());
    }

    public static boolean isAnnotationDeclaredLocally(Class<? extends Annotation> annotationType, Class<?> clazz) {
        boolean declaredLocally = false;
        for (Annotation annotation : clazz.getDeclaredAnnotations()) {
            if (annotation.annotationType().equals(annotationType)) {
                declaredLocally = true;
                break;
            }
        }
        return declaredLocally;
    }
}

Related

  1. findAnnotation(Method method, Class type)
  2. findAnnotation(Set annotations, Class annotationClass)
  3. findAnnotationClass( Class c, Class base)
  4. findAnnotationDeclaringClass(Class annotationType, Class clazz)
  5. findAnnotationDeclaringClass(Class annotationType, Class classToFind)
  6. findAnnotationFromClass(Class target, Class annotation)
  7. findAnnotationFromMethodOrClass(final Method method, final Class annotationClass)
  8. findAnnotationHelper( Class base, Type iface)
  9. findAnnotationIn(List modifiers, Class clazz)