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:
Alexander Udalov
2021-02-03 21:21:55 +01:00
parent 35ec6b4f5c
commit fb296b5b95
4 changed files with 18 additions and 10 deletions
@@ -19,7 +19,6 @@ package kotlin.reflect.jvm.internal
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.descriptors.runtime.components.RuntimeModuleData
import org.jetbrains.kotlin.descriptors.runtime.components.tryLoadClass
import org.jetbrains.kotlin.descriptors.runtime.structure.createArrayType
import org.jetbrains.kotlin.descriptors.runtime.structure.safeClassLoader
import org.jetbrains.kotlin.descriptors.runtime.structure.wrapperByPrimitive
import org.jetbrains.kotlin.load.java.JvmAbi
@@ -19,7 +19,6 @@ package kotlin.reflect.jvm.internal
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.descriptors.TypeAliasDescriptor
import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor
import org.jetbrains.kotlin.descriptors.runtime.structure.createArrayType
import org.jetbrains.kotlin.descriptors.runtime.structure.parameterizedTypeArguments
import org.jetbrains.kotlin.descriptors.runtime.structure.primitiveByWrapper
import org.jetbrains.kotlin.types.KotlinType
@@ -101,6 +101,9 @@ private fun loadClass(classLoader: ClassLoader, packageName: String, className:
return classLoader.tryLoadClass(fqName)
}
internal fun Class<*>.createArrayType(): Class<*> =
java.lang.reflect.Array.newInstance(this, 0)::class.java
internal fun DescriptorVisibility.toKVisibility(): KVisibility? =
when (this) {
DescriptorVisibilities.PUBLIC -> KVisibility.PUBLIC