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:
- This file contains the implementation of
ByteCodeWriter, which provides methods for writing various types of byte code instructions. - The writer supports common operations like writing integers, shorts, strings, and method calls in bytecode format.
- It also includes utility functions to convert hexadecimal strings into binary data and vice versa.
- This file contains the implementation of
-
DynamicClassLoader.kt:
- This file defines a dynamic class loader that can load classes from byte code arrays at runtime.
- The
definemethod takes a class name and its corresponding bytecode, and returns the loaded class object.
-
Types.kt:
- This file contains definitions for various constant pool types used in Java bytecode, such as UTF8 strings, integers, class entries, and method references.
- It also includes an enumeration of opcodes (instructions) that can be emitted in bytecode.
-
ClassBuilder.kt:
- This file provides a DSL-like interface for constructing Java classes dynamically.
- The
classDeffunction allows you to specify the access modifiers, superclass, and methods of a class. - Each method can have its instructions defined using the
InstructionBlockclass.
To use this project, follow these steps:
-
Add Dependencies:
- Ensure your project includes the necessary Kotlin and Java SDK dependencies (Java 24, Kotlin 2.1).
-
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()- Load and Use the Generated Class:
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