private InterceptorData createInterceptorData(final Interceptor<?> i) { final InterceptorData data; if (CdiInterceptorBean.class.isInstance(i)) { final CdiInterceptorBean cdiInterceptorBean = CdiInterceptorBean.class.cast(i); data = new InterceptorData(i.getBeanClass()); data.getAroundInvoke().addAll(getInterceptionMethodAsListOrEmpty(cdiInterceptorBean, InterceptionType.AROUND_INVOKE)); data.getPostConstruct().addAll(getInterceptionMethodAsListOrEmpty(cdiInterceptorBean, InterceptionType.POST_CONSTRUCT)); data.getPreDestroy().addAll(getInterceptionMethodAsListOrEmpty(cdiInterceptorBean, InterceptionType.PRE_DESTROY)); data.getPostActivate().addAll(getInterceptionMethodAsListOrEmpty(cdiInterceptorBean, InterceptionType.POST_ACTIVATE)); data.getPrePassivate().addAll(getInterceptionMethodAsListOrEmpty(cdiInterceptorBean, InterceptionType.PRE_PASSIVATE)); data.getAroundTimeout().addAll(getInterceptionMethodAsListOrEmpty(cdiInterceptorBean, InterceptionType.AROUND_TIMEOUT)); /* AfterBegin, BeforeCompletion and AfterCompletion are ignored since not handled by CDI */ } else { // TODO: here we are not as good as in previous since we loose inheritance for instance data = InterceptorData.scan(i.getBeanClass()); } return data; }
@SuppressWarnings("unchecked") private <T> Object processInterceptorChain( final List<Interceptor<?>> chain, final Object target, final Method method, final Object[] args, final ContextFactory contextFactory, final InterceptorChain.ChainEnd<T> next, final InterceptionType interceptionType ) throws Throwable { if (chain.isEmpty()) { return next.invoke(); } final Interceptor<Object> it = (Interceptor<Object>) chain.remove(0); final Releaser releaser = new Releaser(); try { return intercept( it, it.create(contextFactory.create(it, releaser)), target, method, args, () -> processInterceptorChain(chain, target, method, args, contextFactory, next, interceptionType), interceptionType ); } finally { releaser.release(); } }
private <T> Object intercept( final Interceptor<T> it, final T instance, final Object target, final Method method, final Object[] args, final Proceed proceed, final InterceptionType interceptionType ) throws Exception { return it.intercept( interceptionType, instance, context(target, interceptionType == InterceptionType.AROUND_INVOKE ? method : null, args, proceed) ); }
@Override public List<Interceptor<?>> resolveInterceptors(final InterceptionType type, final Annotation... interceptorBindings) { final List<Interceptor<?>> interceptors = super.resolveInterceptors(type, interceptorBindings); final List<Interceptor<?>> parentInterceptors = getParentBm().resolveInterceptors(type, interceptorBindings); for (final Interceptor<?> i : parentInterceptors) { if (!interceptors.contains(i)) { interceptors.add(i); } } return interceptors; }
@Override public Object invoke(Object proxy, Method method, Object[] parameters) throws Throwable { // check if interceptors are defined, otherwise just call the original logik List<Interceptor<?>> interceptors = interceptorLookup.lookup(proxy, method); if (interceptors != null && !interceptors.isEmpty()) { try { DeltaSpikeProxyInvocationContext invocationContext = new DeltaSpikeProxyInvocationContext( this, beanManager, interceptors, proxy, method, parameters, null); Object returnValue = invocationContext.proceed(); if (invocationContext.isProceedOriginal()) { return invocationContext.getProceedOriginalReturnValue(); } return returnValue; } catch (DeltaSpikeProxyInvocationWrapperException e) { throw e.getCause(); } } return proceed(proxy, method, parameters); }
public DeltaSpikeProxyInvocationContext(DeltaSpikeProxyInvocationHandler invocationHandler, BeanManager beanManager, List<Interceptor<H>> interceptors, T target, Method method, Object[] parameters, Object timer) { super(target, method, parameters, timer); this.invocationHandler = invocationHandler; this.interceptors = interceptors; this.beanManager = beanManager; this.interceptorIndex = 0; }
public List<Interceptor<?>> lookup(Object instance, Method method) { List<Interceptor<?>> interceptors = cache.get(method); if (interceptors == null) { interceptors = resolveInterceptors(instance, method); cache.put(method, interceptors); } return interceptors; }
private List<Interceptor<?>> resolveInterceptors(Object instance, Method method) { BeanManager beanManager = BeanManagerProvider.getInstance().getBeanManager(); Annotation[] interceptorBindings = extractInterceptorBindings(beanManager, instance, method); if (interceptorBindings.length > 0) { return beanManager.resolveInterceptors(InterceptionType.AROUND_INVOKE, interceptorBindings); } return new ArrayList<Interceptor<?>>(); }
/** * Listener to ProcessBean event to locate handlers. * * @param processBean current {@link AnnotatedType} * @param beanManager Activated Bean Manager * @throws TypeNotPresentException if any of the actual type arguments refers to a non-existent type declaration * when trying to obtain the actual type arguments from a * {@link java.lang.reflect.ParameterizedType} * @throws java.lang.reflect.MalformedParameterizedTypeException * if any of the actual type parameters refer to a parameterized type that cannot * be instantiated for any reason when trying to obtain the actual type arguments * from a {@link java.lang.reflect.ParameterizedType} */ @SuppressWarnings("UnusedDeclaration") public <T> void findHandlers(@Observes final ProcessBean<?> processBean, final BeanManager beanManager) { if (!isActivated) { return; } if (processBean.getBean() instanceof Interceptor || processBean.getBean() instanceof Decorator || !(processBean.getAnnotated() instanceof AnnotatedType)) { return; } AnnotatedType annotatedType = (AnnotatedType)processBean.getAnnotated(); if (annotatedType.getJavaClass().isAnnotationPresent(ExceptionHandler.class)) { final Set<AnnotatedMethod<? super T>> methods = annotatedType.getMethods(); for (AnnotatedMethod<? super T> method : methods) { if (HandlerMethodImpl.isHandler(method)) { if (method.getJavaMember().getExceptionTypes().length != 0) { processBean.addDefinitionError(new IllegalArgumentException( String.format("Handler method %s must not throw exceptions", method.getJavaMember()))); } //beanManager won't be stored in the instance -> no issue with wls12c registerHandlerMethod(new HandlerMethodImpl(processBean.getBean(), method, beanManager)); } } } }
@Test public void testDisabledInterceptor() { List<Interceptor<?>> interceptors = weld.getBeanManager().resolveInterceptors(InterceptionType.AROUND_INVOKE, FooBinding.Literal.INSTANCE); assertEquals(1, interceptors.size()); assertEquals(MockInterceptor.class, interceptors.get(0).getBeanClass()); }
@Override public List<Interceptor<?>> resolveInterceptors(InterceptionType type, Annotation... interceptorBindings) { // TODO Auto-generated method stub return null; }
@Override public List<Interceptor<?>> resolveInterceptors( final InterceptionType arg0, final Annotation... arg1) { return this.beanManager.resolveInterceptors(arg0, arg1); }
private boolean isEjbInterceptor(final Interceptor<?> pc) { final Set<Annotation> interceptorBindings = pc.getInterceptorBindings(); return interceptorBindings == null || interceptorBindings.isEmpty(); }