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