Pihentagy
06/23/2023, 8:16 AM@GetMapping("/health-check")
fun healthCheck() = runBlocking {
mapOf(
"version" to javaClass.getPackage().implementationVersion,
)
}
returns
{
"version": null
}
however this one
@GetMapping("/health-check")
fun healthCheck() =
mapOf(
"version" to javaClass.getPackage().implementationVersion,
)
returns something.
Why it that?Sam
06/23/2023, 8:19 AMjavaClass
is an extension property on the current receiver. Inside runBlocking
, the receiver is replaced by a CoroutineScope
, so javaClass
actually gets you the class of the coroutine scope, which I guess is not what you want!Sam
06/23/2023, 8:22 AMclass MyClass {
fun myFun() = runBlocking {
val wrongClass = this.javaClass // ❌
val myClass = this@MyClass.javaClass // ✅
}
}
Pihentagy
06/23/2023, 8:30 AMSam
06/23/2023, 8:33 AMPihentagy
06/23/2023, 8:34 AMSam
06/23/2023, 8:38 AM@GetMapping("/health-check")
fun healthCheck() = runBlocking {
mapOf(
"version" to getImplementationVersion(),
)
}
fun getImplementationVersion() = javaClass.`package`.implementationVersion