Search in sources :

Example 1 with Interceptor

use of io.micronaut.aop.Interceptor in project micronaut-core by micronaut-projects.

the class DefaultInterceptorRegistry method resolveInterceptors.

@Override
@NonNull
public <T> Interceptor<T, ?>[] resolveInterceptors(@NonNull Executable<T, ?> method, @NonNull Collection<BeanRegistration<Interceptor<T, ?>>> interceptors, @NonNull InterceptorKind interceptorKind) {
    final AnnotationMetadata annotationMetadata = method.getAnnotationMetadata();
    if (interceptors.isEmpty()) {
        return resolveToNone((ExecutableMethod<?, ?>) method, interceptorKind, annotationMetadata);
    } else {
        instrumentAnnotationMetadata(beanContext, method);
        final Collection<AnnotationValue<?>> applicableBindings = AbstractInterceptorChain.resolveInterceptorValues(annotationMetadata, interceptorKind);
        if (applicableBindings.isEmpty()) {
            return resolveToNone((ExecutableMethod<?, ?>) method, interceptorKind, annotationMetadata);
        } else {
            @SuppressWarnings({ "unchecked", "rawtypes" }) final Interceptor[] resolvedInterceptors = (Interceptor[]) interceptorStream(method.getDeclaringType(), (Collection) interceptors, interceptorKind, applicableBindings).filter(bean -> (bean instanceof MethodInterceptor) || !(bean instanceof ConstructorInterceptor)).toArray(Interceptor[]::new);
            if (LOG.isTraceEnabled()) {
                LOG.trace("Resolved {} {} interceptors out of a possible {} for method: {} - {}", resolvedInterceptors.length, interceptorKind, interceptors.size(), method.getDeclaringType(), method instanceof Described ? ((Described) method).getDescription(true) : method.toString());
                for (int i = 0; i < resolvedInterceptors.length; i++) {
                    Interceptor<?, ?> resolvedInterceptor = resolvedInterceptors[i];
                    LOG.trace("Interceptor {} - {}", i, resolvedInterceptor);
                }
            }
            // noinspection unchecked
            return resolvedInterceptors;
        }
    }
}
Also used : Described(io.micronaut.core.naming.Described) MethodInterceptor(io.micronaut.aop.MethodInterceptor) ConstructorInterceptor(io.micronaut.aop.ConstructorInterceptor) AnnotationValue(io.micronaut.core.annotation.AnnotationValue) Collection(java.util.Collection) MethodInterceptor(io.micronaut.aop.MethodInterceptor) ConstructorInterceptor(io.micronaut.aop.ConstructorInterceptor) Interceptor(io.micronaut.aop.Interceptor) AnnotationMetadata(io.micronaut.core.annotation.AnnotationMetadata) NonNull(io.micronaut.core.annotation.NonNull)

Example 2 with Interceptor

use of io.micronaut.aop.Interceptor in project micronaut-core by micronaut-projects.

the class AbstractInterceptorChain method resolveInterceptorValues.

/**
 * Resolve interceptor binding for the given annotation metadata and kind.
 * @param annotationMetadata The annotation metadata
 * @param kind The kind
 * @return The binding
 * @since 3.3.0
 */
