@Override
public Statement generateDecorator(InjectableInstance<Observes> instance) {
final Context ctx = instance.getInjectionContext().getProcessingContext().getContext();
final MetaMethod method = instance.getMethod();
final MetaParameter parm = instance.getParm();
if (!method.isPublic()) {
instance.ensureMemberExposed();
}
final String parmClassName = parm.getType().getFullyQualifiedName();
final Statement bus = instance.getInjectionContext().getInjector(MessageBus.class).getType(instance);
final String subscribeMethodName = method.isAnnotationPresent(Local.class) ? "subscribeLocal" : "subscribe";
final String subject = CDI.getSubjectNameByType(parmClassName);
final Annotation[] qualifiers = InjectUtil.extractQualifiers(instance).toArray(new Annotation[0]);
final List<String> qualifierNames = CDI.getQualifiersPart(qualifiers);
AnonymousClassStructureBuilder callBack = Stmt.newObject(AbstractCDIEventCallback.class).extend();
BlockBuilder<AnonymousClassStructureBuilder> callBackBlock;
if (qualifierNames != null) {
callBackBlock = callBack.initialize();
for (String qualifierName : qualifierNames) {
callBackBlock.append(Stmt.loadClassMember("qualifierSet").invoke("add", qualifierName));
}
callBack = callBackBlock.finish();
}
callBackBlock = callBack.publicOverridesMethod("callback", Parameter.of(Message.class, "message"))
.append(Stmt.declareVariable("msgQualifiers", new TypeLiteral<Set<String>>() {
},
Stmt.loadVariable("message").invoke("get", Set.class, CDIProtocol.Qualifiers)))
.append(Stmt
.if_(Bool.or(
Stmt.loadClassMember("qualifierSet").invoke("equals", Refs.get("msgQualifiers")),
Bool.and(Bool.equals(Refs.get("msgQualifiers"), null),
Stmt.loadClassMember("qualifierSet").invoke("isEmpty"))))
.append(instance.callOrBind(Stmt.loadVariable("message").invoke("get", parm.getType().asClass(), CDIProtocol.BeanReference)))
.finish());
return Stmt.create(ctx).nestedCall(bus).invoke(subscribeMethodName, subject, callBackBlock.finish().finish());
}