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;
}
}
}
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();
}
}
}
Aggregations