Here you can find the source of findAnnotation(Class> clazz, Class
public static <T extends Annotation> T findAnnotation(Class<?> clazz, Class<T> annotationClass)
//package com.java2s; /******************************************************************************************* * Copyright (c) 2016, zzg.zhou(11039850@qq.com) * //from w ww . j av a 2 s . com * Monalisa is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. *******************************************************************************************/ import java.lang.annotation.Annotation; public class Main { public static <T extends Annotation> T findAnnotation(Class<?> clazz, Class<T> annotationClass) { T a = null; Class<?> c = findClassWithAnnotation(clazz, annotationClass); if (c != null) { a = c.getAnnotation(annotationClass); } return a; } public static <T extends Annotation> Class<?> findClassWithAnnotation(Class<?> clazz, Class<T> annotationClass) { T a = null; while (a == null && clazz != null) { a = clazz.getAnnotation(annotationClass); if (a == null) { Class<?>[] interfaces = clazz.getInterfaces(); if (interfaces != null) { for (Class<?> c : interfaces) { a = c.getAnnotation(annotationClass); if (a != null) { return c; } } } clazz = clazz.getSuperclass(); } else { return clazz; } } return null; } }