Replace reflective constructor invocation with method handle

This commit is contained in:
Iaroslav Postovalov 2020-12-09 14:43:37 +07:00
parent 95c1504c00
commit 07d6d89192
No known key found for this signature in database
GPG Key ID: 46E15E4A31B3BCD7

View File

@ -7,6 +7,8 @@ import org.objectweb.asm.*
import org.objectweb.asm.Opcodes.* import org.objectweb.asm.Opcodes.*
import org.objectweb.asm.Type.* import org.objectweb.asm.Type.*
import org.objectweb.asm.commons.InstructionAdapter import org.objectweb.asm.commons.InstructionAdapter
import java.lang.invoke.MethodHandles
import java.lang.invoke.MethodType
import java.util.stream.Collectors.toMap import java.util.stream.Collectors.toMap
import kotlin.contracts.InvocationKind import kotlin.contracts.InvocationKind
import kotlin.contracts.contract import kotlin.contracts.contract
@ -187,13 +189,14 @@ internal class AsmBuilder<T>(
visitEnd() visitEnd()
} }
// java.io.File("dump.class").writeBytes(classWriter.toByteArray()) val cls = classLoader.defineClass(className, classWriter.toByteArray())
val l = MethodHandles.publicLookup()
classLoader if (hasConstants)
.defineClass(className, classWriter.toByteArray()) l.findConstructor(cls, MethodType.methodType(Void.TYPE, Array<Any>::class.java))
.constructors .invoke(constants.toTypedArray()) as Expression<T>
.first() else
.newInstance(*(constants.toTypedArray().wrapToArrayIf { hasConstants })) as Expression<T> l.findConstructor(cls, MethodType.methodType(Void.TYPE)).invoke() as Expression<T>
} }
/** /**
@ -248,7 +251,7 @@ internal class AsmBuilder<T>(
r.internalName, r.internalName,
"valueOf", "valueOf",
getMethodDescriptor(r, primitive), getMethodDescriptor(r, primitive),
false false,
) )
} }