Use Class.forName instead of ClassLoader.loadClass in reflection

This fixes an issue in constructing annotation instances with array
class elements. For some reason, behavior of `ClassLoader.loadClass`
differs from `Class.forName` in handling arrays, namely:

* `loadClass("[Ltest.Foo;")` returns null
* `Class.forName("[Ltest.Foo;")` returns class for array of test.Foo

Overall, there doesn't seem to be any way to load an array class with
`CLassLoader.loadClass`.

We pass initialize=false to forName because this is the behavior of
ClassLoader.loadClass: it doesn't perform class initialization (e.g.
<clinit> is not executed).

 #KT-31318 Fixed
This commit is contained in:
Alexander Udalov
2019-07-03 13:35:25 +02:00
parent 72d74ff888
commit a6be6f4986
7 changed files with 36 additions and 7 deletions
@@ -90,8 +90,8 @@ private fun loadClass(classLoader: ClassLoader, packageName: String, className:
}
var fqName = "$packageName.${className.replace('.', '$')}"
repeat(arrayDimensions) {
fqName = "[$fqName"
if (arrayDimensions > 0) {
fqName = "[".repeat(arrayDimensions) + "L$fqName;"
}
return classLoader.tryLoadClass(fqName)