集册 Java实例教程 检查是否使用给定的注释对类或方法进行注释

检查是否使用给定的注释对类或方法进行注释

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

515
检查是否使用给定的注释对类或方法进行注释
/**来 自 时代Java**/


//package com.nowjava;

import java.lang.annotation.Annotation;

import java.lang.reflect.AnnotatedElement;


public class Main {

    /**

     * Checks if a Class or Method are annotated with the given annotation

     *

     * @param elem                 the Class or Method to reflect on

     * @param annotationType       the annotation type

     * @param checkMetaAnnotations check for meta annotations

     * @return true if annotations is present

     */

    public static boolean hasAnnotation(AnnotatedElement elem,

            Class<? extends Annotation> annotationType,

            boolean checkMetaAnnotations) {

        if (elem.isAnnotationPresent(annotationType)) {

            return true;

        }

        if (checkMetaAnnotations) {

            for (Annotation a : elem.getAnnotations()) {

                for (Annotation meta : a.annotationType().getAnnotations()) {

                    if (meta.annotationType().getName()/**from n  o  w  j  a  v 
展开阅读全文