Hi all,

I'm doing some tests using ApacheOpenWebBeans 1.1.7 as the CDI implementation.
I am using a Framework (frameworkdemoiselle.gov.br) that offers an
interceptor that handles exceptions thrown by the target bean.

A summary of this interceptor is written bellow:

=========================
@AroundInvoke
public Object manage(final InvocationContext ic) throws Exception {
        Object result = null;

        try {
                result = ic.proceed();

        } catch (Exception cause) {
                // Handle Exception here
                target = ic.getTarget();
                ...

        }
        return result;
}
=========================

The "Handle Exception" logic uses the target bean. The problem is that
the "ic.getTarget()" method returns null and i get a
NullPointerException.
I've done the same test using WELD and ic.getTarget() does not returns null.

Analyzing the source code of the class
"org.apache.webbeans.intercept.InvocationContextImpl" I see that it
destroys the target instance when it catches an exception.

=========================
public Object proceed() throws Exception
{
        try
        {
            if (type.equals(InterceptionType.AROUND_INVOKE))
            {
                return proceedAroundInvokes(interceptorDatas);
            }
            else if (type.equals(InterceptionType.AROUND_TIMEOUT))
            {
                return proceedAroundTimeouts(interceptorDatas);
            }
            return proceedCommonAnnots(interceptorDatas, type);

        }
        catch (InvocationTargetException ite)
        {
            target = null; // destroy target instance    <<=================

            // Try to provide the original exception to the interceptor stack,
            // not the InvocationTargetException from Method.invoke
            Throwable t = ite.getCause();
            if (t instanceof Exception)
            {
                throw (Exception) t;
            }
            throw ite;
        }
        catch (Exception e)
        {
            target = null; // destroy target instance <<=================

            throw e;
        }
}
=========================

My question is: Why we need to destroy the target instance when occurs
an Exception? Doing it so, the interceptor cannot get the target after
invoking proceed() method.

Thanks,

Thiago

Reply via email to