public boolean doTask(final InjectionContext ctx) {
final IOCProcessingContext processingContext = ctx.getProcessingContext();
final InjectableInstance injectableInstance = getInjectableInstance(ctx);
final QualifyingMetadata qualifyingMetadata = processingContext.getQualifyingMetadataFactory()
.createFrom(injectableInstance.getQualifiers());
final Statement val;
ctx.allowProxyCapture();
switch (taskType) {
case Type:
ctx.getQualifiedInjector(type, qualifyingMetadata);
break;
case PrivateField:
ctx.addExposedField(field, PrivateAccessType.Write);
case Field: {
final Statement beanRefStmt = ctx.getBeanReference(injector.getInjectedType());
final Statement fieldAccessStmt;
if (field.isStatic()) {
throw new InjectionFailure("attempt to inject bean into a static field: "
+ field.getDeclaringClass().getFullyQualifiedName() + "." + field.getName());
}
else {
fieldAccessStmt = InjectUtil.setPublicOrPrivateFieldValue(ctx, beanRefStmt, field, Refs.get("bean"));
}
try {
val = AsyncInjectUtil.getInjectorOrProxy(ctx, getInjectableInstance(ctx), field.getType(), qualifyingMetadata,
new AsyncInjectorResolveCallback() {
@Override
public void onResolved(final Injector resolvedInjector) {
generateCallback(InjectUtil.getVarNameFromType(resolvedInjector.getConcreteInjectedType(), field),
resolvedInjector.getInjectedType(), ctx, fieldAccessStmt,
Stmt.loadVariable("async").invoke("finish", Refs.get("this")));
}
});
}
catch (InjectionFailure e) {
throw UnsatisfiedDependenciesException.createWithSingleFieldFailure(field, field.getDeclaringClass(),
field.getType(), e.getMessage());
}
catch (UnproxyableClassException e) {
final String err = "your object graph may have cyclical dependencies and the cycle could not be proxied. " +
"use of the @Dependent scope and @New qualifier may not " +
"produce properly initalized objects for: " + getInjector().getInjectedType().getFullyQualifiedName() + "\n" +
"\t Offending node: " + toString() + "\n" +
"\t Note : this issue can be resolved by making "
+ e.getUnproxyableClass().getFullyQualifiedName() + " proxyable. Introduce a default" +
" no-arg constructor and make sure the class is non-final.";
throw UnsatisfiedDependenciesException.createWithSingleFieldFailure(field, field.getDeclaringClass(),
field.getType(), err);
}
processingContext.append(val);
break;
}
case PrivateMethod:
ctx.addExposedMethod(method);
case Method: {
for (final MetaParameter parm : method.getParameters()) {
ctx.getProcessingContext().handleDiscoveryOfType(
InjectableInstance.getParameterInjectedInstance(parm, injector, ctx), parm.getType());
}
final AtomicInteger atomicInteger = new AtomicInteger(0);
final Statement[] args = AsyncInjectUtil.resolveInjectionDependencies(method.getParameters(), ctx, method,
new AsyncInjectorResolveCallback() {
@Override
public void onResolved(final Injector resolvedInjector) {
generateCallback(
InjectUtil.getVarNameFromType(resolvedInjector.getConcreteInjectedType(),
method.getParameters()[atomicInteger.getAndIncrement()]),
resolvedInjector.getInjectedType(),
ctx,
Stmt.loadVariable("async").invoke("finish", Refs.get("this"), Refs.get("bean")));
}
});
final Statement beanRef = ctx.getBeanReference(method.getDeclaringClass());
final Statement methodCallStatement = InjectUtil.invokePublicOrPrivateMethod(ctx,
beanRef,
method,
args);
final Statement finishCallback = Stmt.newObject(Runnable.class).extend()
.publicOverridesMethod("run")
.append(methodCallStatement)
.finish()
.finish();