}
}
final String varName = InjectUtil.getUniqueVarName();
final Statement subscribeStatement;
if (local) {
subscribeStatement = Stmt.invokeStatic(ErraiBus.class, "get")
.invoke("subscribeLocal", svcName, injectableInstance.getValueStatement());
}
else {
subscribeStatement = Stmt.invokeStatic(ErraiBus.class, "get")
.invoke("subscribe", svcName, injectableInstance.getValueStatement());
}
final Statement declareVar = Stmt.declareFinalVariable(varName, Subscription.class, subscribeStatement);
final MetaClass destructionCallbackType =
parameterizedAs(DestructionCallback.class, typeParametersOf(injectableInstance.getEnclosingType()));
// register a destructor to unregister the service when the bean is destroyed.
final BlockBuilder<AnonymousClassStructureBuilder> destroyMeth
= ObjectBuilder.newInstanceOf(destructionCallbackType).extend()
.publicOverridesMethod("destroy", Parameter.of(injectableInstance.getEnclosingType(), "obj", true))
.append(Stmt.loadVariable(varName).invoke("remove"));
final Statement descrCallback = Stmt.create().loadVariable("context").invoke("addDestructionCallback",
Refs.get(injectableInstance.getInjector().getInstanceVarName()), destroyMeth.finish().finish());
return Arrays.asList(declareVar, descrCallback);
}