Skip to content

Commit

Permalink
Make use of FlavorBinder#scope when injecting into fields
Browse files Browse the repository at this point in the history
  • Loading branch information
GrowlyX committed Jan 2, 2022
1 parent a229883 commit f2c883b
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions src/main/kotlin/gg/scala/flavor/Flavor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class Flavor(
}
}

fun tracked(lambda: () -> Unit): Long
private fun tracked(lambda: () -> Unit): Long
{
val start = System.currentTimeMillis()
lambda.invoke()
Expand All @@ -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)
{
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit f2c883b

Please sign in to comment.