Skip to content

Commit c0dc484

Browse files
committed
feat: support for beanRef in KafkaListener annotation
1 parent fea0a2e commit c0dc484

File tree

26 files changed

+160
-54
lines changed

26 files changed

+160
-54
lines changed

springwolf-core/src/main/java/io/github/springwolf/core/asyncapi/scanners/bindings/BindingFactory.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010
import java.util.Map;
1111

1212
public interface BindingFactory<T> {
13-
default String getChannelId(T annotation) {
14-
return ReferenceUtil.toValidId(getChannelName(annotation));
13+
default String getChannelId(T annotation, Class<?> component) {
14+
return ReferenceUtil.toValidId(getChannelName(annotation, component));
1515
}
1616

17-
String getChannelName(T annotation);
17+
String getChannelName(T annotation, Class<?> component);
1818

1919
Map<String, ChannelBinding> buildChannelBinding(T annotation);
2020

springwolf-core/src/main/java/io/github/springwolf/core/asyncapi/scanners/channels/annotations/SpringAnnotationClassLevelChannelsScanner.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,13 @@ private Stream<ChannelObject> mapClassToChannel(
4545
Set<Method> methods =
4646
annotatedMethods.stream().map(MethodAndAnnotation::method).collect(Collectors.toSet());
4747
Map<String, Message> messages = new HashMap<>(springAnnotationMessagesService.buildMessages(
48-
classAnnotation, methods, SpringAnnotationMessagesService.MessageType.CHANNEL));
48+
classAnnotation, component, methods, SpringAnnotationMessagesService.MessageType.CHANNEL));
4949

50-
return mapClassToChannel(classAnnotation, messages);
50+
return mapClassToChannel(classAnnotation, component, messages);
5151
}
5252

53-
private Stream<ChannelObject> mapClassToChannel(ClassAnnotation classAnnotation, Map<String, Message> messages) {
54-
return Stream.of(springAnnotationChannelService.buildChannel(classAnnotation, messages));
53+
private Stream<ChannelObject> mapClassToChannel(
54+
ClassAnnotation classAnnotation, Class<?> component, Map<String, Message> messages) {
55+
return Stream.of(springAnnotationChannelService.buildChannel(classAnnotation, component, messages));
5556
}
5657
}

springwolf-core/src/main/java/io/github/springwolf/core/asyncapi/scanners/channels/annotations/SpringAnnotationMethodLevelChannelsScanner.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ private ChannelObject mapMethodToChannel(MethodAndAnnotation<MethodAnnotation> m
4848
MessageObject message = springAnnotationMessageService.buildMessage(annotation, payloadSchema, headerSchema);
4949
Map<String, Message> messages = Map.of(message.getMessageId(), MessageReference.toComponentMessage(message));
5050

51-
return springAnnotationChannelService.buildChannel(annotation, messages);
51+
return springAnnotationChannelService.buildChannel(
52+
annotation, method.method().getDeclaringClass(), messages);
5253
}
5354
}

springwolf-core/src/main/java/io/github/springwolf/core/asyncapi/scanners/common/channel/SpringAnnotationChannelService.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ public class SpringAnnotationChannelService<Annotation extends java.lang.annotat
1818

1919
private final BindingFactory<Annotation> bindingFactory;
2020

