private static Map<Serializable, Object> initInterceptorInstanceMap(InterceptionModel model, CreationalContext ctx, BeanManagerImpl manager, Set<InterceptionType> interceptionTypes) { Map<Serializable, Object> interceptorInstances = new HashMap<>(); for (InterceptorClassMetadata<?> interceptor : model.getAllInterceptors()) { if (interceptionTypes != null) { for (InterceptionType interceptionType : interceptionTypes) { if (interceptor.isEligible(interceptionType) && !interceptorInstances.containsKey(interceptor.getKey())) { interceptorInstances.put(interceptor.getKey(), interceptor.getInterceptorFactory().create(ctx, manager)); } } } else { interceptorInstances.put(interceptor.getKey(), interceptor.getInterceptorFactory().create(ctx, manager)); } } return WeldCollections.immutableMapView(interceptorInstances); }
public List<InterceptorMethodInvocation> buildInterceptorMethodInvocationsForConstructorInterception() { List<? extends InterceptorClassMetadata<?>> interceptorList = interceptionModel.getConstructorInvocationInterceptors(); List<InterceptorMethodInvocation> interceptorInvocations = new ArrayList<InterceptorMethodInvocation>(interceptorList.size()); for (InterceptorClassMetadata<?> metadata : interceptorList) { Object interceptorInstance = getInterceptorInstance(metadata); InterceptorInvocation invocation = metadata.getInterceptorInvocation(interceptorInstance, InterceptionType.AROUND_CONSTRUCT); interceptorInvocations.addAll(invocation.getInterceptorMethodInvocations()); } return ImmutableList.copyOf(interceptorInvocations); } }
public <T> T getInterceptorInstance(InterceptorClassMetadata<T> interceptorMetadata) { return cast(interceptorInstances.get(interceptorMetadata.getJavaClass())); }
@SuppressWarnings({ "unchecked", "rawtypes" }) private static Map<Class<?>, Object> initInterceptorInstanceMap(InterceptionModel model, CreationalContext ctx, BeanManagerImpl manager, Set<InterceptionType> interceptionTypes) { Map<Class<?>, Object> interceptorInstances = new HashMap<Class<?>, Object>(); for (InterceptorClassMetadata<?> interceptor : model.getAllInterceptors()) { if(interceptionTypes != null) { for (InterceptionType interceptionType : interceptionTypes) { if (interceptor.isEligible(interceptionType)) { interceptorInstances.put(interceptor.getJavaClass(), interceptor.getInterceptorFactory().create(ctx, manager)); } } } else { interceptorInstances.put(interceptor.getJavaClass(), interceptor.getInterceptorFactory().create(ctx, manager)); } } return immutableMapView(interceptorInstances); }
private void validateInterceptors(BeanManagerImpl beanManager, AbstractClassBean<?> classBean) { InterceptionModel interceptionModel = beanManager.getInterceptorModelRegistry().get(classBean.getAnnotated()); if (interceptionModel != null) { Set<? extends InterceptorClassMetadata<?>> interceptors = interceptionModel.getAllInterceptors(); if (interceptors.size() > 0) { boolean passivationCapabilityCheckRequired = beanManager.isPassivatingScope(classBean.getScope()); for (InterceptorClassMetadata<?> interceptorMetadata : interceptors) { if (interceptorMetadata.getInterceptorFactory() instanceof CdiInterceptorFactory<?> && passivationCapabilityCheckRequired) { CdiInterceptorFactory<?> cdiInterceptorFactory = (CdiInterceptorFactory<?>) interceptorMetadata.getInterceptorFactory(); Interceptor<?> interceptor = cdiInterceptorFactory.getInterceptor(); Bean<?> resolvedBean = beanManager.resolve(beanManager.getBeans(injectionPoint)); validateInterceptorDecoratorInjectionPointPassivationCapable(injectionPoint, resolvedBean, beanManager, classBean); if (interceptorMetadata.getInterceptorFactory() instanceof PlainInterceptorFactory<?>) { PlainInterceptorFactory<?> factory = (PlainInterceptorFactory<?>) interceptorMetadata.getInterceptorFactory(); Class<?> interceptorClass = interceptorMetadata.getJavaClass(); if (passivationCapabilityCheckRequired && !Reflections.isSerializable(interceptorClass)) { throw ValidatorLogger.LOG.passivatingBeanWithNonserializableInterceptor(this, interceptorClass.getName());
private InterceptorClassMetadata<?> getFirstNonPassivationCapableInterceptor() { for (InterceptorClassMetadata<?> interceptorMetadata : getBeanManager().getInterceptorModelRegistry().get(getAnnotated()).getAllInterceptors()) { if (!Reflections.isSerializable(interceptorMetadata.getJavaClass())) { return interceptorMetadata; } } return null; }
public List<InterceptorMethodInvocation> buildInterceptorMethodInvocations(Object instance, Method method, InterceptionType interceptionType) { List<? extends InterceptorClassMetadata<?>> interceptorList = interceptionModel.getInterceptors(interceptionType, method); List<InterceptorMethodInvocation> interceptorInvocations = new ArrayList<InterceptorMethodInvocation>(interceptorList.size()); for (InterceptorClassMetadata<?> interceptorMetadata : interceptorList) { interceptorInvocations.addAll(interceptorMetadata.getInterceptorInvocation(getInterceptorInstance(interceptorMetadata), interceptionType) .getInterceptorMethodInvocations()); } TargetClassInterceptorMetadata targetClassInterceptorMetadata = getInterceptionModel().getTargetClassInterceptorMetadata(); if (targetClassInterceptorMetadata != null && targetClassInterceptorMetadata.isEligible(interceptionType)) { interceptorInvocations .addAll(targetClassInterceptorMetadata.getInterceptorInvocation(instance, interceptionType).getInterceptorMethodInvocations()); } return ImmutableList.copyOf(interceptorInvocations); }
@Override public Object intercept(InterceptionType type, T instance, final InvocationContext ctx) { final org.jboss.weld.interceptor.spi.model.InterceptionType interceptionType = org.jboss.weld.interceptor.spi.model.InterceptionType.valueOf(type .name()); final List<InterceptorMethodInvocation> methodInvocations = interceptorMetadata.getInterceptorInvocation(instance, interceptionType) .getInterceptorMethodInvocations(); Set<Annotation> interceptorBindings = null; if (ctx instanceof org.jboss.weld.interceptor.WeldInvocationContext) { interceptorBindings = Reflections.<org.jboss.weld.interceptor.WeldInvocationContext> cast(ctx).getInterceptorBindings(); } try { /* * Calling Interceptor.intercept() may result in multiple interceptor method invocations (provided the interceptor class interceptor methods on * superclasses). This requires cooperation with InvocationContext. * * We use a wrapper InvocationContext for the purpose of executing the chain of interceptor methods of this interceptor. */ return new WeldInvocationContextImpl(ctx, methodInvocations, interceptorBindings, null).proceed(); } catch (RuntimeException e) { throw e; } catch (Exception e) { throw new WeldException(e); } }
private void initClassDeclaredEjbInterceptors() { Class<?>[] classDeclaredInterceptors = interceptorsApi.extractInterceptorClasses(annotatedType); boolean excludeClassLevelAroundConstructInterceptors = constructor.isAnnotationPresent(ExcludeClassInterceptors.class); if (classDeclaredInterceptors != null) { for (Class<?> clazz : classDeclaredInterceptors) { InterceptorClassMetadata<?> interceptor = reader.getPlainInterceptorMetadata(clazz); for (InterceptionType interceptionType : InterceptionType.values()) { if (excludeClassLevelAroundConstructInterceptors && interceptionType.equals(InterceptionType.AROUND_CONSTRUCT)) { /* * @ExcludeClassInterceptors suppresses @AroundConstruct interceptors defined on class level */ continue; } if (interceptor.isEligible(org.jboss.weld.interceptor.spi.model.InterceptionType.valueOf(interceptionType))) { builder.interceptGlobal(interceptionType, null, Collections.<InterceptorClassMetadata<?>>singleton(interceptor), null); } } } } }
@Override public boolean intercepts(InterceptionType type) { return interceptorMetadata.isEligible(org.jboss.weld.interceptor.spi.model.InterceptionType.valueOf(type.name())); }
private List<Interceptor<?>> extractCdiInterceptors(Collection<? extends InterceptorClassMetadata<?>> interceptorMetadatas) { // ignore interceptors which are not CDI interceptors ArrayList<Interceptor<?>> interceptors = new ArrayList<Interceptor<?>>(); for (InterceptorClassMetadata<?> interceptorMetadata : interceptorMetadatas) { InterceptorFactory<?> interceptorFactory = interceptorMetadata.getInterceptorFactory(); if (interceptorFactory instanceof CdiInterceptorFactory<?>) { CdiInterceptorFactory<?> cdiInterceptorFactory = (CdiInterceptorFactory<?>) interceptorFactory; interceptors.add(cdiInterceptorFactory.getInterceptor()); } } return interceptors; }
private void checkInterceptorBindings() { if (interceptorMetadata.isEligible(org.jboss.weld.interceptor.spi.model.InterceptionType.POST_CONSTRUCT) || interceptorMetadata.isEligible(org.jboss.weld.interceptor.spi.model.InterceptionType.PRE_DESTROY) || interceptorMetadata.isEligible(org.jboss.weld.interceptor.spi.model.InterceptionType.POST_ACTIVATE) || interceptorMetadata.isEligible(org.jboss.weld.interceptor.spi.model.InterceptionType.PRE_PASSIVATE)) { for (Annotation interceptorBindingType : interceptorBindingTypes) { Target target = interceptorBindingType.annotationType().getAnnotation(Target.class); if (target == null || hasInvalidTargetType(target.value())) { ReflectionLogger.LOG.lifecycleCallbackInterceptorWithInvalidBindingTarget(this, interceptorBindingType.annotationType().getName(), target != null ? Arrays.toString(target.value()) : "Target meta-annotation is not present"); } } } }
/** * * @return a unique key which might be used to identify a metadata instance */ default Serializable getKey() { return getJavaClass(); } }
@Override public Serializable getKey() { return key != null ? key : InterceptorClassMetadata.super.getKey(); }
private void validateInterceptors(BeanManagerImpl beanManager, AbstractClassBean<?> classBean) { InterceptionModel interceptionModel = beanManager.getInterceptorModelRegistry().get(classBean.getAnnotated()); if (interceptionModel != null) { Set<? extends InterceptorClassMetadata<?>> interceptors = interceptionModel.getAllInterceptors(); if (interceptors.size() > 0) { boolean passivationCapabilityCheckRequired = beanManager.isPassivatingScope(classBean.getScope()); for (InterceptorClassMetadata<?> interceptorMetadata : interceptors) { if (interceptorMetadata.getInterceptorFactory() instanceof CdiInterceptorFactory<?> && passivationCapabilityCheckRequired) { CdiInterceptorFactory<?> cdiInterceptorFactory = (CdiInterceptorFactory<?>) interceptorMetadata.getInterceptorFactory(); Interceptor<?> interceptor = cdiInterceptorFactory.getInterceptor(); Bean<?> resolvedBean = beanManager.resolve(beanManager.getBeans(injectionPoint)); validateInterceptorDecoratorInjectionPointPassivationCapable(injectionPoint, resolvedBean, beanManager, classBean); if (interceptorMetadata.getInterceptorFactory() instanceof PlainInterceptorFactory<?>) { PlainInterceptorFactory<?> factory = (PlainInterceptorFactory<?>) interceptorMetadata.getInterceptorFactory(); Class<?> interceptorClass = interceptorMetadata.getJavaClass(); if (passivationCapabilityCheckRequired && !Reflections.isSerializable(interceptorClass)) { throw ValidatorLogger.LOG.passivatingBeanWithNonserializableInterceptor(this, interceptorClass.getName());
private InterceptorClassMetadata<?> getFirstNonPassivationCapableInterceptor() { for (InterceptorClassMetadata<?> interceptorMetadata : getBeanManager().getInterceptorModelRegistry().get(getAnnotated()).getAllInterceptors()) { if (!Reflections.isSerializable(interceptorMetadata.getJavaClass())) { return interceptorMetadata; } } return null; }
public List<InterceptorMethodInvocation> buildInterceptorMethodInvocations(Object instance, Method method, InterceptionType interceptionType) { List<? extends InterceptorClassMetadata<?>> interceptorList = interceptionModel.getInterceptors(interceptionType, method); List<InterceptorMethodInvocation> interceptorInvocations = new ArrayList<InterceptorMethodInvocation>(interceptorList.size()); for (InterceptorClassMetadata<?> interceptorMetadata : interceptorList) { interceptorInvocations.addAll(interceptorMetadata.getInterceptorInvocation(getInterceptorInstance(interceptorMetadata), interceptionType) .getInterceptorMethodInvocations()); } TargetClassInterceptorMetadata targetClassInterceptorMetadata = getInterceptionModel().getTargetClassInterceptorMetadata(); if (targetClassInterceptorMetadata != null && targetClassInterceptorMetadata.isEligible(interceptionType)) { interceptorInvocations .addAll(targetClassInterceptorMetadata.getInterceptorInvocation(instance, interceptionType).getInterceptorMethodInvocations()); } return ImmutableList.copyOf(interceptorInvocations); }
@Override public Object intercept(InterceptionType type, T instance, final InvocationContext ctx) { final org.jboss.weld.interceptor.spi.model.InterceptionType interceptionType = org.jboss.weld.interceptor.spi.model.InterceptionType.valueOf(type .name()); final List<InterceptorMethodInvocation> methodInvocations = interceptorMetadata.getInterceptorInvocation(instance, interceptionType) .getInterceptorMethodInvocations(); Set<Annotation> interceptorBindings = null; if (ctx instanceof org.jboss.weld.interceptor.WeldInvocationContext) { interceptorBindings = Reflections.<org.jboss.weld.interceptor.WeldInvocationContext> cast(ctx).getInterceptorBindings(); } try { /* * Calling Interceptor.intercept() may result in multiple interceptor method invocations (provided the interceptor class interceptor methods on * superclasses). This requires cooperation with InvocationContext. * * We use a wrapper InvocationContext for the purpose of executing the chain of interceptor methods of this interceptor. */ return new WeldInvocationContextImpl(ctx, methodInvocations, interceptorBindings, null).proceed(); } catch (RuntimeException e) { throw e; } catch (Exception e) { throw new WeldException(e); } }
private void initClassDeclaredEjbInterceptors() { Class<?>[] classDeclaredInterceptors = interceptorsApi.extractInterceptorClasses(annotatedType); boolean excludeClassLevelAroundConstructInterceptors = constructor != null && constructor.isAnnotationPresent(ExcludeClassInterceptors.class); if (classDeclaredInterceptors != null) { for (Class<?> clazz : classDeclaredInterceptors) { InterceptorClassMetadata<?> interceptor = reader.getPlainInterceptorMetadata(clazz); for (InterceptionType interceptionType : InterceptionType.values()) { if (excludeClassLevelAroundConstructInterceptors && interceptionType.equals(InterceptionType.AROUND_CONSTRUCT)) { /* * @ExcludeClassInterceptors suppresses @AroundConstruct interceptors defined on class level */ continue; } if (interceptor.isEligible(org.jboss.weld.interceptor.spi.model.InterceptionType.valueOf(interceptionType))) { builder.interceptGlobal(interceptionType, null, Collections.<InterceptorClassMetadata<?>>singleton(interceptor), null); } } } } }