@Override
public List<? extends 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(PrivateAccessType.Write);
}
final String parmClassName = parm.getType().getFullyQualifiedName();
final Statement bus = instance.getInjectionContext().getInjector(MessageBus.class).getBeanInstance(instance);
final List<Annotation> annotations = InjectUtil.extractQualifiers(instance);
final Annotation[] qualifiers = annotations.toArray(new Annotation[annotations.size()]);
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", true))
._(Stmt.declareVariable("msgQualifiers", new TypeLiteral<Set<String>>() {
},
Stmt.loadVariable("message").invoke("get", Set.class, CDIProtocol.Qualifiers)))
._(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"))))
._(RunAsyncWrapper.wrap(instance.callOrBind(Stmt.loadVariable("message")
.invoke("get", parm.getType().asClass(), CDIProtocol.BeanReference))))
.finish()).finish()
.publicOverridesMethod("toString")
._(Stmt.load("Observer: " + parmClassName + " " + Arrays.toString(qualifiers)).returnValue());
final List<Statement> statements = new ArrayList<Statement>();
// create the destruction callback to deregister the service when the bean is destroyed.
final String subscrVar = InjectUtil.getUniqueVarName();
Statement subscribeStatement =
Stmt.declareVariable(Subscription.class).asFinal().named(subscrVar)
.initializeWith(Stmt.create(ctx).invokeStatic(CDI.class, "subscribe", parmClassName,
callBackBlock.finish().finish()));
statements.add(subscribeStatement);
// create the destruction callback to deregister the service when the bean is destroyed.
final MetaClass destructionCallbackType =
parameterizedAs(DestructionCallback.class, typeParametersOf(instance.getEnclosingType()));
final BlockBuilder<AnonymousClassStructureBuilder> destroyMeth
= ObjectBuilder.newInstanceOf(destructionCallbackType).extend()
.publicOverridesMethod("destroy", Parameter.of(instance.getEnclosingType(), "obj", true))
.append(Stmt.loadVariable(subscrVar).invoke("remove"));
for (Class<?> cls : EnvUtil.getAllPortableConcreteSubtypes(parm.getType().asClass())) {
final String subscrHandle = InjectUtil.getUniqueVarName();
statements.add(Stmt.declareVariable(Subscription.class).asFinal().named(subscrHandle)
.initializeWith(Stmt.nestedCall(bus).invoke("subscribe", CDI.getSubjectNameByType(cls.getName()),
Stmt.loadStatic(CDI.class, "ROUTING_CALLBACK"))));