21-
public ChannelObject buildChannel(Annotation annotation, Map<String, Message> messages) {
21+
public ChannelObject buildChannel(Annotation annotation, Class<?> component, Map<String, Message> messages) {
2222
Map<String, ChannelBinding> channelBinding = bindingFactory.buildChannelBinding(annotation);
2323
Map<String, ChannelBinding> chBinding = channelBinding != null ? new HashMap<>(channelBinding) : null;
24-
String channelName = bindingFactory.getChannelName(annotation);
24+
String channelName = bindingFactory.getChannelName(annotation, component);
2525
return ChannelObject.builder()
2626
.channelId(ReferenceUtil.toValidId(channelName))
2727
.address(channelName)

springwolf-core/src/main/java/io/github/springwolf/core/asyncapi/scanners/common/message/SpringAnnotationMessagesService.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,13 @@ public enum MessageType {
4343
}
4444

4545
public Map<String, MessageReference> buildMessages(
46-
ClassAnnotation classAnnotation, Set<Method> methods, MessageType messageType) {
46+
ClassAnnotation classAnnotation, Class<?> component, Set<Method> methods, MessageType messageType) {
4747
Set<MessageObject> messages = methods.stream()
4848
.map(method -> buildMessage(classAnnotation, method))
4949
.collect(toSet());
5050

5151
if (messageType == MessageType.OPERATION) {
52-
String channelId = bindingFactory.getChannelName(classAnnotation);
52+
String channelId = bindingFactory.getChannelName(classAnnotation, component);
5353
return toOperationsMessagesMap(channelId, messages);
5454
}
5555
return toMessagesMap(messages);

springwolf-core/src/main/java/io/github/springwolf/core/asyncapi/scanners/common/operation/SpringAnnotationOperationService.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,14 @@ public class SpringAnnotationOperationService<MethodAnnotation extends Annotatio
2525
private final SpringAnnotationMessageService<MethodAnnotation> springAnnotationMessageService;
2626

2727
public Operation buildOperation(
28-
MethodAnnotation annotation, PayloadSchemaObject payloadType, SchemaObject headerSchema) {
28+
MethodAnnotation annotation,
29+
Class<?> component,
30+
PayloadSchemaObject payloadType,
31+
SchemaObject headerSchema) {
2932
MessageObject message = springAnnotationMessageService.buildMessage(annotation, payloadType, headerSchema);
3033
Map<String, OperationBinding> operationBinding = bindingFactory.buildOperationBinding(annotation);
3134
Map<String, OperationBinding> opBinding = operationBinding != null ? new HashMap<>(operationBinding) : null;
32-
String channelId = bindingFactory.getChannelId(annotation);
35+
String channelId = bindingFactory.getChannelId(annotation, component);
3336

3437
return Operation.builder()
3538
.action(OperationAction.RECEIVE)

springwolf-core/src/main/java/io/github/springwolf/core/asyncapi/scanners/common/operation/SpringAnnotationOperationsService.java

+6-5
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,17 @@ public class SpringAnnotationOperationsService<ClassAnnotation extends Annotatio
2323
private final BindingFactory<ClassAnnotation> bindingFactory;
2424
private final SpringAnnotationMessagesService<ClassAnnotation> springAnnotationMessagesService;
2525

26-
public Operation buildOperation(ClassAnnotation classAnnotation, Set<Method> methods) {
26+
public Operation buildOperation(ClassAnnotation classAnnotation, Class<?> component, Set<Method> methods) {
2727
var messages = springAnnotationMessagesService.buildMessages(
28-
classAnnotation, methods, SpringAnnotationMessagesService.MessageType.OPERATION);
29-
return buildOperation(classAnnotation, messages);
28+
classAnnotation, component, methods, SpringAnnotationMessagesService.MessageType.OPERATION);
29+
return buildOperation(classAnnotation, component, messages);
3030
}
3131

32-
private Operation buildOperation(ClassAnnotation classAnnotation, Map<String, MessageReference> messages) {
32+
private Operation buildOperation(
33+
ClassAnnotation classAnnotation, Class<?> component, Map<String, MessageReference> messages) {
3334
Map<String, OperationBinding> operationBinding = bindingFactory.buildOperationBinding(classAnnotation);
3435
Map<String, OperationBinding> opBinding = operationBinding != null ? new HashMap<>(operationBinding) : null;
35-
String channelName = bindingFactory.getChannelName(classAnnotation);
36+
String channelName = bindingFactory.getChannelName(classAnnotation, component);
3637
String channelId = ReferenceUtil.toValidId(channelName);
3738

3839
return Operation.builder()

springwolf-core/src/main/java/io/github/springwolf/core/asyncapi/scanners/operations/annotations/SpringAnnotationClassLevelOperationsScanner.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,13 @@ private Stream<Map.Entry<String, Operation>> mapClassToOperation(
4343
Class<?> component, Set<MethodAndAnnotation<MethodAnnotation>> annotatedMethods) {
4444
ClassAnnotation classAnnotation = AnnotationUtil.findFirstAnnotationOrThrow(classAnnotationClass, component);
4545

46-
String channelId = bindingFactory.getChannelId(classAnnotation);
46+
String channelId = bindingFactory.getChannelId(classAnnotation, component);
4747
String operationId =
4848
StringUtils.joinWith("_", channelId, OperationAction.RECEIVE.type, component.getSimpleName());
4949

5050
Set<Method> methods =
5151
annotatedMethods.stream().map(MethodAndAnnotation::method).collect(Collectors.toSet());
52-
Operation operation = springAnnotationOperationsService.buildOperation(classAnnotation, methods);
52+
Operation operation = springAnnotationOperationsService.buildOperation(classAnnotation, component, methods);
5353
annotatedMethods.forEach(
5454
method -> customizers.forEach(customizer -> customizer.customize(operation, method.method())));
5555
return Stream.of(Map.entry(operationId, operation));

springwolf-core/src/main/java/io/github/springwolf/core/asyncapi/scanners/operations/annotations/SpringAnnotationMethodLevelOperationsScanner.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,16 @@ public Stream<Map.Entry<String, Operation>> scan(Class<?> clazz) {
4343
private Map.Entry<String, Operation> mapMethodToOperation(MethodAndAnnotation<MethodAnnotation> method) {
4444
MethodAnnotation annotation = AnnotationUtil.findFirstAnnotationOrThrow(methodAnnotationClass, method.method());
4545

46-
String channelId = bindingFactory.getChannelId(annotation);
46+
String channelId =
47+
bindingFactory.getChannelId(annotation, method.method().getDeclaringClass());
4748
String operationId = StringUtils.joinWith(
4849
"_", channelId, OperationAction.RECEIVE.type, method.method().getName());
4950

5051
PayloadSchemaObject payloadSchema = payloadMethodParameterService.extractSchema(method.method());
5152
SchemaObject headerSchema = headerClassExtractor.extractHeader(method.method(), payloadSchema);
5253

53-
Operation operation = springAnnotationOperationService.buildOperation(annotation, payloadSchema, headerSchema);
54+
Operation operation = springAnnotationOperationService.buildOperation(
55+
annotation, method.method().getDeclaringClass(), payloadSchema, headerSchema);
5456
customizers.forEach(customizer -> customizer.customize(operation, method.method()));
5557
return Map.entry(operationId, operation);
5658
}

springwolf-core/src/test/java/io/github/springwolf/core/asyncapi/scanners/channels/annotations/SpringAnnotationClassLevelChannelsScannerIntegrationTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ static class TestBindingFactory implements BindingFactory<TestClassListener> {
291291
Map.of(CHANNEL_ID, new TestBindingFactory.TestOperationBinding());
292292

293293
@Override
294-
public String getChannelName(TestClassListener annotation) {
294+
public String getChannelName(TestClassListener annotation, Class<?> component) {
295295
return CHANNEL;
296296
}
297297

springwolf-core/src/test/java/io/github/springwolf/core/asyncapi/scanners/channels/annotations/SpringAnnotationClassLevelChannelsScannerTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ void scan() {
4040
MessageReference message = MessageReference.toComponentMessage("messageId");
4141
Map<String, Message> messages = Map.of("messageId", message);
4242

43-
when(springAnnotationChannelService.buildChannel(any(), any()))
43+
when(springAnnotationChannelService.buildChannel(any(), any(), any()))
4444
.thenReturn(ChannelObject.builder()
4545
.channelId(TestBindingFactory.CHANNEL_ID)
4646
.messages(messages)
@@ -58,7 +58,7 @@ void scan() {
5858

5959
int methodsInClass = 2;
6060
verify(springAnnotationMessagesService)
61-
.buildMessages(any(), argThat(list -> list.size() == methodsInClass), any());
61+
.buildMessages(any(), any(), argThat(list -> list.size() == methodsInClass), any());
6262
}
6363

6464
@TestClassListener

springwolf-core/src/test/java/io/github/springwolf/core/asyncapi/scanners/channels/annotations/SpringAnnotationMethodLevelChannelsScannerIntegrationTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ static class TestBindingFactory implements BindingFactory<TestChannelListener> {
311311
Map.of(CHANNEL_ID, new TestBindingFactory.TestOperationBinding());
312312

313313
@Override
314-
public String getChannelName(TestChannelListener annotation) {
314+
public String getChannelName(TestChannelListener annotation, Class<?> component) {
315315
return CHANNEL;
316316
}
317317

springwolf-core/src/test/java/io/github/springwolf/core/asyncapi/scanners/channels/annotations/SpringAnnotationMethodLevelChannelsScannerTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ void scan() {
4949
.build();
5050

5151
when(springAnnotationMessageService.buildMessage(any(), any(), any())).thenReturn(message);
52-
when(springAnnotationChannelService.buildChannel(any(), any())).thenReturn(expectedChannelItem);
52+
when(springAnnotationChannelService.buildChannel(any(), any(), any())).thenReturn(expectedChannelItem);
5353

5454
// when
5555
List<ChannelObject> channels = scanner.scan(ClassWithTestListenerAnnotation.class);

springwolf-core/src/test/java/io/github/springwolf/core/asyncapi/scanners/common/operation/SpringAnnotationOperationServiceTest.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class SpringAnnotationOperationServiceTest {
4040
@BeforeEach
4141
void setUp() {
4242
// when
43-
when(bindingFactory.getChannelId(any())).thenReturn(CHANNEL_ID);
43+
when(bindingFactory.getChannelId(any(), any())).thenReturn(CHANNEL_ID);
4444
doReturn(defaultOperationBinding).when(bindingFactory).buildOperationBinding(any());
4545
}
4646

@@ -59,8 +59,8 @@ void scan_componentHasTestListenerMethods() throws NoSuchMethodException {
5959
.thenReturn(messageObject);
6060

6161
// when
62-
Operation operations =
63-
springAnnotationOperationService.buildOperation(annotation, payloadSchemaName, headerSchema);
62+
Operation operations = springAnnotationOperationService.buildOperation(
63+
annotation, ClassWithTestListenerAnnotation.class, payloadSchemaName, headerSchema);
6464

6565
// then
6666
Operation expectedOperation = Operation.builder()

springwolf-core/src/test/java/io/github/springwolf/core/asyncapi/scanners/operations/annotations/SpringAnnotationClassLevelOperationsScannerTest.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,15 @@ class SpringAnnotationClassLevelOperationsScannerTest {
4242

4343
@BeforeEach
4444
void setUp() {
45-
when(bindingFactory.getChannelId(any())).thenReturn(CHANNEL_NAME_ID);
45+
when(bindingFactory.getChannelId(any(), any())).thenReturn(CHANNEL_NAME_ID);
4646
}
4747

4848
@Test
4949
void scan() {
5050
// given
5151
Operation operation = Operation.builder().build();
52-
when(springAnnotationOperationsService.buildOperation(any(), anySet())).thenReturn(operation);
52+
when(springAnnotationOperationsService.buildOperation(any(), any(), anySet()))
53+
.thenReturn(operation);
5354

5455
// when
5556
List<Map.Entry<String, Operation>> operations =
@@ -64,7 +65,8 @@ void scan() {
6465
void operationCustomizerIsCalled() {
6566
// given
6667
Operation operation = Operation.builder().build();
67-
when(springAnnotationOperationsService.buildOperation(any(), anySet())).thenReturn(operation);
68+
when(springAnnotationOperationsService.buildOperation(any(), any(), anySet()))
69+
.thenReturn(operation);
6870

6971
// when
7072
scanner.scan(ClassWithTestListenerAnnotation.class).toList();

springwolf-core/src/test/java/io/github/springwolf/core/asyncapi/scanners/operations/annotations/SpringAnnotationMethodLevelOperationsScannerTest.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,14 @@ class SpringAnnotationMethodLevelOperationsScannerTest {
4141

4242
@BeforeEach
4343
void setUp() {
44-
when(bindingFactory.getChannelId(any())).thenReturn(CHANNEL_ID);
44+
when(bindingFactory.getChannelId(any(), any())).thenReturn(CHANNEL_ID);
4545
}
4646

4747
@Test
4848
void scan_componentHasTestListenerMethods() {
4949
// given
5050
Operation operation = Operation.builder().build();
51-
when(springAnnotationOperationService.buildOperation(any(), any(), any()))
51+
when(springAnnotationOperationService.buildOperation(any(), any(), any(), any()))
5252
.thenReturn(operation);
5353

5454
// when
@@ -63,7 +63,7 @@ void scan_componentHasTestListenerMethods() {
6363
@Test
6464
void operationCustomizerIsCalled() { // given
6565
Operation operation = Operation.builder().build();
66-
when(springAnnotationOperationService.buildOperation(any(), any(), any()))
66+
when(springAnnotationOperationService.buildOperation(any(), any(), any(), any()))
6767
.thenReturn(operation);
6868

6969
// when

springwolf-plugins/springwolf-amqp-plugin/src/main/java/io/github/springwolf/plugins/amqp/asyncapi/scanners/bindings/AmqpBindingFactory.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ public AmqpBindingFactory(
2929
}
3030

3131
@Override
32-
public String getChannelName(RabbitListener annotation) {
32+
public String getChannelName(RabbitListener annotation, Class<?> component) {
3333
return RabbitListenerUtil.getChannelName(annotation, stringValueResolver);
3434
}
3535

3636
@Override
37-
public String getChannelId(RabbitListener annotation) {
37+
public String getChannelId(RabbitListener annotation, Class<?> component) {
3838
return RabbitListenerUtil.getChannelId(annotation, stringValueResolver);
3939
}
4040

springwolf-plugins/springwolf-jms-plugin/src/main/java/io/github/springwolf/plugins/jms/asyncapi/scanners/bindings/JmsBindingFactory.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class JmsBindingFactory implements BindingFactory<JmsListener> {
1717
private final StringValueResolver stringValueResolver;
1818

1919
@Override
20-
public String getChannelName(JmsListener annotation) {
20+
public String getChannelName(JmsListener annotation, Class<?> component) {
2121
return JmsListenerUtil.getChannelName(annotation, stringValueResolver);
2222
}
2323

0 commit comments

Comments
 (0)