Here you can find the source of getAnnotation(Class> target, Class
public static <T extends Annotation> T getAnnotation(Class<?> target, Class<T> annoCls)
//package com.java2s; /**//w ww .j a va 2 s. c om * Copyright (c) 2008-2011, Open & Green Inc. * All rights reserved. * info@gaoshin.com * * This version of SORMA is licensed under the terms of the Open Source GPL 3.0 license. http://www.gnu.org/licenses/gpl.html. Alternate Licenses are available. * * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; * without even the implied warranty of MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, * AND NON-INFRINGEMENT OF THIRD-PARTY INTELLECTUAL PROPERTY RIGHTS. * See the GNU General Public License for more details. * */ import java.lang.annotation.Annotation; public class Main { public static <T extends Annotation> T getAnnotation(Class<?> target, Class<T> annoCls) { if (Object.class.equals(target)) { return null; } try { T annotation = target.getAnnotation(annoCls); if (annotation != null) return annotation; else return getAnnotation(target.getSuperclass(), annoCls); } catch (Exception e) { return null; } } }