{
MethodInvocation mi = (MethodInvocation)invocation;
KernelControllerContext context = (KernelControllerContext)mi.getArguments()[0];
Name name = (Name)invocation.resolveClassAnnotation(Name.class);
Scope scope = (Scope)invocation.resolveClassAnnotation(Scope.class);
ScopeType scopeType = scope != null ? scope.value() : null;
if ("setKernelControllerContext".equals(mi.getMethod().getName()) && name != null)
{
Object target = context.getTarget();
boolean unmockApplication = false;
if (!Contexts.isApplicationContextActive())
{
Lifecycle.mockApplication();
unmockApplication = true;
}
try
{
Contexts.getApplicationContext().set(
name + Initialization.COMPONENT_SUFFIX,
new MicrocontainerComponent(target.getClass(), name.value(), scopeType, context.getController())
);
}
finally
{
if (unmockApplication)
{
Lifecycle.unmockApplication();
}
}
}
else if (name != null && scopeType != null)
{
scopeType.getContext().remove(name.value());
}
return null;
}