T instance = newInstance((CreationalContextImpl<T>) creationalContext);
if (proxyClass != null)
{
// apply interceptorInfo
InterceptorDecoratorProxyFactory pf = webBeansContext.getInterceptorDecoratorProxyFactory();
Map<Interceptor<?>,Object> interceptorInstances = new HashMap<Interceptor<?>, Object>();
// create EJB-style interceptors
for (Interceptor interceptorBean : interceptorInfo.getEjbInterceptors())
{
interceptorInstances.put(interceptorBean, interceptorBean.create(creationalContext));
}
// create CDI-style interceptors
for (Interceptor interceptorBean : interceptorInfo.getCdiInterceptors())
{
interceptorInstances.put(interceptorBean, interceptorBean.create(creationalContext));
}
// register the bean itself for self-interception
if (interceptorInfo.getSelfInterceptorBean() != null)
{
interceptorInstances.put(interceptorInfo.getSelfInterceptorBean(), instance);
}
T delegate = instance;
if (interceptorInfo.getDecorators() != null && !isDelegateInjection(creationalContext))
{
List<Decorator<?>> decorators = interceptorInfo.getDecorators();
Map<Decorator<?>, Object> instances = new HashMap<Decorator<?>, Object>();
CreationalContextImpl<T> creationalContextImpl = (CreationalContextImpl<T>)creationalContext;
for (int i = decorators.size(); i > 0; i--)
{
Decorator<?> decorator = decorators.get(i - 1);
creationalContextImpl.putDelegate(delegate);
Object decoratorInstance = decorator.create((CreationalContext) creationalContext);
instances.put(decorator, decoratorInstance);
delegate = pf.createProxyInstance(proxyClass, instance, new DecoratorHandler(interceptorInfo, instances, i - 1, instance));
}
}
InterceptorHandler interceptorHandler = new DefaultInterceptorHandler<T>(instance, delegate, methodInterceptors, interceptorInstances, beanPassivationId);
T proxyInstance = pf.createProxyInstance(proxyClass, instance, interceptorHandler);
instance = proxyInstance;
}
return instance;
}