From f56659d45796e154b6bfc29966efac0fbdaf5b0d Mon Sep 17 00:00:00 2001 From: Danielle Voznyy Date: Tue, 12 Mar 2024 22:47:56 -0400 Subject: [PATCH] Update autoscanner to work on new system function syntax --- .../com/mineinabyss/geary/autoscan/AutoScanner.kt | 11 +++++------ .../com/mineinabyss/geary/autoscan/AutoScannerDSL.kt | 11 ++++++++--- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/addons/geary-autoscan/src/main/kotlin/com/mineinabyss/geary/autoscan/AutoScanner.kt b/addons/geary-autoscan/src/main/kotlin/com/mineinabyss/geary/autoscan/AutoScanner.kt index a334e806..ccc21d12 100644 --- a/addons/geary-autoscan/src/main/kotlin/com/mineinabyss/geary/autoscan/AutoScanner.kt +++ b/addons/geary-autoscan/src/main/kotlin/com/mineinabyss/geary/autoscan/AutoScanner.kt @@ -7,13 +7,14 @@ import com.mineinabyss.geary.modules.geary import com.mineinabyss.geary.systems.System import com.mineinabyss.idofront.di.DI import kotlin.reflect.KClass +import kotlin.reflect.KFunction import kotlin.reflect.full.createInstance val autoScanner by DI.observe() interface AutoScanner { val scannedComponents: MutableSet> - val scannedSystems: MutableSet> + val scannedSystems: MutableSet> fun installSystems() @@ -21,14 +22,12 @@ interface AutoScanner { override fun default() = object : AutoScanner { private val logger get() = geary.logger override val scannedComponents = mutableSetOf>() - override val scannedSystems = mutableSetOf>() + override val scannedSystems = mutableSetOf>() override fun installSystems() { scannedSystems.asSequence() - .mapNotNull { it.objectInstance ?: runCatching { it.createInstance() }.getOrNull() } - .filterIsInstance>() - .onEach { geary.pipeline.addSystem(it) } - .map { it::class.simpleName } + .onEach { it.call(geary) } + .map { it.name } .let { if (logger.config.minSeverity <= Severity.Verbose) logger.i("Autoscan loaded singleton systems: ${it.joinToString()}") diff --git a/addons/geary-autoscan/src/main/kotlin/com/mineinabyss/geary/autoscan/AutoScannerDSL.kt b/addons/geary-autoscan/src/main/kotlin/com/mineinabyss/geary/autoscan/AutoScannerDSL.kt index 83bac5a8..a9d4aeab 100644 --- a/addons/geary-autoscan/src/main/kotlin/com/mineinabyss/geary/autoscan/AutoScannerDSL.kt +++ b/addons/geary-autoscan/src/main/kotlin/com/mineinabyss/geary/autoscan/AutoScannerDSL.kt @@ -4,6 +4,7 @@ import co.touchlab.kermit.Severity import com.mineinabyss.geary.addons.dsl.GearyDSL import com.mineinabyss.geary.datatypes.Component import com.mineinabyss.geary.modules.GearyConfiguration +import com.mineinabyss.geary.modules.GearyModule import com.mineinabyss.geary.modules.geary import com.mineinabyss.geary.serialization.dsl.serialization import com.mineinabyss.geary.systems.System @@ -13,9 +14,13 @@ import org.reflections.Reflections import org.reflections.scanners.Scanners import org.reflections.util.ConfigurationBuilder import org.reflections.util.FilterBuilder +import java.lang.reflect.Method import kotlin.reflect.KClass +import kotlin.reflect.KFunction import kotlin.reflect.full.hasAnnotation import kotlin.reflect.full.isSubclassOf +import kotlin.reflect.jvm.kotlinFunction +import kotlin.reflect.typeOf @GearyDSL fun GearyConfiguration.autoscan( @@ -96,10 +101,10 @@ class AutoScannerDSL( */ fun systems() { val scanned = reflections - .get(Scanners.TypesAnnotated.with(AutoScan::class.java).asClass>(classLoader)) + .get(Scanners.MethodsAnnotated.with(AutoScan::class.java).`as`(Method::class.java)) .asSequence() - .map { it.kotlin } - .filter { !it.hasAnnotation() && it.isSubclassOf(System::class) } + .mapNotNull { it.kotlinFunction } + .filter { it.parameters.singleOrNull()?.type == typeOf() } autoScanner.scannedSystems += scanned }