
Facilitates dynamic Java class generation and manipulation at runtime through a byte code writer. Features include customizable JVM bytecode instructions, dynamic class loading, and a DSL-like interface for class construction.
This project is focused on creating a byte code writer and related utilities to dynamically generate Java classes at runtime. The primary goal is to allow developers to write custom JVM bytecode instructions directly in Kotlin, enabling advanced scenarios such as runtime class generation and manipulation.
ByteCodeWriterJvm.kt:
ByteCodeWriter, which provides methods for writing various types of byte code instructions.DynamicClassLoader.kt:
define method takes a class name and its corresponding bytecode, and returns the loaded class object.Types.kt:
ClassBuilder.kt:
classDef function allows you to specify the access modifiers, superclass, and methods of a class.InstructionBlock class.To use this project, follow these steps:
Add Dependencies:
Create a Class Using ClassBuilder:
val myClass = classBuilder {
access = 33u // public final
name = "MyDynamicClass"
method {
access = 9u // public static
name = "main"
signature = "()V"
instructionBlock {
+Opcode.IConst0
+Opcode.Return
}
}
}
val bytecode = myClass.write() val classLoader = DynamicClassLoader(null)
val clazz = classLoader.define("MyDynamicClass", bytecode)
val mainMethod = clazz.getMethod("main")
mainMethod.invoke(null)This project is designed for advanced use cases where dynamic class generation and manipulation are required. It provides a flexible and powerful way to work with JVM bytecode directly from Kotlin code. For more detailed information, please refer to the source code files provided in the project repository
This project is focused on creating a byte code writer and related utilities to dynamically generate Java classes at runtime. The primary goal is to allow developers to write custom JVM bytecode instructions directly in Kotlin, enabling advanced scenarios such as runtime class generation and manipulation.
ByteCodeWriterJvm.kt:
ByteCodeWriter, which provides methods for writing various types of byte code instructions.DynamicClassLoader.kt:
define method takes a class name and its corresponding bytecode, and returns the loaded class object.Types.kt:
ClassBuilder.kt:
classDef function allows you to specify the access modifiers, superclass, and methods of a class.InstructionBlock class.To use this project, follow these steps:
Add Dependencies:
Create a Class Using ClassBuilder:
val myClass = classBuilder {
access = 33u // public final
name = "MyDynamicClass"
method {
access = 9u // public static
name = "main"
signature = "()V"
instructionBlock {
+Opcode.IConst0
+Opcode.Return
}
}
}
val bytecode = myClass.write() val classLoader = DynamicClassLoader(null)
val clazz = classLoader.define("MyDynamicClass", bytecode)
val mainMethod = clazz.getMethod("main")
mainMethod.invoke(null)This project is designed for advanced use cases where dynamic class generation and manipulation are required. It provides a flexible and powerful way to work with JVM bytecode directly from Kotlin code. For more detailed information, please refer to the source code files provided in the project repository