As described in WELD-2608, I'm adding some beans manually using AfterBeanDiscovery. As the observer methods of those beans are not registered automatically (currently if that issue is valid and not expected), I thought it should be easy to also register the observer methods like I registered the beans. So as I registered the beans using this:
afterBeanDiscovery
.addBean()
.read(annotatedBeanType)
.alternative(false)
I assumed adding the observer methods should be similar simple. My naive first approach was this, assuming all necessary information is determined by read as was the case for the bean:
annotatedBeanType
.methods
.findAll {
it.parameters.any {
it.isAnnotationPresent(Observes) ||
it.isAnnotationPresent(ObservesAsync)
}
}
.each { method ->
afterBeanDiscovery
.addObserverMethod()
.read(method)
}
But I found two problems with this, namely the async property and the the notify callback are not set. So what I currently have is:
annotatedBeanType
.methods
.findAll {
it.parameters.any {
it.isAnnotationPresent(Observes) ||
it.isAnnotationPresent(ObservesAsync)
}
}
.groupBy {
it.parameters.any {
it.isAnnotationPresent(ObservesAsync)
}
}
.each { async, methods ->
methods.each { method ->
afterBeanDiscovery
.addObserverMethod()
.read(method)
.async(async)
.notifyWith { eventContext ->
def cdi = CDI.current()
def beanType = annotatedBeanType.javaClass
def bean = cdi.select(beanType).get()
def arguments = method
.parameters
.collect {
if (it.isAnnotationPresent(Observes) || it.isAnnotationPresent(ObservesAsync)) {
eventContext.event
} else {
def qualifiers = it.annotations.findAll {
it.annotationType().isAnnotationPresent(Qualifier)
} as List
cdi.select(it.baseType, *qualifiers).get()
}
}
method.javaMember.invoke(bean, *arguments)
}
}
}
But I hope that this is a bug and the `read` call should both, determine and set the async property and also configure a proper notify callback, especially as my current variant depends on `CDI.current()` which if I understood properly only works if there is exactly one container running, which might not be the case if tests are executed in parallel. |