Gerald Turner commented on Bug WELD-1280

I traced the example PhaseListener (and MyFaces ExtVal startup) thru a debugger and found whats going on.

Weld has an ELCreationalContext which works with a stack, ValueExpressions always push/pop the stack, whereas ELResolver expects the stack to have already been pushed in the case of Dependent scoped objects.

Excerpts from org.jboss.weld.el.AbstractWeldELResolver:

    private Object lookup(BeanManagerImpl beanManager, ELContext context, String name) {
        final Bean<?> bean = beanManager.resolve(beanManager.getBeans(name));
        if (bean == null) {
            return null;
        }
        Class<? extends Annotation> scope = bean.getScope();
        if (!scope.equals(Dependent.class)) {
            ClientProxyProvider cpp = beanManager.getClientProxyProvider();
            if (cpp != null) {
                Object value = cpp.getClientProxy(bean);
                if (value != null) {
                    return value;
                }
            }
            return beanManager.getReference(bean, beanManager.createCreationalContext(bean), false);
        } else {
            // Need to use a "special" creationalContext that can make sure that we do share dependent instances referenced by the EL Expression
            final ELCreationalContext<?> creationalContext = getELCreationalContext(context);
            String beanName = bean.getName();
            Object value = creationalContext.getDependentInstanceForExpression(beanName);
            if (value == null) {
                value = getManager(context).getReference(bean, creationalContext, false);
                creationalContext.registerDependentInstanceForExpression(beanName, value);
            }
            return value;
        }
    }

    private ELCreationalContext<?> getELCreationalContext(ELContext context) {
        ELCreationalContextStack stack = ELCreationalContextStack.getCreationalContextStore(context);
        if (!stack.isEmpty()) {
            return stack.peek().get();
        } else {
            throw new IllegalStateException("No CreationalContext registered for EL evaluation, it is likely that the the expression factory has not been wrapped by the CDI BeanManager, which must be done to use the ELResolver from CDI");
        }
    }

Excerpts from org.jboss.weld.el.WeldValueExpression:

    @Override
    public Object getValue(final ELContext context) {
        ELCreationalContextStack store = getCreationalContextStore(context);
        try {
            store.push(new CreationalContextCallable());
            return delegate().getValue(context);
        } finally {
            CreationalContextCallable callable = store.pop();
            if (callable.exists()) {
                callable.get().release();
            }
        }
    }
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira