private void setupConstructorInterceptionInstantiator(InterceptionModel interceptionModel) { if (interceptionModel != null && interceptionModel.hasExternalConstructorInterceptors()) { setInstantiator(new ConstructorInterceptionInstantiator<>(getInstantiator(), interceptionModel, getType())); } }
interceptionModel = beanManager.getInterceptorModelRegistry().get(getType()); && (interceptionModel.hasExternalNonConstructorInterceptors() || interceptionModel.hasTargetClassInterceptors()); decorators = beanManager.resolveDecorators(getBean().getTypes(), getBean().getQualifiers()); getBeanManager().getContextId(), getInstantiator(), getBean(), decorators));
private CachedInterceptionChain getInterceptionChain(Object instance, Method method, InterceptionType interceptionType) { if (method != null) { CachedInterceptionChain cachedChain = cachedChains.get(method); if (cachedChain == null) { cachedChain = new CachedInterceptionChain(ctx.buildInterceptorMethodInvocations(instance, method, interceptionType), ctx.getInterceptionModel() .getMemberInterceptorBindings(method)); CachedInterceptionChain old = cachedChains.putIfAbsent(method, cachedChain); if (old != null) { cachedChain = old; } } return cachedChain; } return new CachedInterceptionChain(ctx.buildInterceptorMethodInvocations(instance, null, interceptionType), ctx.getInterceptionModel().getClassInterceptorBindings()); }
private Set<InterceptionModel> getInterceptionModelsForType(EnhancedAnnotatedType<T> type, BeanManagerImpl manager, Bean<?> bean) { // if the bean has decorators consider all methods as intercepted if (bean != null && !manager.resolveDecorators(bean.getTypes(), bean.getQualifiers()).isEmpty()) { return null; } SlimAnnotatedTypeStore store = manager.getServices().get(SlimAnnotatedTypeStore.class); Set<InterceptionModel> models = new HashSet<InterceptionModel>(); WeldCollections.addIfNotNull(models, manager.getInterceptorModelRegistry().get(type.slim())); for (SlimAnnotatedType<?> slimType : store.get(type.getJavaClass())) { WeldCollections.addIfNotNull(models, manager.getInterceptorModelRegistry().get(slimType)); } for (InterceptionModel model : models) { if (model.hasTargetClassInterceptors() && model.getTargetClassInterceptorMetadata().isEligible(InterceptionType.AROUND_INVOKE)) { // this means that all methods are intercepted // returning null here means that all methods will be overridden and will delegate to MethodHandler return null; } } return models; }
private InterceptorClassMetadata<?> getFirstNonPassivationCapableInterceptor() { for (InterceptorClassMetadata<?> interceptorMetadata : getBeanManager().getInterceptorModelRegistry().get(getAnnotated()).getAllInterceptors()) { if (!Reflections.isSerializable(interceptorMetadata.getJavaClass())) { return interceptorMetadata; } } return null; }
this.cache = ComputingCacheBuilder.newBuilder().<Key, Optional<InterceptionFactoryData<?>>> build((key) -> { ClassTransformer classTransformer = beanManager.getServices().get(ClassTransformer.class); UnbackedAnnotatedType<?> slimAnnotatedType = classTransformer.getUnbackedAnnotatedType(key.annotatedType, beanManager.getId(), id); new InterceptionModelInitializer(beanManager, enhancedAnnotatedType, Beans.getBeanConstructor(enhancedAnnotatedType), null).init(); InterceptionModel interceptionModel = beanManager.getInterceptorModelRegistry().get(slimAnnotatedType); && (interceptionModel.hasExternalNonConstructorInterceptors() || interceptionModel.hasTargetClassInterceptors()); Set<MethodSignature> interceptedMethodSignatures = new HashSet<MethodSignature>(); for (AnnotatedMethod<?> method : Beans.getInterceptableMethods(enhancedAnnotatedType)) { enhancedMethodSignatures.add(MethodSignatureImpl.of(method)); if (!interceptionModel.getInterceptors(InterceptionType.AROUND_INVOKE, method.getJavaMember()).isEmpty()) { interceptedMethodSignatures.add(MethodSignatureImpl.of(method)); InterceptedProxyFactory<?> proxyFactory = new InterceptedProxyFactory<>(beanManager.getContextId(), enhancedAnnotatedType.getJavaClass(), Collections.singleton(enhancedAnnotatedType.getJavaClass()), enhancedMethodSignatures, interceptedMethodSignatures, "" + idx);
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(); boolean isSerializable = (interceptor instanceof InterceptorImpl) ? ((InterceptorImpl<?>) interceptor).isSerializable() : Beans.isPassivationCapableDependency(interceptor); if (!isSerializable) { throw ValidatorLogger.LOG.passivatingBeanWithNonserializableInterceptor(classBean, interceptor); 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());
public void init() { initTargetClassInterceptors(); businessMethods = Beans.getInterceptableMethods(annotatedType); initEjbInterceptors(); initCdiInterceptors(); InterceptionModel interceptionModel = builder.build(); if (interceptionModel.getAllInterceptors().size() > 0 || hasSerializationOrInvocationInterceptorMethods) { if (annotatedType.isFinal()) { throw BeanLogger.LOG.finalBeanClassWithInterceptorsNotAllowed(annotatedType.getJavaClass()); } if (Reflections.isPrivate(constructor.getJavaMember())) { throw new DeploymentException(ValidatorLogger.LOG.notProxyablePrivateConstructor(annotatedType.getJavaClass().getName(), constructor, annotatedType.getJavaClass())); } manager.getInterceptorModelRegistry().put(annotatedType.slim(), interceptionModel); } }
@Override public <T> BasicInjectionTarget<T> createMessageDrivenInjectionTarget(EnhancedAnnotatedType<T> type, EjbDescriptor<T> d, BeanManagerImpl manager) { InternalEjbDescriptor<T> descriptor = InternalEjbDescriptor.of(d); EnhancedAnnotatedType<T> implementationClass = SessionBeans.getEjbImplementationClass(descriptor, manager, type); Instantiator<T> instantiator = null; if (type.equals(implementationClass)) { instantiator = new DefaultInstantiator<T>(type, null, manager); } else { // Session bean subclassed by the EJB container instantiator = SubclassedComponentInstantiator.forSubclassedEjb(type, implementationClass, null, manager); } InterceptionModel interceptionModel = manager.getInterceptorModelRegistry().get(type.slim()); if (interceptionModel != null) { if (interceptionModel.hasExternalNonConstructorInterceptors()) { instantiator = SubclassedComponentInstantiator .forInterceptedDecoratedBean(implementationClass, null, (AbstractInstantiator<T>) instantiator, manager); instantiator = new InterceptorApplyingInstantiator<>(instantiator, interceptionModel, type.slim()); } if (interceptionModel.hasExternalConstructorInterceptors()) { instantiator = new ConstructorInterceptionInstantiator<>(instantiator, interceptionModel, type.slim()); } } return BasicInjectionTarget.createDefault(type, null, manager, instantiator); }
JsonObjectBuilder beanBuilder = createBasicBeanJson(bean, probe); if (bean.getName() != null) { beanBuilder.add(NAME, bean.getName()); if (bean.getStereotypes() != null && !bean.getStereotypes().isEmpty()) { JsonArrayBuilder stereotypesBuilder = Json.arrayBuilder(); for (Class<?> stereotype : Components.getSortedProbeComponetCandidates(bean.getStereotypes())) { if (BeanKind.INTERCEPTOR.equals(kind) || BeanKind.DECORATOR.equals(kind) || bean.isAlternative()) { JsonObjectBuilder enablementBuilder = Json.objectBuilder(); AnnotationApiAbstraction annotationApi = beanManager.getServices().get(AnnotationApiAbstraction.class); Object priority = bean.getBeanClass().getAnnotation(annotationApi.PRIORITY_ANNOTATION_CLASS); if (priority != null) { Collection<BeanManagerImpl> beanManagers = Container.instance(beanManager).beanDeploymentArchives().values(); for (BeanManagerImpl manager : beanManagers) { ModuleEnablement enablement = manager.getEnabled(); if ((BeanKind.INTERCEPTOR.equals(kind) && enablement.isInterceptorEnabled(bean.getBeanClass())) || (BeanKind.DECORATOR.equals(kind) && enablement.isDecoratorEnabled(bean.getBeanClass())) || isSelectedAlternative(enablement, bean)) { bdasBuilder.add(createSimpleBdaJson(manager.getId())); Set<Annotation> classInterceptorBindings = interceptionModel.getClassInterceptorBindings(); if (!classInterceptorBindings.isEmpty()) { JsonArrayBuilder bindingsBuilder = Json.arrayBuilder();
protected Class<T> createEnhancedSubclass(EnhancedAnnotatedType<T> type, Bean<?> bean, BeanManagerImpl manager) { Set<InterceptionModel> models = getInterceptionModelsForType(type, manager, bean); Set<MethodSignature> enhancedMethodSignatures = new HashSet<MethodSignature>(); Set<MethodSignature> interceptedMethodSignatures = (models == null) ? enhancedMethodSignatures : new HashSet<MethodSignature>(); for (AnnotatedMethod<?> method : Beans.getInterceptableMethods(type)) { enhancedMethodSignatures.add(MethodSignatureImpl.of(method)); if (models != null) { for (InterceptionModel model : models) { if (!model.getInterceptors(InterceptionType.AROUND_INVOKE, method.getJavaMember()).isEmpty()) { interceptedMethodSignatures.add(MethodSignatureImpl.of(method)); break; } } } } Set<Type> types = null; if (bean == null) { types = Collections.<Type>singleton(type.getJavaClass()); } else { types = bean.getTypes(); } return new InterceptedSubclassFactory<T>(manager.getContextId(), type.getJavaClass(), types, bean, enhancedMethodSignatures, interceptedMethodSignatures).getProxyClass(); }
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); }
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); } }
private boolean isInterceptorMethod(Method method) { return ctx.getInterceptionModel().getTargetClassInterceptorMetadata().isInterceptorMethod(method); }
@Override public List<Interceptor<?>> getLifecycleInterceptors(InterceptionType interceptionType) { if (interceptionType == null) { throw BeanLogger.LOG.interceptionTypeNull(); } org.jboss.weld.interceptor.spi.model.InterceptionType internalInterceptionType = org.jboss.weld.interceptor.spi.model.InterceptionType.valueOf(interceptionType.name()); if (!internalInterceptionType.isLifecycleCallback()) { throw BeanLogger.LOG.interceptionTypeNotLifecycle(interceptionType.name()); } if (internalInterceptionType.equals(org.jboss.weld.interceptor.spi.model.InterceptionType.AROUND_CONSTRUCT)) { return extractCdiInterceptors(interceptionModel.getConstructorInvocationInterceptors()); } return extractCdiInterceptors(interceptionModel.getInterceptors(internalInterceptionType, null)); }
List<InterceptorMethodInvocation> chain = interceptionContext.buildInterceptorMethodInvocationsForConstructorInterception(); InvocationContext invocationContext = new WeldInvocationContextImpl(constructor.getJavaMember(), parameters, data, chain, model.getMemberInterceptorBindings(getConstructor())) {
private boolean initHasPostConstructCallback(InjectionTarget<T> producer) { if (producer instanceof BasicInjectionTarget<?>) { BasicInjectionTarget<?> weldProducer = (BasicInjectionTarget<?>) producer; final InterceptionModel interceptors = getInterceptors(); if (interceptors == null || interceptors.getInterceptors(InterceptionType.POST_CONSTRUCT, null).isEmpty()) { if (!weldProducer.getLifecycleCallbackInvoker().hasPostConstructCallback()) { return false; } } } // otherwise we assume there is a post construct callback, just to be safe return true; } }
@Override public Collection<Interceptor<?>> getAllInterceptors() { Set<? extends InterceptorClassMetadata<?>> interceptorMetadataSet = interceptionModel.getAllInterceptors(); return extractCdiInterceptors(interceptorMetadataSet); }
private CachedInterceptionChain getInterceptionChain(Object instance, Method method, InterceptionType interceptionType) { if (method != null) { CachedInterceptionChain cachedChain = cachedChains.get(method); if (cachedChain == null) { cachedChain = new CachedInterceptionChain(ctx.buildInterceptorMethodInvocations(instance, method, interceptionType), ctx.getInterceptionModel() .getMemberInterceptorBindings(method)); CachedInterceptionChain old = cachedChains.putIfAbsent(method, cachedChain); if (old != null) { cachedChain = old; } } return cachedChain; } return new CachedInterceptionChain(ctx.buildInterceptorMethodInvocations(instance, null, interceptionType), ctx.getInterceptionModel().getClassInterceptorBindings()); }
private Set<InterceptionModel> getInterceptionModelsForType(EnhancedAnnotatedType<T> type, BeanManagerImpl manager, Bean<?> bean) { // if the bean has decorators consider all methods as intercepted if (bean != null && !manager.resolveDecorators(bean.getTypes(), bean.getQualifiers()).isEmpty()) { return null; } SlimAnnotatedTypeStore store = manager.getServices().get(SlimAnnotatedTypeStore.class); Set<InterceptionModel> models = new HashSet<InterceptionModel>(); WeldCollections.addIfNotNull(models, manager.getInterceptorModelRegistry().get(type.slim())); for (SlimAnnotatedType<?> slimType : store.get(type.getJavaClass())) { WeldCollections.addIfNotNull(models, manager.getInterceptorModelRegistry().get(slimType)); } for (InterceptionModel model : models) { if (model.hasTargetClassInterceptors() && model.getTargetClassInterceptorMetadata().isEligible(InterceptionType.AROUND_INVOKE)) { // this means that all methods are intercepted // returning null here means that all methods will be overridden and will delegate to MethodHandler return null; } } return models; }