@Bean public AnnotationUtil annotationUtil() { return new AnnotationUtilImpl(); }
@Override public <A extends Annotation> A getTypeAnnotation(Class<?> annotatedType, Class<A> annotation) { A result = getClassAnnotation(annotatedType, annotation); Class<?> currentClass = annotatedType; while (result == null) { result = getInterfacesAnnotation(currentClass, annotation); currentClass = currentClass.getSuperclass(); if (currentClass == null) { break; } } return result; }
/** * 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)}). * @throws IllegalArgumentException if the given {@code annotationType} is NO * {@link AnnotationUtilImpl#isRuntimeAnnotation(Class) runtime annotation}. */ public AnnotationFilter(Class<? extends Annotation> annotationType, boolean forceInheritence) throws IllegalArgumentException { this(annotationType, forceInheritence, AnnotationUtilImpl.getInstance()); }
@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; }
/** * This method gets the singleton instance of this {@link AnnotationUtilImpl}. <br> * <b>ATTENTION:</b><br> * Please prefer dependency-injection instead of using this method. * * @return the singleton instance. */ public static AnnotationUtilImpl getInstance() { if (instance == null) { synchronized (AnnotationUtilImpl.class) { if (instance == null) { AnnotationUtilImpl util = new AnnotationUtilImpl(); util.setReflectionUtil(ReflectionUtilImpl.getInstance()); instance = util; } } } return instance; }
@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; }
/** * {@inheritDoc} * * @return {@code true} if the specified {@link Class#isAnnotationPresent(Class) annotation is present} for the given * {@code type}, {@code false} otherwise. */ @Override public boolean accept(Class<?> type) { if (type != null) { if (this.forceInheritence) { Annotation annotation = this.annotationUtil.getTypeAnnotation(type, this.annotationType); return (annotation != null); } else { return type.isAnnotationPresent(this.annotationType); } } return false; }
/** * 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; }
/** * @param reflectionUtil the reflectionUtil to set */ @Inject public void setReflectionUtil(ReflectionUtil reflectionUtil) { getInitializationState().requireNotInitilized(); this.reflectionUtil = reflectionUtil; }
A result = getInterfacesAnnotation(interfaces[i], annotation); if (result != null) { return result;