Description
Find a method annotation on the declaring class or in any of it's implemented interfaces
License
Open Source License
Declaration
public static <A extends Annotation> A findAnnotation(final Class<?> clazz, final Class<A> type)
Method Source Code
//package com.java2s;
/*/*from ww w . j a v a 2 s . c om*/
This file is part of Cyclos (www.cyclos.org).
A project of the Social Trade Organisation (www.socialtrade.org).
Cyclos is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Cyclos 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with Cyclos; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Main {
/**
* Find a method annotation on the declaring class or in any of it's implemented interfaces
*/
public static <A extends Annotation> A findAnnotation(final Class<?> clazz, final Class<A> type) {
final List<Class<?>> classes = allImplementedTypes(clazz);
for (final Class<?> c : classes) {
try {
final A annotation = c.getAnnotation(type);
if (annotation != null) {
return annotation;
}
} catch (final Exception e) {
// Try next one
}
}
return null;
}
/**
* Find a method annotation on the method in any of it's implemented interfaces
*/
public static <A extends Annotation> A findAnnotation(final Method method, final Class<A> type) {
return findAnnotation(method, type, false);
}
/**
* Find a method annotation on the method or declaring class or in any of it's implemented interfaces
*/
public static <A extends Annotation> A findAnnotation(final Method method, final Class<A> type,
final boolean searchInDeclaringClass) {
final Class<?> declaringClass = method.getDeclaringClass();
final List<Class<?>> classes = allImplementedTypes(declaringClass);
for (final Class<?> c : classes) {
try {
final Method m = c.getMethod(method.getName(), method.getParameterTypes());
final A annotation = m.getAnnotation(type);
if (annotation != null) {
return annotation;
}
} catch (final Exception e) {
// Try next one
}
}
// at this point the annotation was not found
if (searchInDeclaringClass) {
return findAnnotation(method.getDeclaringClass(), type);
} else {
return null;
}
}
/**
* Returns all implemented types for a given class - itself if a class and interfaces
*/
public static List<Class<?>> allImplementedTypes(Class<?> clazz) {
final Class<?>[] interfaces = clazz.getInterfaces();
final List<Class<?>> classes = new ArrayList<Class<?>>(interfaces.length + 1);
while (clazz != null && !clazz.equals(Object.class)) {
classes.add(clazz);
clazz = clazz.getSuperclass();
}
classes.addAll(Arrays.asList(interfaces));
return classes;
}
}
Related
- findAnnotation(Class> clazz, Class annotationClass)
- findAnnotation(Class> clazz, Class annotationClass)
- findAnnotation(Class> clazz, Class annotationClass)
- findAnnotation(Class> clazz, Class annotationType)
- findAnnotation(Class> klass, Class annotationClass)
- findAnnotation(final Class> clazz, final Class annotation)
- findAnnotation(final Class> type, final Class annotationType)
- findAnnotation(final Class targetAnnotation, final Class> annotatedType)
- findAnnotation(final Class annotationClass, final Class> beanClass, final Field field)