Search in sources :

Example 1 with InterceptorBinding

use of io.micronaut.aop.InterceptorBinding 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)1 InterceptorBinding (io.micronaut.aop.InterceptorBinding)1 InterceptorKind (io.micronaut.aop.InterceptorKind)1 InvocationContext (io.micronaut.aop.InvocationContext)1 AnnotationMetadata (io.micronaut.core.annotation.AnnotationMetadata)1 AnnotationValue (io.micronaut.core.annotation.AnnotationValue)1 Internal (io.micronaut.core.annotation.Internal)1 NonNull (io.micronaut.core.annotation.NonNull)1 Nullable (io.micronaut.core.annotation.Nullable)1 MutableConvertibleValues (io.micronaut.core.convert.value.MutableConvertibleValues)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 Collection (java.util.Collection)1 Collections (java.util.Collections)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1