Search in sources :

Example 16 with AnnotationValue

use of io.micronaut.core.annotation.AnnotationValue in project micronaut-micrometer by micronaut-projects.

the class TimedInterceptor method intercept.

@Override
public Object intercept(MethodInvocationContext<Object, Object> context) {
    final AnnotationMetadata metadata = context.getAnnotationMetadata();
    final AnnotationValue<TimedSet> timedSet = metadata.getAnnotation(TimedSet.class);
    if (timedSet != null) {
        final List<AnnotationValue<Timed>> timedAnnotations = timedSet.getAnnotations(VALUE_MEMBER, Timed.class);
        if (!timedAnnotations.isEmpty()) {
            String exceptionClass = "none";
            List<Timer.Sample> syncInvokeSamples = null;
            InterceptedMethod interceptedMethod = InterceptedMethod.of(context);
            try {
                InterceptedMethod.ResultType resultType = interceptedMethod.resultType();
                switch(resultType) {
                    case PUBLISHER:
                        Object interceptResult = context.proceed();
                        if (interceptResult == null) {
                            return null;
                        }
                        Object result;
                        AtomicReference<List<Timer.Sample>> reactiveInvokeSample = new AtomicReference<>();
                        if (context.getReturnType().isSingleResult()) {
                            Mono<?> single = Mono.from(Publishers.convertPublisher(interceptResult, Publisher.class));
                            result = single.doOnSubscribe(d -> reactiveInvokeSample.set(initSamples(timedAnnotations))).doOnError(throwable -> finalizeSamples(timedAnnotations, throwable.getClass().getSimpleName(), reactiveInvokeSample.get())).doOnSuccess(o -> finalizeSamples(timedAnnotations, "none", reactiveInvokeSample.get()));
                        } else {
                            AtomicReference<String> exceptionClassHolder = new AtomicReference<>("none");
                            Flux<?> flowable = Flux.from(Publishers.convertPublisher(interceptResult, Publisher.class));
                            result = flowable.doOnRequest(n -> reactiveInvokeSample.set(initSamples(timedAnnotations))).doOnError(throwable -> exceptionClassHolder.set(throwable.getClass().getSimpleName())).doOnComplete(() -> finalizeSamples(timedAnnotations, exceptionClassHolder.get(), reactiveInvokeSample.get()));
                        }
                        return Publishers.convertPublisher(result, context.getReturnType().getType());
                    case COMPLETION_STAGE:
                        List<Timer.Sample> completionStageInvokeSamples = initSamples(timedAnnotations);
                        CompletionStage<?> completionStage = interceptedMethod.interceptResultAsCompletionStage();
                        CompletionStage<?> completionStageResult = completionStage.whenComplete((o, throwable) -> finalizeSamples(timedAnnotations, throwable == null ? "none" : throwable.getClass().getSimpleName(), completionStageInvokeSamples));
                        return interceptedMethod.handleResult(completionStageResult);
                    case SYNCHRONOUS:
                        syncInvokeSamples = initSamples(timedAnnotations);
                        return context.proceed();
                    default:
                        return interceptedMethod.unsupported();
                }
            } catch (Exception e) {
                exceptionClass = e.getClass().getSimpleName();
                return interceptedMethod.handleException(e);
            } finally {
                finalizeSamples(timedAnnotations, exceptionClass, syncInvokeSamples);
            }
        }
    }
    return context.proceed();
}
Also used : Publishers(io.micronaut.core.async.publisher.Publishers) Timed(io.micrometer.core.annotation.Timed) LoggerFactory(org.slf4j.LoggerFactory) InterceptedMethod(io.micronaut.aop.InterceptedMethod) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) InterceptorBean(io.micronaut.aop.InterceptorBean) Timer(io.micrometer.core.instrument.Timer) MethodInterceptor(io.micronaut.aop.MethodInterceptor) ConcurrentHistogram(org.HdrHistogram.ConcurrentHistogram) TypeHint(io.micronaut.core.annotation.TypeHint) RequiresMetrics(io.micronaut.configuration.metrics.annotation.RequiresMetrics) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) Publisher(org.reactivestreams.Publisher) TimedSet(io.micrometer.core.annotation.TimedSet) Singleton(jakarta.inject.Singleton) Mono(reactor.core.publisher.Mono) Histogram(org.HdrHistogram.Histogram) Flux(reactor.core.publisher.Flux) List(java.util.List) CompletionStage(java.util.concurrent.CompletionStage) MeterRegistry(io.micrometer.core.instrument.MeterRegistry) MethodInvocationContext(io.micronaut.aop.MethodInvocationContext) AnnotationValue(io.micronaut.core.annotation.AnnotationValue) CollectionUtils(io.micronaut.core.util.CollectionUtils) TimedAspect(io.micrometer.core.aop.TimedAspect) AnnotationMetadata(io.micronaut.core.annotation.AnnotationMetadata) VALUE_MEMBER(io.micronaut.core.annotation.AnnotationMetadata.VALUE_MEMBER) TimedSet(io.micrometer.core.annotation.TimedSet) InterceptedMethod(io.micronaut.aop.InterceptedMethod) AtomicReference(java.util.concurrent.atomic.AtomicReference) Publisher(org.reactivestreams.Publisher) AnnotationMetadata(io.micronaut.core.annotation.AnnotationMetadata) Timer(io.micrometer.core.instrument.Timer) AnnotationValue(io.micronaut.core.annotation.AnnotationValue) ArrayList(java.util.ArrayList) List(java.util.List)

