-
Notifications
You must be signed in to change notification settings - Fork 1.7k
GH-3807: Necessity of KafkaHandler on single method class #3865
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9511818
spring-projectsGH-3807: Necessity of KafkaHandler on single method class
chickenchickenlove 13ee0d1
Addressing PR review
chickenchickenlove 19f9371
Addressing PR review
chickenchickenlove 62f82f7
Revert to a52faccd
chickenchickenlove c322d68
Addressing PR review
chickenchickenlove f395cce
Addressing PR review
chickenchickenlove File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
81 changes: 81 additions & 0 deletions
81
spring-kafka/src/main/java/org/springframework/kafka/annotation/KafkaHandlerParser.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* Copyright 2016-2025 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.kafka.annotation; | ||
|
||
import java.lang.reflect.Method; | ||
import java.lang.reflect.Modifier; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
|
||
import org.springframework.core.MethodIntrospector; | ||
import org.springframework.core.annotation.AnnotatedElementUtils; | ||
import org.springframework.util.ReflectionUtils; | ||
import org.springframework.util.ReflectionUtils.MethodFilter; | ||
|
||
/** | ||
* Find the appropriate handler method when the target class has a class-level {@link KafkaListener} | ||
* annotation and contains a single public method without a {@link KafkaHandler} annotation. | ||
* | ||
* @author Sanghyeok An | ||
* | ||
* @since 4.0 | ||
* | ||
* @see KafkaListenerAnnotationBeanPostProcessor | ||
*/ | ||
public class KafkaHandlerParser { | ||
|
||
/** | ||
* Finds the appropriate handler method when the target class has a class-level {@link KafkaListener} | ||
* annotation and contains a single public method without a {@link KafkaHandler} annotation. | ||
* This method is used to determine which method should be invoked for Kafka message handling | ||
* when no explicit {@link KafkaHandler} annotations are present but the class itself is annotated with {@link KafkaListener}. | ||
* | ||
* @param targetClass the class to inspect for handler methods | ||
* @return the method to be used for kafka message handling, or {@code null} if no suitable method is found. | ||
*/ | ||
public Optional<Method> parseSingleHandlerMethod(Class<?> targetClass) { | ||
|
||
Set<Method> methodsWithAnnotations = MethodIntrospector.selectMethods( | ||
targetClass, (MethodFilter) method -> | ||
AnnotatedElementUtils.findMergedAnnotation(method, KafkaHandler.class) != null || | ||
AnnotatedElementUtils.findMergedAnnotation(method, KafkaListener.class) != null | ||
); | ||
|
||
if (!methodsWithAnnotations.isEmpty()) { | ||
return Optional.empty(); | ||
} | ||
|
||
Set<Method> publicMethodsWithoutHandlerAnnotation = MethodIntrospector.selectMethods( | ||
targetClass, (ReflectionUtils.MethodFilter) method -> | ||
Modifier.isPublic(method.getModifiers()) && | ||
AnnotatedElementUtils.findMergedAnnotation(method, KafkaHandler.class) == null && | ||
AnnotatedElementUtils.findMergedAnnotation(method, KafkaListener.class) == null | ||
); | ||
|
||
if (!hasSinglePublicMethod(publicMethodsWithoutHandlerAnnotation)) { | ||
return Optional.empty(); | ||
} | ||
|
||
Method publicMethod = publicMethodsWithoutHandlerAnnotation.iterator().next(); | ||
return Optional.of(publicMethod); | ||
} | ||
|
||
private boolean hasSinglePublicMethod(Set<Method> methods) { | ||
return methods.size() == 1; | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.