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 }