Example 17 with AnnotationValue

use of io.micronaut.core.annotation.AnnotationValue in project micronaut-gcp by micronaut-projects.

the class PubSubClientIntroductionAdvice method intercept.

@Override
public Object intercept(MethodInvocationContext<Object, Object> context) {
    if (context.hasAnnotation(Topic.class)) {
        PubSubPublisherState publisherState = publisherStateCache.computeIfAbsent(context.getExecutableMethod(), method -> {
            String projectId = method.stringValue(PubSubClient.class).orElse(googleCloudConfiguration.getProjectId());
            Optional<Argument> orderingArgument = Arrays.stream(method.getArguments()).filter(argument -> argument.getAnnotationMetadata().hasAnnotation(OrderingKey.class)).findFirst();
            String topic = method.stringValue(Topic.class).orElse(context.getName());
            String endpoint = method.stringValue(Topic.class, "endpoint").orElse("");
            String configurationName = method.stringValue(Topic.class, "configuration").orElse("");
            String contentType = method.stringValue(Topic.class, "contentType").orElse(MediaType.APPLICATION_JSON);
            ProjectTopicName projectTopicName = PubSubTopicUtils.toProjectTopicName(topic, projectId);
            Map<String, String> staticMessageAttributes = new HashMap<>();
            List<AnnotationValue<MessageHeader>> headerAnnotations = context.getAnnotationValuesByType(MessageHeader.class);
            headerAnnotations.forEach((header) -> {
                String name = header.stringValue("name").orElse(null);
                String value = header.stringValue().orElse(null);
                if (StringUtils.isNotEmpty(name) && StringUtils.isNotEmpty(value)) {
                    staticMessageAttributes.put(name, value);
                }
            });
            Argument<?> bodyArgument = findBodyArgument(method).orElseThrow(() -> new PubSubClientException("No valid message body argument found for method: " + context.getExecutableMethod()));
            PubSubPublisherState.TopicState topicState = new PubSubPublisherState.TopicState(contentType, projectTopicName, configurationName, endpoint, orderingArgument.isPresent());
            logger.debug("Created a new publisher[{}] for topic: {}", context.getExecutableMethod().getName(), topic);
            PublisherInterface publisher = publisherFactory.createPublisher(new PublisherFactoryConfig(topicState, pubSubConfigurationProperties.getPublishingExecutor()));
            return new PubSubPublisherState(topicState, staticMessageAttributes, bodyArgument, publisher, orderingArgument);
        });
        Map<String, String> messageAttributes = new HashMap<>(publisherState.getStaticMessageAttributes());
        String contentType = publisherState.getTopicState().getContentType();
        Argument<?> bodyArgument = publisherState.getBodyArgument();
        Map<String, Object> parameterValues = context.getParameterValueMap();
        final ReturnType<Object> returnTypeInfo = context.getReturnType();
        ReturnType<Object> returnType = returnTypeInfo;
        Class<?> javaReturnType = returnType.getType();
        Argument[] arguments = context.getArguments();
        for (Argument arg : arguments) {
            AnnotationValue<MessageHeader> headerAnn = arg.getAnnotation(MessageHeader.class);
            if (headerAnn != null) {
                Map.Entry<String, String> entry = getNameAndValue(arg, headerAnn, parameterValues);
                messageAttributes.put(entry.getKey(), entry.getValue());
            }
        }
        PublisherInterface publisher = publisherState.getPublisher();
        Object body = parameterValues.get(bodyArgument.getName());
        PubsubMessage pubsubMessage = null;
        if (body.getClass() == PubsubMessage.class) {
            pubsubMessage = (PubsubMessage) body;
        } else {
            // if target type is byte[] we bypass serdes completely
            byte[] serialized = null;
            if (body.getClass() == byte[].class) {
                serialized = (byte[]) body;
            } else {
                PubSubMessageSerDes serDes = serDesRegistry.find(contentType).orElseThrow(() -> new PubSubClientException("Could not locate a valid SerDes implementation for type: " + contentType));
                serialized = serDes.serialize(body);
            }
            messageAttributes.put("Content-Type", contentType);
            PubsubMessage.Builder messageBuilder = PubsubMessage.newBuilder();
            messageBuilder.setData(ByteString.copyFrom(serialized)).putAllAttributes(messageAttributes);
            if (publisherState.getOrderingArgument().isPresent()) {
                String orderingKey = conversionService.convert(parameterValues.get(publisherState.getOrderingArgument().get().getName()), String.class).orElseThrow(() -> new PubSubClientException("Could not convert argument annotated with @OrderingKey to String type"));
                messageBuilder.setOrderingKey(orderingKey);
            }
            pubsubMessage = messageBuilder.build();
        }
        PubsubMessage finalPubsubMessage = pubsubMessage;
        Mono<String> reactiveResult = Mono.create(sink -> {
            ApiFuture<String> future = publisher.publish(finalPubsubMessage);
            future.addListener(() -> {
                try {
                    final String result = future.get();
                    sink.success(result);
                } catch (Throwable e) {
                    sink.error(e);
                }
            }, executorService);
        });
        if (javaReturnType == void.class || javaReturnType == Void.class) {
            String result = reactiveResult.block();
            return null;
        } else {
            if (returnTypeInfo.isReactive()) {
                return Publishers.convertPublisher(reactiveResult, javaReturnType);
            } else if (returnTypeInfo.isAsync()) {
                return reactiveResult.toFuture();
            } else {
                String result = reactiveResult.block();
                return conversionService.convert(result, javaReturnType).orElseThrow(() -> new PubSubClientException("Could not convert publisher result to method return type: " + javaReturnType));
            }
        }
    } else {
        return context.proceed();
    }
}
Also used : PublisherFactory(io.micronaut.gcp.pubsub.support.PublisherFactory) Arrays(java.util.Arrays) Publishers(io.micronaut.core.async.publisher.Publishers) PubSubPublisherState(io.micronaut.gcp.pubsub.support.PubSubPublisherState) LoggerFactory(org.slf4j.LoggerFactory) MessageHeader(io.micronaut.messaging.annotation.MessageHeader) HashMap(java.util.HashMap) PublisherFactoryConfig(io.micronaut.gcp.pubsub.support.PublisherFactoryConfig) ExecutableMethod(io.micronaut.inject.ExecutableMethod) PubSubMessageSerDesRegistry(io.micronaut.gcp.pubsub.serdes.PubSubMessageSerDesRegistry) PubsubMessage(com.google.pubsub.v1.PubsubMessage) PreDestroy(javax.annotation.PreDestroy) TaskExecutors(io.micronaut.scheduling.TaskExecutors) PublisherInterface(com.google.cloud.pubsub.v1.PublisherInterface) PubSubClientException(io.micronaut.gcp.pubsub.exception.PubSubClientException) MethodInterceptor(io.micronaut.aop.MethodInterceptor) PubSubTopicUtils(io.micronaut.gcp.pubsub.support.PubSubTopicUtils) MediaType(io.micronaut.http.MediaType) Map(java.util.Map) ReturnType(io.micronaut.core.type.ReturnType) PubSubConfigurationProperties(io.micronaut.gcp.pubsub.configuration.PubSubConfigurationProperties) ProjectTopicName(com.google.pubsub.v1.ProjectTopicName) Argument(io.micronaut.core.type.Argument) PubSubClient(io.micronaut.gcp.pubsub.annotation.PubSubClient) ConversionService(io.micronaut.core.convert.ConversionService) PubSubMessageSerDes(io.micronaut.gcp.pubsub.serdes.PubSubMessageSerDes) ExecutorService(java.util.concurrent.ExecutorService) Logger(org.slf4j.Logger) GoogleCloudConfiguration(io.micronaut.gcp.GoogleCloudConfiguration) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Singleton(jakarta.inject.Singleton) Mono(reactor.core.publisher.Mono) ApiFuture(com.google.api.core.ApiFuture) ByteString(com.google.protobuf.ByteString) StringUtils(io.micronaut.core.util.StringUtils) AbstractMap(java.util.AbstractMap) List(java.util.List) MethodInvocationContext(io.micronaut.aop.MethodInvocationContext) AnnotationValue(io.micronaut.core.annotation.AnnotationValue) OrderingKey(io.micronaut.gcp.pubsub.annotation.OrderingKey) Optional(java.util.Optional) Topic(io.micronaut.gcp.pubsub.annotation.Topic) MessageBody(io.micronaut.messaging.annotation.MessageBody) Named(jakarta.inject.Named) PubSubClientException(io.micronaut.gcp.pubsub.exception.PubSubClientException) Argument(io.micronaut.core.type.Argument) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) PubSubPublisherState(io.micronaut.gcp.pubsub.support.PubSubPublisherState) ByteString(com.google.protobuf.ByteString) PublisherInterface(com.google.cloud.pubsub.v1.PublisherInterface) PubsubMessage(com.google.pubsub.v1.PubsubMessage) PubSubClient(io.micronaut.gcp.pubsub.annotation.PubSubClient) Topic(io.micronaut.gcp.pubsub.annotation.Topic) PubSubMessageSerDes(io.micronaut.gcp.pubsub.serdes.PubSubMessageSerDes) PublisherFactoryConfig(io.micronaut.gcp.pubsub.support.PublisherFactoryConfig) AnnotationValue(io.micronaut.core.annotation.AnnotationValue) ProjectTopicName(com.google.pubsub.v1.ProjectTopicName) MessageHeader(io.micronaut.messaging.annotation.MessageHeader) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) AbstractMap(java.util.AbstractMap)

