Fix permgen problems with script launching by avoiding reflection as much as possible
This commit is contained in:
+13
-9
@@ -225,9 +225,8 @@ object KotlinToJVMBytecodeCompiler {
|
||||
|
||||
try {
|
||||
try {
|
||||
tryConstructClass(scriptClass.kotlin, scriptArgs)
|
||||
?: throw RuntimeException("unable to find appropriate constructor for class ${scriptClass.name} accepting arguments $scriptArgs\n" +
|
||||
"\tconstructors: \n\t\t${scriptClass.kotlin.constructors.joinToString("\n\t\t", "(") { it.parameters.joinToString { it.type.toString() } }}")
|
||||
tryConstructClass(scriptClass, scriptArgs)
|
||||
?: throw RuntimeException("unable to find appropriate constructor for class ${scriptClass.name} accepting arguments $scriptArgs\n")
|
||||
}
|
||||
finally {
|
||||
// NB: these lines are required (see KT-9546) but aren't covered by tests
|
||||
@@ -260,9 +259,9 @@ object KotlinToJVMBytecodeCompiler {
|
||||
}
|
||||
|
||||
@TestOnly
|
||||
fun tryConstructClassPub(scriptClass: KClass<out Any>, scriptArgs: List<String>): Any? = tryConstructClass(scriptClass, scriptArgs)
|
||||
fun tryConstructClassPub(scriptClass: Class<*>, scriptArgs: List<String>): Any? = tryConstructClass(scriptClass, scriptArgs)
|
||||
|
||||
private fun tryConstructClass(scriptClass: KClass<out Any>, scriptArgs: List<String>): Any? {
|
||||
private fun tryConstructClass(scriptClass: Class<*>, scriptArgs: List<String>): Any? {
|
||||
|
||||
fun convertPrimitive(type: KType?, arg: String): Any? =
|
||||
when (type) {
|
||||
@@ -312,10 +311,15 @@ object KotlinToJVMBytecodeCompiler {
|
||||
return state
|
||||
}
|
||||
|
||||
for (ctor in scriptClass.constructors) {
|
||||
val (ctorArgs, scriptArgsLeft) = ctor.parameters.fold(Pair(emptyList<Any>(), scriptArgs), ::foldingFunc)
|
||||
if (ctorArgs.size == ctor.parameters.size && (scriptArgsLeft == null || scriptArgsLeft.isEmpty()))
|
||||
return ctor.call(*ctorArgs.toTypedArray())
|
||||
try {
|
||||
return scriptClass.getConstructor(Array<String>::class.java).newInstance(*arrayOf<Any>(scriptArgs.toTypedArray()))
|
||||
}
|
||||
catch (e: java.lang.NoSuchMethodException) {
|
||||
for (ctor in scriptClass.kotlin.constructors) {
|
||||
val (ctorArgs, scriptArgsLeft) = ctor.parameters.fold(Pair(emptyList<Any>(), scriptArgs), ::foldingFunc)
|
||||
if (ctorArgs.size == ctor.parameters.size && (scriptArgsLeft == null || scriptArgsLeft.isEmpty()))
|
||||
return ctor.call(*ctorArgs.toTypedArray())
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user