From 07d6d891928d89d3eb14917cfbf78d790e4883b8 Mon Sep 17 00:00:00 2001 From: Iaroslav Postovalov Date: Wed, 9 Dec 2020 14:43:37 +0700 Subject: [PATCH] Replace reflective constructor invocation with method handle --- .../kscience/kmath/asm/internal/AsmBuilder.kt | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/kmath-ast/src/jvmMain/kotlin/kscience/kmath/asm/internal/AsmBuilder.kt b/kmath-ast/src/jvmMain/kotlin/kscience/kmath/asm/internal/AsmBuilder.kt index eb0f60f4d..d146afeee 100644 --- a/kmath-ast/src/jvmMain/kotlin/kscience/kmath/asm/internal/AsmBuilder.kt +++ b/kmath-ast/src/jvmMain/kotlin/kscience/kmath/asm/internal/AsmBuilder.kt @@ -7,6 +7,8 @@ import org.objectweb.asm.* import org.objectweb.asm.Opcodes.* import org.objectweb.asm.Type.* import org.objectweb.asm.commons.InstructionAdapter +import java.lang.invoke.MethodHandles +import java.lang.invoke.MethodType import java.util.stream.Collectors.toMap import kotlin.contracts.InvocationKind import kotlin.contracts.contract @@ -187,13 +189,14 @@ internal class AsmBuilder( visitEnd() } -// java.io.File("dump.class").writeBytes(classWriter.toByteArray()) + val cls = classLoader.defineClass(className, classWriter.toByteArray()) + val l = MethodHandles.publicLookup() - classLoader - .defineClass(className, classWriter.toByteArray()) - .constructors - .first() - .newInstance(*(constants.toTypedArray().wrapToArrayIf { hasConstants })) as Expression + if (hasConstants) + l.findConstructor(cls, MethodType.methodType(Void.TYPE, Array::class.java)) + .invoke(constants.toTypedArray()) as Expression + else + l.findConstructor(cls, MethodType.methodType(Void.TYPE)).invoke() as Expression } /** @@ -248,7 +251,7 @@ internal class AsmBuilder( r.internalName, "valueOf", getMethodDescriptor(r, primitive), - false + false, ) }