From f170f108c5faa53bb8dbdbec671b7c954f748df3 Mon Sep 17 00:00:00 2001 From: Oguzhan Soykan Date: Mon, 28 Oct 2024 17:12:30 +0100 Subject: [PATCH] Delegate single instance resolve to QuarkusTypeResolver --- .../kediatr/quarkus/KediatRBeanProvider.kt | 21 ++++--------------- ...sTypeScanner.kt => QuarkusTypeResolver.kt} | 13 ++++++++---- 2 files changed, 13 insertions(+), 21 deletions(-) rename projects/kediatr-quarkus-starter/src/main/kotlin/com/trendyol/kediatr/quarkus/{QuarkusTypeScanner.kt => QuarkusTypeResolver.kt} (74%) diff --git a/projects/kediatr-quarkus-starter/src/main/kotlin/com/trendyol/kediatr/quarkus/KediatRBeanProvider.kt b/projects/kediatr-quarkus-starter/src/main/kotlin/com/trendyol/kediatr/quarkus/KediatRBeanProvider.kt index 3d9d789..ecdb6c4 100644 --- a/projects/kediatr-quarkus-starter/src/main/kotlin/com/trendyol/kediatr/quarkus/KediatRBeanProvider.kt +++ b/projects/kediatr-quarkus-starter/src/main/kotlin/com/trendyol/kediatr/quarkus/KediatRBeanProvider.kt @@ -1,34 +1,21 @@ -@file:Suppress("UNCHECKED_CAST") - package com.trendyol.kediatr.quarkus import com.trendyol.kediatr.* import io.quarkus.runtime.Startup import jakarta.enterprise.context.ApplicationScoped -import jakarta.enterprise.inject.spi.BeanManager class KediatRBeanProvider( - private val beanManager: BeanManager, - private val quarkusTypeScanner: QuarkusTypeScanner + private val resolver: QuarkusTypeResolver ) : DependencyProvider { - override fun getSingleInstanceOf(clazz: Class): T { - val beans = beanManager.getBeans(clazz) - val bean = beans.firstOrNull() ?: error("No bean found for class $clazz") - val ctx = beanManager.createCreationalContext(bean) - return beanManager.getReference(bean, clazz, ctx) as T - } + override fun getSingleInstanceOf(clazz: Class): T = resolver.resolveOrThrow(clazz) - override fun getSubTypesOf(clazz: Class): Collection> = quarkusTypeScanner.getSubTypesOf(clazz) + override fun getSubTypesOf(clazz: Class): Collection> = resolver.resolveTypesOrEmpty(clazz) } @ApplicationScoped class QuarkusMediatorBuilder { @ApplicationScoped - fun kediatRBeanProvider( - beanManager: BeanManager, - quarkusTypeScanner: QuarkusTypeScanner - ): KediatRBeanProvider = - KediatRBeanProvider(beanManager, quarkusTypeScanner) + fun kediatRBeanProvider(resolver: QuarkusTypeResolver): KediatRBeanProvider = KediatRBeanProvider(resolver) @ApplicationScoped @Startup diff --git a/projects/kediatr-quarkus-starter/src/main/kotlin/com/trendyol/kediatr/quarkus/QuarkusTypeScanner.kt b/projects/kediatr-quarkus-starter/src/main/kotlin/com/trendyol/kediatr/quarkus/QuarkusTypeResolver.kt similarity index 74% rename from projects/kediatr-quarkus-starter/src/main/kotlin/com/trendyol/kediatr/quarkus/QuarkusTypeScanner.kt rename to projects/kediatr-quarkus-starter/src/main/kotlin/com/trendyol/kediatr/quarkus/QuarkusTypeResolver.kt index 7bb053a..2dc7821 100644 --- a/projects/kediatr-quarkus-starter/src/main/kotlin/com/trendyol/kediatr/quarkus/QuarkusTypeScanner.kt +++ b/projects/kediatr-quarkus-starter/src/main/kotlin/com/trendyol/kediatr/quarkus/QuarkusTypeResolver.kt @@ -7,12 +7,17 @@ import jakarta.enterprise.inject.spi.* import java.lang.reflect.* @ApplicationScoped -class QuarkusTypeScanner( +class QuarkusTypeResolver( private val beanManager: BeanManager ) { - fun getSubTypesOf( - clazz: Class - ): Collection> = beanManager.getBeans(Any::class.java) + fun resolveOrThrow(clazz: Class): T { + val beans = beanManager.getBeans(clazz) + val bean = beans.firstOrNull() ?: error("No bean found for class $clazz") + val ctx = beanManager.createCreationalContext(bean) + return beanManager.getReference(bean, clazz, ctx) as T + } + + fun resolveTypesOrEmpty(clazz: Class): Collection> = beanManager.getBeans(Any::class.java) .asSequence() .filterNot(::quarkusThings) .flatMap { it.types }