public Object intercept()

in webbeans-impl/src/main/java/org/apache/webbeans/component/InterceptorBean.java [130:195]


    public Object intercept(InterceptionType interceptionType, T instance, InvocationContext invocationContext)
    {
        try
        {
            if (InterceptionType.AROUND_INVOKE == interceptionType && aroundInvokeMethod != null)
            {
                return aroundInvokeMethod.invoke(instance, invocationContext);
            }

            Method[] interceptorMethods = getInterceptorMethods(interceptionType);
            if (interceptorMethods == null || interceptorMethods.length == 0)
            {
                // this very interceptor doesn't support this interception type.
                // this might happen for lifecycle callback methods
                // let's continue with the next interceptor
                return invocationContext.proceed();
            }
            else if (interceptorMethods.length == 1)
            {
                // directly invoke the interceptor method with the given InvocationContext
                if (interceptorMethods[0].getParameterTypes().length == 1)
                {
                    return interceptorMethods[0].invoke(instance, invocationContext);
                } // else it can be a @PostContruct void pc(); which shouldn't be called from here
                else
                {
                    return invocationContext.proceed();
                }
            }
            else
            {
                // otherwise we need to wrap the InvocationContext into an own temporary InvocationContext
                // which handles multiple interceptor methods at a time
                if (invocationContext instanceof MultiMethodInvocationContext)
                {
                    // this happens while we recurse through the interceptors which have multiple interceptor-methods
                    MultiMethodInvocationContext mmInvocationContext = (MultiMethodInvocationContext) invocationContext;
                    int methodIndex = mmInvocationContext.getCurrentInterceptorIdx();
                    if (methodIndex < (interceptorMethods.length -1))
                    {
                        return interceptorMethods[methodIndex].invoke(instance, invocationContext);
                    }
                    else
                    {
                        return interceptorMethods[methodIndex].invoke(instance, mmInvocationContext.getWrapped());
                    }
                }
                else
                {
                    // We need to create the wrapper InvocationContext on the first time.
                    // This will internally walk through all the methods
                    MultiMethodInvocationContext mmInvocationContext
                            = new MultiMethodInvocationContext(invocationContext, interceptionType, instance, this);
                    return mmInvocationContext.proceed();
                }
            }
        }
        catch (InvocationTargetException ite)
        {
            throw ExceptionUtil.throwAsRuntimeException(ite.getCause());
        }
        catch (Exception e)
        {
            throw ExceptionUtil.throwAsRuntimeException(e);
        }
    }