Example 18 with AnnotationValue

use of io.micronaut.core.annotation.AnnotationValue in project micronaut-gcp by micronaut-projects.

the class PubSubConsumerAdvice method process.

@Override
public void process(BeanDefinition<?> beanDefinition, ExecutableMethod<?, ?> method) {
    if (beanDefinition.hasDeclaredAnnotation(PubSubListener.class)) {
        AnnotationValue<Subscription> subscriptionAnnotation = method.getAnnotation(Subscription.class);
        io.micronaut.context.Qualifier<Object> qualifier = beanDefinition.getAnnotationTypeByStereotype(Qualifier.class).map(type -> Qualifiers.byAnnotation(beanDefinition, type)).orElse(null);
        boolean hasAckArg = Arrays.stream(method.getArguments()).anyMatch(arg -> Acknowledgement.class.isAssignableFrom(arg.getType()));
        Class<Object> beanType = (Class<Object>) beanDefinition.getBeanType();
        Object bean = beanContext.findBean(beanType, qualifier).orElseThrow(() -> new MessageListenerException("Could not find the bean to execute the method " + method));
        DefaultExecutableBinder<PubSubConsumerState> binder = new DefaultExecutableBinder<>();
        if (subscriptionAnnotation != null) {
            String subscriptionName = subscriptionAnnotation.getRequiredValue(String.class);
            ProjectSubscriptionName projectSubscriptionName = PubSubSubscriptionUtils.toProjectSubscriptionName(subscriptionName, googleCloudConfiguration.getProjectId());
            String defaultContentType = subscriptionAnnotation.stringValue("contentType").orElse(MediaType.APPLICATION_JSON);
            String configuration = subscriptionAnnotation.stringValue("configuration").orElse("");
            MessageReceiver receiver = (PubsubMessage message, AckReplyConsumer ackReplyConsumer) -> {
                String messageContentType = message.getAttributesMap().getOrDefault("Content-Type", "");
                String contentType = Optional.of(messageContentType).filter(StringUtils::isNotEmpty).orElse(defaultContentType);
                DefaultPubSubAcknowledgement pubSubAcknowledgement = new DefaultPubSubAcknowledgement(ackReplyConsumer);
                PubSubConsumerState consumerState = new PubSubConsumerState(message, ackReplyConsumer, projectSubscriptionName, contentType);
                try {
                    BoundExecutable executable = null;
                    try {
                        executable = binder.bind(method, binderRegistry, consumerState);
                    } catch (Exception ex) {
                        handleException(new PubSubMessageReceiverException("Error binding message to the method", ex, bean, consumerState));
                    }
                    // Discard result
                    executable.invoke(bean);
                    if (!hasAckArg) {
                        // if manual ack is not specified we auto ack message after method execution
                        pubSubAcknowledgement.ack();
                    } else {
                        Optional<Object> boundAck = Arrays.stream(executable.getBoundArguments()).filter(o -> (o instanceof DefaultPubSubAcknowledgement)).findFirst();
                        if (boundAck.isPresent()) {
                            DefaultPubSubAcknowledgement manualAck = (DefaultPubSubAcknowledgement) boundAck.get();
                            if (!manualAck.isClientAck()) {
                                logger.warn("Method {} was executed and no message acknowledge detected. Did you forget to invoke ack()/nack()?", method.getName());
                            }
                        }
                    }
                } catch (Exception e) {
                    handleException(new PubSubMessageReceiverException("Error handling message", e, bean, consumerState));
                }
            };
            try {
                this.subscriberFactory.createSubscriber(new SubscriberFactoryConfig(projectSubscriptionName, receiver, configuration, pubSubConfigurationProperties.getSubscribingExecutor()));
            } catch (Exception e) {
                throw new PubSubListenerException("Failed to create subscriber", e);
            }
        }
    }
}
Also used : java.util(java.util) Subscription(io.micronaut.gcp.pubsub.annotation.Subscription) BeanContext(io.micronaut.context.BeanContext) MessageListenerException(io.micronaut.messaging.exceptions.MessageListenerException) LoggerFactory(org.slf4j.LoggerFactory) PubSubSubscriptionUtils(io.micronaut.gcp.pubsub.support.PubSubSubscriptionUtils) ExecutableMethod(io.micronaut.inject.ExecutableMethod) AckReplyConsumer(com.google.cloud.pubsub.v1.AckReplyConsumer) PubSubMessageSerDesRegistry(io.micronaut.gcp.pubsub.serdes.PubSubMessageSerDesRegistry) PubsubMessage(com.google.pubsub.v1.PubsubMessage) MediaType(io.micronaut.http.MediaType) PubSubConfigurationProperties(io.micronaut.gcp.pubsub.configuration.PubSubConfigurationProperties) BoundExecutable(io.micronaut.core.bind.BoundExecutable) ConversionService(io.micronaut.core.convert.ConversionService) DefaultExecutableBinder(io.micronaut.core.bind.DefaultExecutableBinder) PubSubListener(io.micronaut.gcp.pubsub.annotation.PubSubListener) MessageReceiver(com.google.cloud.pubsub.v1.MessageReceiver) Logger(org.slf4j.Logger) GoogleCloudConfiguration(io.micronaut.gcp.GoogleCloudConfiguration) PubSubMessageReceiverException(io.micronaut.gcp.pubsub.exception.PubSubMessageReceiverException) Qualifiers(io.micronaut.inject.qualifiers.Qualifiers) Singleton(jakarta.inject.Singleton) Acknowledgement(io.micronaut.messaging.Acknowledgement) ExecutableMethodProcessor(io.micronaut.context.processor.ExecutableMethodProcessor) StringUtils(io.micronaut.core.util.StringUtils) AnnotationValue(io.micronaut.core.annotation.AnnotationValue) ProjectSubscriptionName(com.google.pubsub.v1.ProjectSubscriptionName) BeanDefinition(io.micronaut.inject.BeanDefinition) Qualifier(jakarta.inject.Qualifier) io.micronaut.gcp.pubsub.bind(io.micronaut.gcp.pubsub.bind) PubSubMessageReceiverExceptionHandler(io.micronaut.gcp.pubsub.exception.PubSubMessageReceiverExceptionHandler) PubSubListenerException(io.micronaut.gcp.pubsub.exception.PubSubListenerException) PubsubMessage(com.google.pubsub.v1.PubsubMessage) Acknowledgement(io.micronaut.messaging.Acknowledgement) StringUtils(io.micronaut.core.util.StringUtils) Subscription(io.micronaut.gcp.pubsub.annotation.Subscription) MessageListenerException(io.micronaut.messaging.exceptions.MessageListenerException) AckReplyConsumer(com.google.cloud.pubsub.v1.AckReplyConsumer) ProjectSubscriptionName(com.google.pubsub.v1.ProjectSubscriptionName) PubSubMessageReceiverException(io.micronaut.gcp.pubsub.exception.PubSubMessageReceiverException) DefaultExecutableBinder(io.micronaut.core.bind.DefaultExecutableBinder) MessageListenerException(io.micronaut.messaging.exceptions.MessageListenerException) PubSubMessageReceiverException(io.micronaut.gcp.pubsub.exception.PubSubMessageReceiverException) PubSubListenerException(io.micronaut.gcp.pubsub.exception.PubSubListenerException) MessageReceiver(com.google.cloud.pubsub.v1.MessageReceiver) BoundExecutable(io.micronaut.core.bind.BoundExecutable) PubSubListenerException(io.micronaut.gcp.pubsub.exception.PubSubListenerException)

