Here you can find the source of getAnnotation(Class
Parameter | Description |
---|---|
ownerClass | a parameter |
method | a parameter |
public static <T extends Annotation> T getAnnotation(Class<T> annotation, Class<?> ownerClass, Method method)
//package com.java2s; /*//from w w w. j av a 2s .c o m Leola Programming Language Author: Tony Sparks See license.txt */ import java.lang.annotation.Annotation; import java.lang.reflect.Method; public class Main { /** * Retrieves the annotation from the method, if it is not on the method, the parent * is checked. This solves the "inherited annotation" problem for interfaces. * @param ownerClass * @param method * @return */ public static <T extends Annotation> T getAnnotation(Class<T> annotation, Class<?> ownerClass, Method method) { if (method == null) { return null; } if (method.isAnnotationPresent(annotation)) { return method.getAnnotation(annotation); } else { for (Class<?> aInterface : ownerClass.getInterfaces()) { Method inheritedMethod = getInheritedMethod(aInterface, method); if (inheritedMethod != null) { T result = getAnnotation(annotation, aInterface, inheritedMethod); if (result != null) { return result; } } } /* Query the parent class for the annotation */ Class<?> superClass = method.getDeclaringClass().getSuperclass(); Method inheritedMethod = getInheritedMethod(superClass, method); return getAnnotation(annotation, superClass, inheritedMethod); } } /** * Get the inherited method. * @param aClass * @param method * @return */ public static Method getInheritedMethod(Class<?> aClass, Method method) { Method inheritedMethod = null; try { for (Class<?> a = aClass; a != null; a = a.getSuperclass()) { inheritedMethod = aClass.getMethod(method.getName(), method.getParameterTypes()); if (inheritedMethod != null) { return inheritedMethod; } } } catch (Exception e) { /* Ignore */ } return null; } }