/** * The constructor. * * @param annotationType is the annotation that has to be {@link Class#isAnnotationPresent(Class) present} if a type * should be {@link #accept(Class) accepted}. * @param forceInheritence if {@code true} then also types are accepted if one of their super-types (including * implemented interfaces) is properly annotated (see * {@link AnnotationUtilImpl#getTypeAnnotation(Class, Class)}). * @param annotationUtil is the {@link AnnotationUtilImpl} instance to use. * @throws IllegalArgumentException if the given {@code annotationType} is NO * {@link AnnotationUtilImpl#isRuntimeAnnotation(Class) runtime annotation}. */ public AnnotationFilter(Class<? extends Annotation> annotationType, boolean forceInheritence, AnnotationUtilImpl annotationUtil) throws IllegalArgumentException { super(); if (!annotationUtil.isRuntimeAnnotation(annotationType)) { throw new IllegalArgumentException( "Given annotation (" + annotationType + ") can NOT be resolved at runtime!"); } this.annotationUtil = annotationUtil; this.annotationType = annotationType; this.forceInheritence = forceInheritence; }
@Override public <A extends Annotation> A getClassAnnotation(Class<?> annotatedClass, Class<A> annotation) throws IllegalArgumentException { if (!isRuntimeAnnotation(annotation)) { throw new AnnotationNotRuntimeException(annotation); } if (!isAnnotationForType(annotation, ElementType.TYPE)) { throw new AnnotationNotForTargetException(annotation, ElementType.TYPE); } A result = annotatedClass.getAnnotation(annotation); Class<?> currentClass = annotatedClass; while (result == null) { currentClass = currentClass.getSuperclass(); if (currentClass == null) { return null; } else { result = currentClass.getAnnotation(annotation); } } return result; }
@Override public <A extends Annotation> A getMethodAnnotation(Method annotatedMethod, Class<A> annotation) { if (!isRuntimeAnnotation(annotation)) { throw new AnnotationNotRuntimeException(annotation); } if (!isAnnotationForType(annotation, ElementType.METHOD)) { throw new AnnotationNotForTargetException(annotation, ElementType.METHOD); } A result = annotatedMethod.getAnnotation(annotation); if (result == null) { String methodName = annotatedMethod.getName(); Class<?>[] parameterTypes = annotatedMethod.getParameterTypes(); Class<?> inheritingClass = annotatedMethod.getDeclaringClass(); while (result == null) { Method currentMethod = getReflectionUtil().getParentMethod(inheritingClass, methodName, parameterTypes); if (currentMethod == null) { return null; } else { result = currentMethod.getAnnotation(annotation); inheritingClass = currentMethod.getDeclaringClass(); } } } return result; }