Here you can find the source of getAnnotation(Class> objectClass, Class
public static <T extends Annotation> T getAnnotation(Class<?> objectClass, Class<T> annotationClass)
//package com.java2s; /*/*from w ww.j av a2 s. co m*/ * Copyright (C) 2016 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; import java.lang.reflect.Method; public class Main { /** * Extract the annotation from the controllerMethod or the declaring class. * * @param method * @param annotationClass * @param <T> * @return the annotation or null */ public static <T extends Annotation> T getAnnotation(Method method, Class<T> annotationClass) { T annotation = method.getAnnotation(annotationClass); if (annotation == null) { annotation = getAnnotation(method.getDeclaringClass(), annotationClass); } return annotation; } public static <T extends Annotation> T getAnnotation(Class<?> objectClass, Class<T> annotationClass) { if (objectClass == null || Object.class == objectClass) { return null; } T annotation = objectClass.getAnnotation(annotationClass); if (annotation != null) { return annotation; } return getAnnotation(objectClass.getSuperclass(), annotationClass); } }