23

Koffee: Java bytecode assembler as a Kotlin DSL

 4 years ago
source link: https://www.tuicool.com/articles/uyauEbu
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Koffee

A Kotlin DSL wrapping ObjectWeb's ASM bytecode manipulation library. (In particular, asm-tree .)

Limitations

Since the DSL is in Kotlin, there are a few reserved words that get in our way. In order to get around this, we provide properties with the literal name and use backtick-escaping, and the preferred approach of a leading underscore before the reserved name.

  • Since 'interface' is a globally reserved keyword in Kotlin, the access flag interface can be referenced using interface` or _interface
  • Since 'super' is a globally reserved keyword in Kotlin, the access flag super can be referenced using super` or _super
  • Since 'return' is a globally reserved keyword in Kotlin, the instruction return can be referenced using return` or _return

Example

val helloWorld: ClassNode = assembleClass(public, "com/example/HelloWorld") {
    name = "com/example/HelloWorld"

    method(public + static, "main", type(Array<String>::class)) {
        getstatic(System::class, "out", PrintStream::class)
        ldc("Hello, world!")
        invokevirtual(PrintStream::class, void, String::class)
        _return
    }
}

Goals

  • Koffee is designed to be used in projects that already make use of ASM, so we don't need to abstract too far away.
  • Despite this, Koffee should aim to make the use of ASM easier - High-level abstractions are acceptable if they are common. (e.g. 'load int constant' can be made uniform)

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK