Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -407,18 +407,32 @@ public Object postProcessAfterInitialization(final Object bean, final String bea
this.logger.debug(() -> annotatedMethods.size() + " @KafkaListener methods processed on bean '"
+ beanName + "': " + annotatedMethods);
}
Set<Method> methodsWithHandler = MethodIntrospector.selectMethods(targetClass,
(ReflectionUtils.MethodFilter) method ->
AnnotationUtils.findAnnotation(method, KafkaHandler.class) != null);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think no need to do this extraction outside of the if (hasClassLevelListeners) { branch.

boolean hasMethodLevelKafkaHandlerAnnotation = !methodsWithHandler.isEmpty();
if (hasClassLevelListeners) {
Set<Method> methodsWithHandler = MethodIntrospector.selectMethods(targetClass,
(ReflectionUtils.MethodFilter) method ->
AnnotationUtils.findAnnotation(method, KafkaHandler.class) != null);
List<Method> multiMethods = new ArrayList<>(methodsWithHandler);
processMultiMethodListeners(classLevelListeners, multiMethods, targetClass, bean, beanName);
}
throwErrorIfNoListenerMethods(bean, hasMethodLevelListeners,
hasClassLevelListeners, hasMethodLevelKafkaHandlerAnnotation);
}
}
return bean;
}

private void throwErrorIfNoListenerMethods(Object bean, boolean hasMethodLevelListeners,
boolean hasClassLevelListeners, boolean hasMethodLevelKafkaHandlerAnnotation) {
if (hasMethodLevelListeners) {
return;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit awkward style.
Why do we need to go to this method at all, if we meet this condition?
I fully don't see a reason in this method.
The if (!hasMethodLevelListeners && hasClassLevelListeners && !hasMethodLevelKafkaHandlerAnnotation) and throw new IllegalStateException is enough to have in place.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with you and addressed it!
I anticipated that the conditions could become more complex because of #3865 .
So, I extracted them into a separate method to clearly separate and handle the problematic conditions.

}

if (hasClassLevelListeners && !hasMethodLevelKafkaHandlerAnnotation) {
throw new IllegalStateException("No kafka listener methods found on bean type: " + bean.getClass());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think just on bean is enough, without any getClass() extraction.
The bean instance might lead to the place where it declared.

}
}

/*
* AnnotationUtils.getRepeatableAnnotations does not look at interfaces
*/
Expand Down