Search in sources :

Example 1 with DefaultBeanContext

use of io.micronaut.context.DefaultBeanContext in project micronaut-core by micronaut-projects.

the class ConstructorInterceptorChain method instantiate.

/**
 * Internal methods that handles the logic of instantiating a bean that has constructor interception applied.
 *
 * @param resolutionContext                         The resolution context
 * @param beanContext                               The bean context
 * @param interceptors                              The interceptors. Can be null and if so should be resolved from the context.
 * @param definition                                The definition
 * @param constructor                               The bean constructor
 * @param additionalProxyConstructorParametersCount The additional proxy constructor parameters count
 * @param parameters                                The resolved parameters
 * @param <T1>                                      The bean type
 * @return The instantiated bean
 * @since 3.0.0
 */
@Internal
@UsedByGeneratedCode
@NonNull
public static <T1> T1 instantiate(@NonNull BeanResolutionContext resolutionContext, @NonNull BeanContext beanContext, @Nullable List<BeanRegistration<Interceptor<T1, T1>>> interceptors, @NonNull BeanDefinition<T1> definition, @NonNull BeanConstructor<T1> constructor, int additionalProxyConstructorParametersCount, @NonNull Object... parameters) {
    if (interceptors == null) {
        final AnnotationMetadataHierarchy hierarchy = new AnnotationMetadataHierarchy(definition.getAnnotationMetadata(), constructor.getAnnotationMetadata());
        final Collection<AnnotationValue<?>> annotationValues = resolveInterceptorValues(hierarchy, InterceptorKind.AROUND_CONSTRUCT);
        final Collection<BeanRegistration<Interceptor<?, ?>>> resolved = ((DefaultBeanContext) beanContext).getBeanRegistrations(resolutionContext, Interceptor.ARGUMENT, Qualifiers.byInterceptorBindingValues(annotationValues));
        interceptors = new ArrayList(resolved);
    }
    final InterceptorRegistry interceptorRegistry = beanContext.getBean(InterceptorRegistry.ARGUMENT);
    final Interceptor<T1, T1>[] resolvedInterceptors = interceptorRegistry.resolveConstructorInterceptors(constructor, interceptors);
    return Objects.requireNonNull(new ConstructorInterceptorChain<T1>(definition, constructor, resolvedInterceptors, additionalProxyConstructorParametersCount, parameters).proceed(), "Constructor interceptor chain illegally returned null for constructor: " + constructor.getDescription());
}
Also used : BeanRegistration(io.micronaut.context.BeanRegistration) AnnotationValue(io.micronaut.core.annotation.AnnotationValue) DefaultBeanContext(io.micronaut.context.DefaultBeanContext) AnnotationMetadataHierarchy(io.micronaut.inject.annotation.AnnotationMetadataHierarchy) Internal(io.micronaut.core.annotation.Internal) NonNull(io.micronaut.core.annotation.NonNull) UsedByGeneratedCode(io.micronaut.core.annotation.UsedByGeneratedCode)

Example 2 with DefaultBeanContext

use of io.micronaut.context.DefaultBeanContext in project micronaut-core by micronaut-projects.

the class MethodInterceptorChain method doIntercept.

@SuppressWarnings({ "unchecked", "rawtypes" })
private static <T1> T1 doIntercept(BeanResolutionContext resolutionContext, BeanContext beanContext, BeanDefinition<T1> definition, ExecutableMethod<T1, T1> interceptedMethod, T1 bean, InterceptorKind kind) {
    final AnnotationMetadata annotationMetadata = interceptedMethod.getAnnotationMetadata();
    final Collection<AnnotationValue<?>> binding = resolveInterceptorValues(annotationMetadata, kind);
    final Collection<BeanRegistration<Interceptor<?, ?>>> resolved = ((DefaultBeanContext) beanContext).getBeanRegistrations(resolutionContext, Interceptor.ARGUMENT, Qualifiers.byInterceptorBindingValues(binding));
    final InterceptorRegistry interceptorRegistry = beanContext.getBean(InterceptorRegistry.ARGUMENT);
    final Interceptor[] resolvedInterceptors = interceptorRegistry.resolveInterceptors((ExecutableMethod) interceptedMethod, (Collection) resolved, kind);
    if (ArrayUtils.isNotEmpty(resolvedInterceptors)) {
        final MethodInterceptorChain<T1, T1> chain = new MethodInterceptorChain<T1, T1>(resolvedInterceptors, bean, interceptedMethod, kind);
        return Objects.requireNonNull(chain.proceed(), kind.name() + " interceptor chain illegal returned null for type: " + definition.getBeanType());
    } else {
        return interceptedMethod.invoke(bean);
    }
}
Also used : BeanRegistration(io.micronaut.context.BeanRegistration) AnnotationValue(io.micronaut.core.annotation.AnnotationValue) DefaultBeanContext(io.micronaut.context.DefaultBeanContext) AnnotationMetadata(io.micronaut.core.annotation.AnnotationMetadata)

Aggregations

BeanRegistration (io.micronaut.context.BeanRegistration)2 DefaultBeanContext (io.micronaut.context.DefaultBeanContext)2 AnnotationValue (io.micronaut.core.annotation.AnnotationValue)2 AnnotationMetadata (io.micronaut.core.annotation.AnnotationMetadata)1 Internal (io.micronaut.core.annotation.Internal)1 NonNull (io.micronaut.core.annotation.NonNull)1 UsedByGeneratedCode (io.micronaut.core.annotation.UsedByGeneratedCode)1 AnnotationMetadataHierarchy (io.micronaut.inject.annotation.AnnotationMetadataHierarchy)1