@NonNull
protected static Collection<AnnotationValue<?>> resolveInterceptorValues(@NonNull AnnotationMetadata annotationMetadata, @NonNull InterceptorKind kind) {
    if (annotationMetadata instanceof AnnotationMetadataHierarchy) {
        final List<AnnotationValue<InterceptorBinding>> declaredValues = annotationMetadata.getDeclaredMetadata().getAnnotationValuesByType(InterceptorBinding.class);
        final List<AnnotationValue<InterceptorBinding>> parentValues = ((AnnotationMetadataHierarchy) annotationMetadata).getRootMetadata().getAnnotationValuesByType(InterceptorBinding.class);
        if (CollectionUtils.isNotEmpty(declaredValues) || CollectionUtils.isNotEmpty(parentValues)) {
            Set<AnnotationValue<?>> resolved = new HashSet<>(declaredValues.size() + parentValues.size());
            Set<String> declared = new HashSet<>(declaredValues.size());
            for (AnnotationValue<InterceptorBinding> declaredValue : declaredValues) {
                final String annotationName = declaredValue.stringValue().orElse(null);
                if (annotationName != null) {
                    final InterceptorKind specifiedkind = declaredValue.enumValue("kind", InterceptorKind.class).orElse(null);
                    if (specifiedkind == null || specifiedkind.equals(kind)) {
                        if (!annotationMetadata.isRepeatableAnnotation(annotationName)) {
                            declared.add(annotationName);
                        }
                        resolved.add(declaredValue);
                    }
                }
            }
            for (AnnotationValue<InterceptorBinding> parentValue : parentValues) {
                final String annotationName = parentValue.stringValue().orElse(null);
                if (annotationName != null && !declared.contains(annotationName)) {
                    final InterceptorKind specifiedkind = parentValue.enumValue("kind", InterceptorKind.class).orElse(null);
                    if (specifiedkind == null || specifiedkind.equals(kind)) {
                        resolved.add(parentValue);
                    }
                }
            }
            return resolved;
        } else {
            return Collections.emptyList();
        }
    } else {
        List<AnnotationValue<InterceptorBinding>> bindings = annotationMetadata.getAnnotationValuesByType(InterceptorBinding.class);
        if (CollectionUtils.isNotEmpty(bindings)) {
            return bindings.stream().filter(av -> {
                if (!av.stringValue().isPresent()) {
                    return false;
                }
                final InterceptorKind specifiedkind = av.enumValue("kind", InterceptorKind.class).orElse(null);
                return specifiedkind == null || specifiedkind.equals(kind);
            }).collect(Collectors.toSet());
        } else {
            return Collections.emptyList();
        }
    }
}
Also used : InterceptorBinding(io.micronaut.aop.InterceptorBinding) LoggerFactory(org.slf4j.LoggerFactory) InvocationContext(io.micronaut.aop.InvocationContext) Internal(io.micronaut.core.annotation.Internal) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) Nullable(io.micronaut.core.annotation.Nullable) Map(java.util.Map) Argument(io.micronaut.core.type.Argument) MutableConvertibleValues(io.micronaut.core.convert.value.MutableConvertibleValues) MutableArgumentValue(io.micronaut.core.type.MutableArgumentValue) Logger(org.slf4j.Logger) AnnotationMetadataHierarchy(io.micronaut.inject.annotation.AnnotationMetadataHierarchy) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) Collectors(java.util.stream.Collectors) InterceptorKind(io.micronaut.aop.InterceptorKind) NonNull(io.micronaut.core.annotation.NonNull) List(java.util.List) Interceptor(io.micronaut.aop.Interceptor) AnnotationValue(io.micronaut.core.annotation.AnnotationValue) CollectionUtils(io.micronaut.core.util.CollectionUtils) Optional(java.util.Optional) AnnotationMetadata(io.micronaut.core.annotation.AnnotationMetadata) Collections(java.util.Collections) InterceptorBinding(io.micronaut.aop.InterceptorBinding) InterceptorKind(io.micronaut.aop.InterceptorKind) AnnotationValue(io.micronaut.core.annotation.AnnotationValue) AnnotationMetadataHierarchy(io.micronaut.inject.annotation.AnnotationMetadataHierarchy) HashSet(java.util.HashSet) NonNull(io.micronaut.core.annotation.NonNull)

Aggregations

Interceptor (io.micronaut.aop.Interceptor)2 AnnotationMetadata (io.micronaut.core.annotation.AnnotationMetadata)2 AnnotationValue (io.micronaut.core.annotation.AnnotationValue)2 NonNull (io.micronaut.core.annotation.NonNull)2 Collection (java.util.Collection)2 ConstructorInterceptor (io.micronaut.aop.ConstructorInterceptor)1 InterceptorBinding (io.micronaut.aop.InterceptorBinding)1 InterceptorKind (io.micronaut.aop.InterceptorKind)1 InvocationContext (io.micronaut.aop.InvocationContext)1 MethodInterceptor (io.micronaut.aop.MethodInterceptor)1 Internal (io.micronaut.core.annotation.Internal)1 Nullable (io.micronaut.core.annotation.Nullable)1 MutableConvertibleValues (io.micronaut.core.convert.value.MutableConvertibleValues)1 Described (io.micronaut.core.naming.Described)1 Argument (io.micronaut.core.type.Argument)1 MutableArgumentValue (io.micronaut.core.type.MutableArgumentValue)1 CollectionUtils (io.micronaut.core.util.CollectionUtils)1 AnnotationMetadataHierarchy (io.micronaut.inject.annotation.AnnotationMetadataHierarchy)1 Collections (java.util.Collections)1 HashSet (java.util.HashSet)1