Example 19 with AnnotationValue

use of io.micronaut.core.annotation.AnnotationValue in project micronaut-core by micronaut-projects.

the class JsonViewServerFilter method doFilter.

@Override
public Publisher<MutableHttpResponse<?>> doFilter(HttpRequest<?> request, ServerFilterChain chain) {
    final RouteInfo<?> routeInfo = request.getAttribute(HttpAttributes.ROUTE_INFO, RouteInfo.class).orElse(null);
    final Publisher<MutableHttpResponse<?>> responsePublisher = chain.proceed(request);
    if (routeInfo != null) {
        final Optional<Class<?>> viewClass = routeInfo.findAnnotation(JsonView.class).flatMap(AnnotationValue::classValue);
        if (viewClass.isPresent()) {
            return Flux.from(responsePublisher).switchMap(response -> {
                final Optional<?> optionalBody = response.getBody();
                if (optionalBody.isPresent()) {
                    Object body = optionalBody.get();
                    MediaTypeCodec codec = codecFactory.resolveJsonViewCodec(viewClass.get());
                    if (Publishers.isConvertibleToPublisher(body)) {
                        Publisher<?> pub = Publishers.convertPublisher(body, Publisher.class);
                        response.body(Flux.from(pub).map(o -> codec.encode((Argument) routeInfo.getBodyType(), o)).subscribeOn(Schedulers.fromExecutorService(executorService)));
                    } else {
                        return Mono.fromCallable(() -> {
                            @SuppressWarnings({ "unchecked", "rawtypes" }) final byte[] encoded = codec.encode((Argument) routeInfo.getBodyType(), body);
                            response.body(encoded);
                            return response;
                        }).subscribeOn(Schedulers.fromExecutorService(executorService));
                    }
                }
                return Flux.just(response);
            });
        }
    }
    return responsePublisher;
}
Also used : MutableHttpResponse(io.micronaut.http.MutableHttpResponse) Argument(io.micronaut.core.type.Argument) JsonView(com.fasterxml.jackson.annotation.JsonView) AnnotationValue(io.micronaut.core.annotation.AnnotationValue) RouteInfo(io.micronaut.web.router.RouteInfo) MediaTypeCodec(io.micronaut.http.codec.MediaTypeCodec)

