|
| 1 | +/* |
| 2 | + * Copyright 2016-2025 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.kafka.annotation; |
| 18 | + |
| 19 | +import java.lang.reflect.Method; |
| 20 | +import java.lang.reflect.Modifier; |
| 21 | +import java.util.Optional; |
| 22 | +import java.util.Set; |
| 23 | + |
| 24 | +import org.springframework.core.MethodIntrospector; |
| 25 | +import org.springframework.core.annotation.AnnotatedElementUtils; |
| 26 | +import org.springframework.core.annotation.AnnotationUtils; |
| 27 | +import org.springframework.util.ReflectionUtils; |
| 28 | +import org.springframework.util.ReflectionUtils.MethodFilter; |
| 29 | + |
| 30 | +/** |
| 31 | + * Find the appropriate handler method when the target class has a class-level {@link KafkaListener} |
| 32 | + * annotation and contains a single public method without a {@link KafkaHandler} annotation. |
| 33 | + * |
| 34 | + * @author Sanghyeok An |
| 35 | + * |
| 36 | + * @since 4.0 |
| 37 | + * |
| 38 | + * @see KafkaListenerAnnotationBeanPostProcessor |
| 39 | + */ |
| 40 | +public class KafkaHandlerParser { |
| 41 | + |
| 42 | + /** |
| 43 | + * Finds the appropriate handler method when the target class has a class-level {@link KafkaListener} |
| 44 | + * annotation and contains a single public method without a {@link KafkaHandler} annotation. |
| 45 | + * This method is used to determine which method should be invoked for Kafka message handling |
| 46 | + * when no explicit {@link KafkaHandler} annotations are present but the class itself is annotated with {@link KafkaListener}. |
| 47 | + * |
| 48 | + * @param targetClass the class to inspect for handler methods |
| 49 | + * @return the method to be used for kafka message handling, or {@code null} if no suitable method is found. |
| 50 | + */ |
| 51 | + public Optional<Method> parseSingleHandlerMethod(Class<?> targetClass) { |
| 52 | + |
| 53 | + Set<Method> methodsWithAnnotations = MethodIntrospector.selectMethods( |
| 54 | + targetClass, (MethodFilter) method -> |
| 55 | + AnnotatedElementUtils.findMergedAnnotation(method, KafkaHandler.class) != null || |
| 56 | + AnnotatedElementUtils.findMergedAnnotation(method, KafkaListener.class) != null |
| 57 | + ); |
| 58 | + |
| 59 | + if (!methodsWithAnnotations.isEmpty()) { |
| 60 | + return Optional.empty(); |
| 61 | + } |
| 62 | + |
| 63 | + Set<Method> publicMethodsWithoutHandlerAnnotation = MethodIntrospector.selectMethods( |
| 64 | + targetClass, (ReflectionUtils.MethodFilter) method -> |
| 65 | + Modifier.isPublic(method.getModifiers()) && |
| 66 | + AnnotatedElementUtils.findMergedAnnotation(method, KafkaHandler.class) == null && |
| 67 | + AnnotatedElementUtils.findMergedAnnotation(method, KafkaListener.class) == null |
| 68 | + ); |
| 69 | + |
| 70 | + if (!hasSinglePublicMethod(publicMethodsWithoutHandlerAnnotation)) { |
| 71 | + return Optional.empty(); |
| 72 | + } |
| 73 | + |
| 74 | + Method publicMethod = publicMethodsWithoutHandlerAnnotation.iterator().next(); |
| 75 | + return Optional.of(publicMethod); |
| 76 | + } |
| 77 | + |
| 78 | + private boolean hasSinglePublicMethod(Set<Method> methods) { |
| 79 | + return methods.size() == 1; |
| 80 | + } |
| 81 | + |
| 82 | +} |
0 commit comments