Skip to content

Commit

Permalink
Merge pull request InsertKoinIO#1175 from peter-kovacs-accedo/KoinJav…
Browse files Browse the repository at this point in the history
…aComponent_nullable

KoinJavaComponent.kt update to make it able to return null values.
  • Loading branch information
arnaudgiuliani authored Nov 17, 2021
2 parents e1628df + 5ac85e6 commit b1f74b3
Showing 1 changed file with 43 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,26 @@ object KoinJavaComponent {
}
}

/**
* Retrieve given dependency lazily if available
* @param clazz - dependency class
* @param qualifier - bean canonicalName / optional
* @param scope - scope
* @param parameters - dependency parameters / optional
*/

@JvmStatic
@JvmOverloads
fun <T : Any> injectOrNull(
clazz: Class<*>,
qualifier: Qualifier? = null,
parameters: ParametersDefinition? = null
): Lazy<T> {
return lazy(LazyThreadSafetyMode.SYNCHRONIZED) {
getOrNull(clazz, qualifier, parameters)
}
}

/**
* Retrieve given dependency
* @param clazz - dependency class
Expand All @@ -71,6 +91,29 @@ object KoinJavaComponent {
)
}

/**
* Retrieve given dependency if available
* @param clazz - dependency class
* @param qualifier - bean canonicalName / optional
* @param scope - scope
* @param parameters - dependency parameters / optional
*/

@JvmStatic
@JvmOverloads
fun <T : Any> getOrNull(
clazz: Class<*>,
qualifier: Qualifier? = null,
parameters: ParametersDefinition? = null
): T? {
val kClass = clazz.kotlin
return getKoin().getOrNull(
kClass,
qualifier,
parameters
)
}

// /**
// * Retrieve given dependency
// * @param clazz - dependency class
Expand Down

0 comments on commit b1f74b3

Please sign in to comment.