Example 20 with AnnotationValue

use of io.micronaut.core.annotation.AnnotationValue in project micronaut-core by micronaut-projects.

the class AbstractEnvironmentAnnotationMetadata method getAnnotationValuesByType.

@Override
@NonNull
public <T extends Annotation> List<AnnotationValue<T>> getAnnotationValuesByType(@NonNull Class<T> annotationType) {
    ArgumentUtils.requireNonNull("annotationType", annotationType);
    Environment environment = getEnvironment();
    List<AnnotationValue<T>> values = environmentAnnotationMetadata.getAnnotationValuesByType(annotationType);
    if (environment != null) {
        return values.stream().map(entries -> new EnvironmentAnnotationValue<>(environment, entries)).collect(Collectors.toList());
    }
    return values;
}
Also used : Environment(io.micronaut.context.env.Environment) java.util(java.util) Array(java.lang.reflect.Array) Internal(io.micronaut.core.annotation.Internal) OptionalValues(io.micronaut.core.value.OptionalValues) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) NonNull(io.micronaut.core.annotation.NonNull) StringUtils(io.micronaut.core.util.StringUtils) Stream(java.util.stream.Stream) Nullable(io.micronaut.core.annotation.Nullable) AnnotationValue(io.micronaut.core.annotation.AnnotationValue) Annotation(java.lang.annotation.Annotation) AnnotationMetadata(io.micronaut.core.annotation.AnnotationMetadata) ArgumentUtils(io.micronaut.core.util.ArgumentUtils) PropertyPlaceholderResolver(io.micronaut.context.env.PropertyPlaceholderResolver) Argument(io.micronaut.core.type.Argument) Environment(io.micronaut.context.env.Environment) AnnotationValue(io.micronaut.core.annotation.AnnotationValue) NonNull(io.micronaut.core.annotation.NonNull)

Aggregations

AnnotationValue (io.micronaut.core.annotation.AnnotationValue)60 AnnotationMetadata (io.micronaut.core.annotation.AnnotationMetadata)27 NonNull (io.micronaut.core.annotation.NonNull)24 Optional (java.util.Optional)24 List (java.util.List)23 Map (java.util.Map)22 StringUtils (io.micronaut.core.util.StringUtils)21 Nullable (io.micronaut.core.annotation.Nullable)18 Argument (io.micronaut.core.type.Argument)18 ExecutableMethod (io.micronaut.inject.ExecutableMethod)17 ArrayList (java.util.ArrayList)16 Collections (java.util.Collections)16 Annotation (java.lang.annotation.Annotation)15 Arrays (java.util.Arrays)15 HashMap (java.util.HashMap)15 Collectors (java.util.stream.Collectors)15 InterceptedMethod (io.micronaut.aop.InterceptedMethod)14 Stream (java.util.stream.Stream)14 ConversionService (io.micronaut.core.convert.ConversionService)13 Singleton (jakarta.inject.Singleton)13