GH-1014 - Pick up reference types for event listeners declared in annotations.

We now detect event types a listener is interested in declared in annotations for inclusion the reference documentation. This allows for the rare case that the event listener method not actually declaring the event type as parameter as it might not be needed in the payload but only the fact that an event was published at all.
This commit is contained in:
Oliver Drotbohm
2025-01-06 19:12:36 +01:00
parent 8192f1bb2d
commit 7e08447139
5 changed files with 96 additions and 7 deletions

View File

@@ -17,6 +17,7 @@ package org.springframework.modulith.docs;
import static org.springframework.util.ClassUtils.*;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.regex.Matcher;
@@ -295,15 +296,17 @@ class Asciidoctor {
return header + type.getReferenceMethods().map(it -> {
var method = it.getMethod();
Assert.isTrue(method.getRawParameterTypes().size() > 0,
() -> String.format("Method %s must have at least one parameter!", method));
var exposedReferenceTypes = it.getReferenceTypes().stream()
.filter(refType -> modules.getModuleByType(refType)
.map(module -> module.isExposed(refType))
.orElse(true))
.toList();
var parameterType = method.getRawParameterTypes().get(0);
var isAsync = it.isAsync() ? "(async) " : "";
return docSource.flatMap(source -> source.getDocumentation(method))
.map(doc -> String.format("** %s %s-- %s", toInlineCode(parameterType), isAsync, doc))
.orElseGet(() -> String.format("** %s %s", toInlineCode(parameterType), isAsync));
.map(doc -> String.format("** %s %s-- %s", toInlineCode(exposedReferenceTypes), isAsync, doc))
.orElseGet(() -> String.format("** %s %s", toInlineCode(exposedReferenceTypes), isAsync));
}).collect(Collectors.joining("\n"));
}
@@ -317,6 +320,14 @@ class Asciidoctor {
.collect(Collectors.joining(", "));
}
private String toInlineCode(Collection<Class<?>> types) {
return types.stream()
.map(Class::getName)
.map(this::toInlineCode)
.collect(Collectors.joining(", "));
}
private static String toLink(String source, String href) {
return String.format("link:%s[%s]", href, source);
}