Do not create Array instance in kotlin-reflect where not necessary
Although the previous code of computing JVM internal name from a Class instance was shorter, it led to unnecessary creation of array types, which is less performant and makes configuration of kotlin-reflect for GraalVM native-image more verbose. Unfortunately I didn't succeed in writing a test for this, since `Array.newInstance` calls a native method which doesn't record any trace of it being called in the class loader: the resulting array type never goes through findClass/loadClass, and is not visible via findLoadedClass. #KT-44594 Fixed
This commit is contained in:
+15
-8
@@ -67,16 +67,23 @@ val Class<*>.classId: ClassId
|
||||
}
|
||||
|
||||
val Class<*>.desc: String
|
||||
get() {
|
||||
if (this == Void.TYPE) return "V"
|
||||
// This is a clever exploitation of a format returned by Class.getName(): for arrays, it's almost an internal name,
|
||||
// but with '.' instead of '/'
|
||||
return createArrayType().name.substring(1).replace('.', '/')
|
||||
get() = when {
|
||||
isPrimitive -> when (name) {
|
||||
"boolean" -> "Z"
|
||||
"char" -> "C"
|
||||
"byte" -> "B"
|
||||
"short" -> "S"
|
||||
"int" -> "I"
|
||||
"float" -> "F"
|
||||
"long" -> "J"
|
||||
"double" -> "D"
|
||||
"void" -> "V"
|
||||
else -> throw IllegalArgumentException("Unsupported primitive type: $this")
|
||||
}
|
||||
isArray -> name.replace('.', '/')
|
||||
else -> "L${name.replace('.', '/')};"
|
||||
}
|
||||
|
||||
fun Class<*>.createArrayType(): Class<*> =
|
||||
Array.newInstance(this, 0)::class.java
|
||||
|
||||
/**
|
||||
* @return all arguments of a parameterized type, including those of outer classes in case this type represents an inner generic.
|
||||
* The returned list starts with the arguments to the innermost class, then continues with those of its outer class, and so on.
|
||||
|
||||
Reference in New Issue
Block a user