Skip to content

Commit

Permalink
(feat) Add support to parse Annotations from super classes
Browse files Browse the repository at this point in the history
When a class is inheriting annotated methods from a superclass, the code is now properly parsing those methods for annotations.

This development allows to parse annotations from abstract classes or other inheritance classes, where the annotations are defined in a common class.
  • Loading branch information
ctasada committed May 1, 2024
1 parent d844219 commit fdfe867
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.EmbeddedValueResolverAware;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.util.StringValueResolver;

Expand Down Expand Up @@ -58,7 +59,7 @@ protected Stream<MethodAndAnnotation<A>> getAnnotatedMethods(Class<?> type) {
Class<A> annotationClass = this.asyncAnnotationProvider.getAnnotation();
log.debug("Scanning class \"{}\" for @\"{}\" annotated methods", type.getName(), annotationClass.getName());

return Arrays.stream(type.getDeclaredMethods())
return Arrays.stream(ReflectionUtils.getAllDeclaredMethods(type))
.filter(method -> !method.isBridge())
.filter(method -> AnnotationScannerUtil.findAnnotation(annotationClass, method) != null)
.peek(method -> log.debug("Mapping method \"{}\" to channels", method.getName()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,43 @@ void scan_componentHasAsyncMethodAnnotation() {
.containsExactly(Map.entry("test-channel_send_methodWithAnnotation", expectedOperation));
}

@Test
void scan_componentHasAsyncMethodAnnotationInAbstractClass() {
// Given a class with methods annotated with AsyncListener, where only the channel-name is set
setClassToScan(ClassExtendsFromAbstractWithListenerAnnotation.class);

// When scan is called
Map<String, Operation> actualOperations = operationsScanner.scan();

// Then the returned collection contains the channel
MessagePayload payload = MessagePayload.of(MultiFormatSchema.builder()
.schema(SchemaReference.fromSchema(SimpleFoo.class.getSimpleName()))
.build());

MessageObject message = MessageObject.builder()
.messageId("simpleFoo")
.name("SimpleFooPayLoad")
.title("Message Title")
.description("Message description")
.payload(payload)
.headers(MessageHeaders.of(
MessageReference.toSchema(AsyncHeadersNotDocumented.NOT_DOCUMENTED.getTitle())))
.bindings(EMPTY_MAP)
.build();

Operation expectedOperation = Operation.builder()
.action(OperationAction.SEND)
.channel(ChannelReference.fromChannel("abstract-test-channel"))
.description("test abstract channel operation description")
.title("abstract-test-channel_send")
.bindings(EMPTY_MAP)
.messages(List.of(MessageReference.toChannelMessage("abstract-test-channel", message)))
.build();

assertThat(actualOperations)
.containsExactly(Map.entry("abstract-test-channel_send_methodWithAnnotation", expectedOperation));
}

private static class ClassWithoutListenerAnnotation {

private void methodWithoutAnnotation() {}
Expand Down Expand Up @@ -370,6 +407,25 @@ private void methodWithAnnotation(SimpleFoo payload) {}
private void methodWithoutAnnotation() {}
}

private static class ClassExtendsFromAbstractWithListenerAnnotation extends AbstractClassWithListenerAnnotation {}

private abstract static class AbstractClassWithListenerAnnotation {

@AsyncListener(
operation =
@AsyncOperation(
channelName = "abstract-test-channel",
description = "test abstract channel operation description",
message =
@AsyncMessage(
description = "Message description",
messageId = "simpleFooAbstract",
name = "SimpleFooPayLoad",
contentType = "application/json",
title = "Message Title")))
protected void methodWithAnnotation(SimpleFoo payload) {}
}

@Nested
class ImplementingInterface {
@ParameterizedTest
Expand Down

0 comments on commit fdfe867

Please sign in to comment.