Search in sources :

Example 1 with FieldInjectionPoint

use of io.micronaut.inject.FieldInjectionPoint in project micronaut-kafka by micronaut-projects.

the class KafkaProducerFactory method getProducer.

/**
 * Creates a new {@link KafkaProducer} for the given configuration.
 *
 * @param injectionPoint The injection point used to create the bean
 * @param producerConfiguration An optional producer configuration
 * @param <K> The key type
 * @param <V> The value type
 * @return The consumer
 */
@Bean
@Any
public <K, V> Producer<K, V> getProducer(@Nullable InjectionPoint<KafkaProducer<K, V>> injectionPoint, @Nullable @Parameter AbstractKafkaProducerConfiguration<K, V> producerConfiguration) {
    if (injectionPoint == null) {
        if (producerConfiguration != null) {
            Optional<Serializer<K>> keySerializer = producerConfiguration.getKeySerializer();
            Optional<Serializer<V>> valueSerializer = producerConfiguration.getValueSerializer();
            Properties config = producerConfiguration.getConfig();
            if (keySerializer.isPresent() && valueSerializer.isPresent()) {
                Serializer<K> ks = keySerializer.get();
                Serializer<V> vs = valueSerializer.get();
                return new KafkaProducer<>(config, ks, vs);
            } else if (keySerializer.isPresent() || valueSerializer.isPresent()) {
                throw new ConfigurationException("Both the [keySerializer] and [valueSerializer] must be set when setting either");
            } else {
                return new KafkaProducer<>(config);
            }
        } else {
            throw new ConfigurationException("No Kafka configuration specified when using direct instantiation");
        }
    }
    Argument<?> argument;
    if (injectionPoint instanceof FieldInjectionPoint) {
        argument = ((FieldInjectionPoint<?, ?>) injectionPoint).asArgument();
    } else if (injectionPoint instanceof ArgumentInjectionPoint) {
        argument = ((ArgumentInjectionPoint<?, ?>) injectionPoint).getArgument();
    } else {
        throw new ConfigurationException("Cannot directly retrieve KafkaProducer instances. Use @Inject or constructor injection");
    }
    Argument<?> k = argument.getTypeVariable("K").orElse(null);
    Argument<?> v = argument.getTypeVariable("V").orElse(null);
    if (k == null || v == null) {
        throw new ConfigurationException("@KafkaClient used on type missing generic argument values for Key and Value: " + injectionPoint);
    }
    final String id = injectionPoint.getAnnotationMetadata().stringValue(KafkaClient.class).orElse(null);
    return getKafkaProducer(id, null, k, v, false);
}
Also used : KafkaProducer(org.apache.kafka.clients.producer.KafkaProducer) Properties(java.util.Properties) ArgumentInjectionPoint(io.micronaut.inject.ArgumentInjectionPoint) ConfigurationException(io.micronaut.context.exceptions.ConfigurationException) KafkaClient(io.micronaut.configuration.kafka.annotation.KafkaClient) FieldInjectionPoint(io.micronaut.inject.FieldInjectionPoint) Serializer(org.apache.kafka.common.serialization.Serializer) Any(io.micronaut.context.annotation.Any) Bean(io.micronaut.context.annotation.Bean)

Example 2 with FieldInjectionPoint

use of io.micronaut.inject.FieldInjectionPoint in project micronaut-test by micronaut-projects.

the class MicronautJunit5Extension method alignMocks.

@Override
protected void alignMocks(ExtensionContext context, Object instance) {
    if (specDefinition == null) {
        return;
    }
    findSpecInstance(context).ifPresent(specInstance -> {
        for (FieldInjectionPoint injectedField : specDefinition.getInjectedFields()) {
            final boolean isMock = applicationContext.resolveMetadata(injectedField.getType()).isAnnotationPresent(MockBean.class);
            if (isMock) {
                final Field field = injectedField.getField();
                field.setAccessible(true);
                try {
                    final Object mock = field.get(specInstance);
                    if (mock instanceof InterceptedProxy) {
                        InterceptedProxy ip = (InterceptedProxy) mock;
                        final Object target = ip.interceptedTarget();
                        field.set(specInstance, target);
                    }
                } catch (IllegalAccessException e) {
                // continue
                }
            }
        }
    });
}
Also used : Field(java.lang.reflect.Field) InterceptedProxy(io.micronaut.aop.InterceptedProxy) FieldInjectionPoint(io.micronaut.inject.FieldInjectionPoint)

Aggregations

FieldInjectionPoint (io.micronaut.inject.FieldInjectionPoint)2 InterceptedProxy (io.micronaut.aop.InterceptedProxy)1 KafkaClient (io.micronaut.configuration.kafka.annotation.KafkaClient)1 Any (io.micronaut.context.annotation.Any)1 Bean (io.micronaut.context.annotation.Bean)1 ConfigurationException (io.micronaut.context.exceptions.ConfigurationException)1 ArgumentInjectionPoint (io.micronaut.inject.ArgumentInjectionPoint)1 Field (java.lang.reflect.Field)1 Properties (java.util.Properties)1 KafkaProducer (org.apache.kafka.clients.producer.KafkaProducer)1 Serializer (org.apache.kafka.common.serialization.Serializer)1