/** Produces a new bean instance */ @Override public T produce(CreationalContext<T> cxt) { Class<?> type = _producerBean.getBeanClass(); // factory instance owns its own dependency chain; it's not one of the // context bean's dependencies. CreationalContextImpl<T> env = null; if (cxt instanceof CreationalContextImpl<?>) { env = (CreationalContextImpl<T>) cxt; } ProducesCreationalContext<X> factoryEnv = null; X factory = CreationalContextImpl.find(env, _producerBean); if (factory == null) { factoryEnv = new ProducesCreationalContext<X>(_producerBean, env); factory = getBeanManager().getReference(_producerBean, factoryEnv); } if (factory == null) { throw new IllegalStateException( L.l("{0}: unexpected null factory for {1}", this, _producerBean)); } T instance = produce(factory, env); if (env != null && _producerBean.getScope() == Dependent.class) { factoryEnv.release(); // _producerBean.destroy(factory, factoryEnv); } if (_isPassivating && !(instance instanceof Serializable)) throw new IllegalProductException( L.l( "'{0}' is an invalid @{1} instance because it's not serializable for bean {2}", instance, getScope().getSimpleName(), this)); return instance; }