Skip to content

Instantly share code, notes, and snippets.

@doorbash
Last active September 18, 2020 21:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save doorbash/56128c1d2dab152cb2e769dffb75421a to your computer and use it in GitHub Desktop.
Save doorbash/56128c1d2dab152cb2e769dffb75421a to your computer and use it in GitHub Desktop.
import kotlin.reflect.jvm.jvmErasure
import kotlin.reflect.typeOf
class Y
class X<T>
@OptIn(ExperimentalStdlibApi::class)
inline fun <reified T> T.getGenericType(): Class<Any>? = typeOf<T>().arguments[0].type?.jvmErasure?.java as Class<Any>?
fun main() {
println(X<Float>().getGenericType())
println(X<Y>().getGenericType())
}
/*
float
float
class Y
class Y
*/
@doorbash
Copy link
Author

implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment