I am looking to call the following Java method signature from Kotlin
<C extends javax.cache.configuration.Configuration<K,V>> C getConfiguration(Class<C> clazz)
I think in Java the standard way would be to call
CacheConfiguration configuration = igniteCache.getConfiguration(CacheConfiguration.class);
I can't figure out how to express this in Kotlin to get this object.
I've tried
val configuration = igniteCache.getConfiguration(CacheConfiguration::class.java)
but this gives me the following error
Type mismatch: inferred type is CacheConfiguration<*, *> but Configuration<K!, V!>! was expected
So what is the correct way to call this method?
Update
https://stackoverflow.com/a/57016668/10039185 provided the answer how to do this.
I'm just adding two snippets with fuller Ignite specific examples of how to do this
Using unchecked cast
private fun <K, V> checkCacheUsingUncheckedCast(igniteCache: IgniteCache<K, V>?) {
val configuration = igniteCache?.getConfiguration(CacheConfiguration::class.java as Class<CacheConfiguration<K, V>>)
if (configuration != null) {
// do something with the configuration
}
}
Using reified type
private fun <K, V> checkCacheUsingReifiedType(igniteCache: IgniteCache<K, V>?) {
val configuration = igniteCache?.configuration<K, V, CacheConfiguration<K, V>>()
if (configuration != null) {
// do something with the configuration
}
}
private inline fun <K, V, reified C : Configuration<K, V>> IgniteCache<K, V>.configuration() = getConfiguration(C::class.java)