From f2c883be65273bd149ac5968a74e61f4ea9272e4 Mon Sep 17 00:00:00 2001 From: GrowlyX Date: Sun, 2 Jan 2022 18:56:58 -0500 Subject: [PATCH] Make use of FlavorBinder#scope when injecting into fields --- src/main/kotlin/gg/scala/flavor/Flavor.kt | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/main/kotlin/gg/scala/flavor/Flavor.kt b/src/main/kotlin/gg/scala/flavor/Flavor.kt index e38a109..49ce250 100644 --- a/src/main/kotlin/gg/scala/flavor/Flavor.kt +++ b/src/main/kotlin/gg/scala/flavor/Flavor.kt @@ -96,7 +96,7 @@ class Flavor( } } - fun tracked(lambda: () -> Unit): Long + private fun tracked(lambda: () -> Unit): Long { val start = System.currentTimeMillis() lambda.invoke() @@ -106,8 +106,15 @@ class Flavor( private fun scanAndInject(clazz: KClass<*>, instance: Any? = null) { - val singleton = instance ?: InjectScope.SINGLETON - .instanceCreator.invoke(clazz) + val singletonRaw = try + { + InjectScope.SINGLETON + .instanceCreator.invoke(clazz) + } catch (exception: Exception) + { + null + } + val singleton = instance ?: singletonRaw!! for (field in clazz.java.fields) { @@ -138,6 +145,14 @@ class Flavor( val accessability = field.isAccessible binder?.let { + if (binder.scope == InjectScope.SINGLETON) + { + if (singletonRaw == null) + { + return@let + } + } + field.isAccessible = false field.set(singleton, it.instance) field.isAccessible = accessability