From 7e0820d861763535e5c163215feb6d3f3bb86432 Mon Sep 17 00:00:00 2001 From: Iaroslav Postovalov Date: Sat, 21 Aug 2021 19:58:06 +0700 Subject: [PATCH] Add debug property for dumping classes generated with ASM --- .../kscience/kmath/asm/internal/AsmBuilder.kt | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/kmath-ast/src/jvmMain/kotlin/space/kscience/kmath/asm/internal/AsmBuilder.kt b/kmath-ast/src/jvmMain/kotlin/space/kscience/kmath/asm/internal/AsmBuilder.kt index de7da30df..418d6141b 100644 --- a/kmath-ast/src/jvmMain/kotlin/space/kscience/kmath/asm/internal/AsmBuilder.kt +++ b/kmath-ast/src/jvmMain/kotlin/space/kscience/kmath/asm/internal/AsmBuilder.kt @@ -14,9 +14,11 @@ import space.kscience.kmath.expressions.Expression import space.kscience.kmath.expressions.MST import java.lang.invoke.MethodHandles import java.lang.invoke.MethodType +import java.nio.file.Paths import java.util.stream.Collectors.toMap import kotlin.contracts.InvocationKind import kotlin.contracts.contract +import kotlin.io.path.writeBytes /** * ASM Builder is a structure that abstracts building a class designated to unwrap [MST] to plain Java expression. @@ -194,15 +196,18 @@ internal class AsmBuilder( visitEnd() } - val cls = classLoader.defineClass(className, classWriter.toByteArray()) - // java.io.File("dump.class").writeBytes(classWriter.toByteArray()) + val binary = classWriter.toByteArray() + val cls = classLoader.defineClass(className, binary) + + if (System.getProperty("space.kscience.communicator.prettyapi.dump.generated.classes") == "1") + Paths.get("$className.class").writeBytes(binary) + val l = MethodHandles.publicLookup() - if (hasConstants) - l.findConstructor(cls, MethodType.methodType(Void.TYPE, Array::class.java)) - .invoke(constants.toTypedArray()) as Expression + (if (hasConstants) + l.findConstructor(cls, MethodType.methodType(Void.TYPE, Array::class.java))(constants.toTypedArray()) else - l.findConstructor(cls, MethodType.methodType(Void.TYPE)).invoke() as Expression + l.findConstructor(cls, MethodType.methodType(Void.TYPE))()) as Expression } /**