Add debug property for dumping classes generated with ASM

This commit is contained in:
Iaroslav Postovalov 2021-08-21 19:58:06 +07:00
parent 0b90dd4df2
commit 7e0820d861

View File

@ -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<T>(
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<Any>::class.java))
.invoke(constants.toTypedArray()) as Expression<T>
(if (hasConstants)
l.findConstructor(cls, MethodType.methodType(Void.TYPE, Array<Any>::class.java))(constants.toTypedArray())
else
l.findConstructor(cls, MethodType.methodType(Void.TYPE)).invoke() as Expression<T>
l.findConstructor(cls, MethodType.methodType(Void.TYPE))()) as Expression<T>
}
/**