Here you can find the source of getAnnotation(Method method, Class
Parameter | Description |
---|---|
method | a parameter |
annotationClass | a parameter |
T | a parameter |
public static <T extends Annotation> T getAnnotation(Method method, Class<T> annotationClass)
//package com.java2s; /*/*w w w .java 2s . co m*/ * Copyright (C) 2015 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; import java.lang.reflect.Parameter; public class Main { /** * Extract the annotation from the method 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 t = method.getAnnotation(annotationClass); if (t == null) { t = getAnnotation(method.getDeclaringClass(), annotationClass); } return t; } public static <T extends Annotation> T getAnnotation(Parameter parameter, Class<T> annotationClass) { for (Annotation annotation : parameter.getAnnotations()) { if (annotation.annotationType() == annotationClass) { return (T) annotation; } } return null; } 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); } }