io.micronaut.inject.ExecutableMethod Java Examples

The following examples show how to use io.micronaut.inject.ExecutableMethod. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TransactionalInterceptor.java    From micronaut-data with Apache License 2.0 6 votes vote down vote up
/**
 * @param executableMethod The method
 * @return The {@link TransactionAttribute}
 */
protected TransactionAttribute resolveTransactionDefinition(
        ExecutableMethod<Object, Object> executableMethod) {
    AnnotationValue<TransactionalAdvice> annotation = executableMethod.getAnnotation(TransactionalAdvice.class);

    if (annotation == null) {
        throw new IllegalStateException("No declared @Transactional annotation present");
    }

    DefaultTransactionAttribute attribute = new DefaultTransactionAttribute();
    attribute.setName(executableMethod.getDeclaringType().getSimpleName() + "." + executableMethod.getMethodName());
    attribute.setReadOnly(annotation.isTrue("readOnly"));
    annotation.intValue("timeout").ifPresent(value -> attribute.setTimeout(Duration.ofSeconds(value)));
    final Class[] noRollbackFors = annotation.classValues("noRollbackFor");
    //noinspection unchecked
    attribute.setNoRollbackFor(noRollbackFors);
    annotation.enumValue("propagation", TransactionDefinition.Propagation.class)
            .ifPresent(attribute::setPropagationBehavior);
    annotation.enumValue("isolation", TransactionDefinition.Isolation.class)
            .ifPresent(attribute::setIsolationLevel);
    return attribute;
}
 
Example #2
Source File: TransactionInterceptor.java    From micronaut-spring with Apache License 2.0 6 votes vote down vote up
/**
 * @param targetMethod           The target method
 * @param annotationMetadata     The annotation metadata
 * @param transactionManagerName The transaction manager
 * @return The {@link TransactionAttribute}
 */
protected TransactionAttribute resolveTransactionAttribute(
        ExecutableMethod<Object, Object> targetMethod,
        AnnotationMetadata annotationMetadata,
        String transactionManagerName) {
    return transactionDefinitionMap.computeIfAbsent(targetMethod, method -> {

        BindableRuleBasedTransactionAttribute attribute = new BindableRuleBasedTransactionAttribute();
        attribute.setReadOnly(annotationMetadata.isTrue(Transactional.class, "readOnly"));
        attribute.setTimeout(annotationMetadata.intValue(Transactional.class, "timeout").orElse(TransactionDefinition.TIMEOUT_DEFAULT));
        //noinspection unchecked
        attribute.setRollbackFor(annotationMetadata.classValues(Transactional.class, "rollbackFor"));
        //noinspection unchecked
        attribute.setNoRollbackFor(annotationMetadata.classValues(Transactional.class, "noRollbackFor"));
        int propagation = annotationMetadata
                .enumValue(Transactional.class, "propagation", Propagation.class)
                .orElse(Propagation.REQUIRED).value();
        attribute.setPropagationBehavior(propagation);
        int isolation = annotationMetadata
                .enumValue(Transactional.class, "isolation", Isolation.class)
                .orElse(Isolation.DEFAULT).value();
        attribute.setIsolationLevel(isolation);
        attribute.setQualifier(transactionManagerName);
        return attribute;
    });
}
 
Example #3
Source File: SpringConfigurationInterceptor.java    From micronaut-spring with Apache License 2.0 6 votes vote down vote up
@Override
public Object intercept(MethodInvocationContext<Object, Object> context) {
    final AnnotationMetadata annotationMetadata = context.getAnnotationMetadata();
    final boolean isSingleton = MicronautBeanFactory.isSingleton(annotationMetadata);
    if (isSingleton) {
        final ExecutableMethod<Object, Object> method = context.getExecutableMethod();
        synchronized (computedSingletons) {
            Object o = computedSingletons.get(method);
            if (o == null) {
                o = context.proceed();
                if (o == null) {
                    throw new BeanCreationException("Bean factor method [" + method + "] returned null");
                }
                computedSingletons.put(method, o);
            }
            return o;
        }
    }
    return context.proceed();
}
 
Example #4
Source File: GoogleMethodRouteBuilder.java    From micronaut-gcp with Apache License 2.0 5 votes vote down vote up
@Override
protected UriRoute buildBeanRoute(String httpMethodName, HttpMethod httpMethod, String uri, BeanDefinition<?> beanDefinition, ExecutableMethod<?, ?> method) {
    String cp = contextPathProvider.getContextPath();
    if (cp != null) {
        uri = StringUtils.prependUri(cp, uri);
    }
    return super.buildBeanRoute(httpMethodName, httpMethod, uri, beanDefinition, method);
}
 
Example #5
Source File: KafkaConsumerProcessor.java    From micronaut-kafka with Apache License 2.0 5 votes vote down vote up
private Argument findBodyArgument(ExecutableMethod<?, ?> method) {
    return Arrays.stream(method.getArguments())
            .filter(arg -> arg.getType() == ConsumerRecord.class || arg.getAnnotationMetadata().hasAnnotation(Body.class))
            .findFirst()
            .orElseGet(() ->
                    Arrays.stream(method.getArguments())
                            .filter(arg -> !arg.getAnnotationMetadata().hasStereotype(Bindable.class))
                            .findFirst()
                            .orElse(null)
            );
}
 
Example #6
Source File: AbstractQueryInterceptor.java    From micronaut-data with Apache License 2.0 5 votes vote down vote up
/**
 * Prepares a query for the given context.
 *
 * @param methodKey The method key
 * @param context The context
 * @return The query
 */
