集册 Java实例教程 在指定的clazz(包括指定的clazz本身)的继承层次结构中找到第一个Class

在指定的clazz(包括指定的clazz本身)的继承层次结构中找到第一个Class

欢马劈雪     最近更新时间:2020-01-02 10:19:05

465
在指定的clazz(包括指定的clazz本身)的继承层次结构中找到第一个Class
/* from 时 代 J     a    v  a - nowjava.com*/


import java.lang.annotation.Annotation;

import java.lang.reflect.AnnotatedElement;

import java.lang.reflect.Method;

import java.util.Arrays;

import java.util.LinkedList;

import java.util.List;

import java.util.Map;

import java.util.WeakHashMap;


public class Main{


    /**

     * Find the first {@link Class} in the inheritance hierarchy of the specified <code>clazz</code>

     * (including the specified <code>clazz</code> itself) which declares an annotation for the

     * specified <code>annotationType</code>, or <code>null</code> if not found. If the supplied

     * <code>clazz</code> is <code>null</code>, <code>null</code> will be returned.

     * <p>If the supplied <code>clazz</code> 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 Class object corresponding to the annotation type

     * @param clazz the Class object corresponding to the class on which to check for the annotation,

     * or <code>null</code>

     * @return the first {@link Class} in the inheritance hierarchy of the specified <code>clazz</code>

     * which declares an annotation for the specified <code>annotationType</code>, or <code>null</code>

     * if not found

     * @see Class#isAnnotationPresent(Class)

     * @see Class#getDeclaredAnnotations()

     */

    public static Class<?> findAnnotationDeclaringClass(

            Class<? extends Annotation> annotationType, Class<?> clazz) {

        Assert.notNull(annotationType, "Annotation type must not be null");

        if (clazz == null || clazz.equals(Object.class)) {

            return null;
            /* 
            *来 自
             N o w  J a v a  . c o m
            */

        }

        return (isAnnotationDeclaredLocally(annotationType, clazz)) ? clazz

                : findAnnotationDeclaringClass(annotationType,

                        clazz.getSuperclass());

    }

    /**

     * Determine whether an annotation for the specified <code>annotationType</code> is

     * declared locally on the supplied <code>clazz</code>. The supplied {@link Class}

     * may represent any type.

     * <p>Note: This method does <strong>not</strong> determine if the annotation is

     * {@link java.lang.annotation.Inherited inherited}. For greater clarity regarding inherited

     * annotations, consider using {@link #isAnnotationInherited(Class, Class)} instead.

     * @param annotationType the Class object corresponding to the annotation type

     * @param clazz the Class object corresponding to the class on which to check for the annotation

     * @return <code>true</code> if an annotation for the specified <code>annotationType</code>

     * is declared locally on the supplied <code>clazz</code>

     * @see Class#getDeclaredAnnotations()

     * @see #isAnnotationInherited(Class, Class)

     */

    public static boolean isAnnotationDeclaredLocally(

            Class<? 
展开阅读全文