protected final PreparedQuery<?, Number> prepareCountQuery(RepositoryMethodKey methodKey, @NonNull MethodInvocationContext<T, R> context) {
    ExecutableMethod<T, R> executableMethod = context.getExecutableMethod();
    StoredQuery<?, Long> storedQuery = countQueries.get(methodKey);
    if (storedQuery == null) {

        String query = context.stringValue(Query.class, DataMethod.META_MEMBER_COUNT_QUERY).orElseThrow(() ->
                new IllegalStateException("No query present in method")
        );
        Class rootEntity = getRequiredRootEntity(context);

        storedQuery = new DefaultStoredQuery<Object, Long>(
                executableMethod,
                Long.class,
                rootEntity,
                query,
                DataMethod.META_MEMBER_PARAMETER_BINDING,
                true
        );
        countQueries.put(methodKey, storedQuery);
    }

    Pageable pageable = storedQuery.hasPageable() ? getPageable(context) : Pageable.UNPAGED;
    //noinspection unchecked
    return new DefaultPreparedQuery(
            context,
            storedQuery,
            storedQuery.getQuery(),
            pageable,
            false
    );
}
 
Example #7
Source File: TransactionalSessionInterceptor.java    From micronaut-sql with Apache License 2.0 4 votes vote down vote up
@Override
public Object intercept(MethodInvocationContext<Session, Object> context) {
    final ExecutableMethod<Session, Object> method = context.getExecutableMethod();
    return method.invoke(sessionFactory.getCurrentSession(), context.getParameterValues());
}
 
Example #8
Source File: KafkaConsumerProcessor.java    From micronaut-kafka with Apache License 2.0 4 votes vote down vote up
private void configureDeserializers(ExecutableMethod<?, ?> method, DefaultKafkaConsumerConfiguration consumerConfiguration) {
    Properties properties = consumerConfiguration.getConfig();
    // figure out the Key deserializer
    Argument bodyArgument = findBodyArgument(method);

    if (!properties.containsKey(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG)) {
        if (!consumerConfiguration.getKeyDeserializer().isPresent()) {
            Optional<Argument> keyArgument = Arrays.stream(method.getArguments())
                    .filter(arg -> arg.isAnnotationPresent(KafkaKey.class)).findFirst();

            if (keyArgument.isPresent()) {
                consumerConfiguration.setKeyDeserializer(
                        serdeRegistry.pickDeserializer(keyArgument.get())
                );
            } else {
                //noinspection SingleStatementInBlock
                if (bodyArgument != null && ConsumerRecord.class.isAssignableFrom(bodyArgument.getType())) {
                    Optional<Argument<?>> keyType = bodyArgument.getTypeVariable("K");
                    if (keyType.isPresent()) {
                        consumerConfiguration.setKeyDeserializer(
                                serdeRegistry.pickDeserializer(keyType.get())
                        );
                    } else {
                        consumerConfiguration.setKeyDeserializer(new ByteArrayDeserializer());
                    }
                } else {
                    consumerConfiguration.setKeyDeserializer(new ByteArrayDeserializer());
                }
            }
        }
    }

    // figure out the Value deserializer
    if (!properties.containsKey(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG) && !consumerConfiguration.getValueDeserializer().isPresent()) {

        if (bodyArgument != null) {

            if (ConsumerRecord.class.isAssignableFrom(bodyArgument.getType())) {
                Optional<Argument<?>> valueType = bodyArgument.getTypeVariable("V");
                if (valueType.isPresent()) {
                    consumerConfiguration.setValueDeserializer(
                            serdeRegistry.pickDeserializer(valueType.get())
                    );
                } else {
                    consumerConfiguration.setValueDeserializer(new StringDeserializer());
                }

            } else {
                boolean batch = method.isTrue(KafkaListener.class, "batch");

                consumerConfiguration.setValueDeserializer(
                        serdeRegistry.pickDeserializer(batch ? bodyArgument.getFirstTypeVariable().orElse(bodyArgument) : bodyArgument)
                );
            }
        } else {
            //noinspection SingleStatementInBlock
            consumerConfiguration.setValueDeserializer(new StringDeserializer());
        }
    }
}
 
Example #9
Source File: RepositoryMethodKey.java    From micronaut-data with Apache License 2.0 4 votes vote down vote up
/**
 * Default constructor.
 * @param repository The repository
 * @param method The method
 */
public RepositoryMethodKey(Object repository, ExecutableMethod method) {
    this.repository = repository;
    this.method = method;
    this.hashCode = Objects.hash(repository, method);
}
 
Example #10
Source File: MicronautLockConfigurationExtractor.java    From ShedLock with Apache License 2.0 4 votes vote down vote up
@NonNull
Optional<LockConfiguration> getLockConfiguration(@NonNull ExecutableMethod<Object, Object> method) {
    Optional<AnnotationValue<SchedulerLock>> annotation = findAnnotation(method);
    return annotation.map(this::getLockConfiguration);
}
 
Example #11
Source File: MicronautLockConfigurationExtractor.java    From ShedLock with Apache License 2.0 4 votes vote down vote up
Optional<AnnotationValue<SchedulerLock>> findAnnotation(ExecutableMethod<Object, Object> method) {
    return method.findAnnotation(SchedulerLock.class);
}
 
Example #12
Source File: AbstractQueryInterceptor.java    From micronaut-data with Apache License 2.0 2 votes vote down vote up
/**
 * Default constructor.
 * @param method The method
 * @param rootEntity The root entity
 * @param pageable The pageable
 */
DefaultPagedQuery(ExecutableMethod<?, ?> method, @NonNull Class<E> rootEntity, Pageable pageable) {
    this.method = method;
    this.rootEntity = rootEntity;
    this.pageable = pageable;
}