InsnTarget

interface InsnTarget(source)

Used for manipulating bytecode around a target sequence of bytecode instructions in an InsnList. Because ASM's instruction classes don't implement equals, a custom method is used for matching instructions, which currently supports most of them. See insnEquals for a full list.

Provides a shared label registry through TargetedAssembly for sharing labels across insertions within the same target.

Use InsnList.findTarget or one of its overloaded functions to create an appropriate implementation of this interface.

Example usage:

val method = MethodNode().koffee {
ldc("Hello World")
astore_0
ldc("Foo Bar")
astore_1

getstatic("java/lang/System", "out", "java/io/PrintStream")
// Load "Hello World"
aload_0
invokevirtual("java/io/PrintStream", "println", "(Ljava/lang/String;)V")
}
// Locate the sequence of printing instructions
val target = method.findTarget {
getstatic("java/lang/System", "out", "java/io/PrintStream")
aload_0 // <- Offset 1
invokevirtual("java/io/PrintStream", "println", "(Ljava/lang/String;)V")
}
// Replace the loaded variable on the stack
target.insert(1) {
pop
aload_1
}

See also

Inheritors

Types

Link copied to clipboard
class Found(origin: InsnList, first: AbstractInsnNode, last: AbstractInsnNode) : InsnTarget

An acitve implementation of InsnTarget used for successfully located targets.

Link copied to clipboard

A dummy NO-OP implementation of InsnTarget used when the searched target was not found.

Functions

Link copied to clipboard
abstract fun find(index: Int): AbstractInsnNode

Find an insn node in this target at a specific index

Link copied to clipboard
abstract fun insert(offset: Int = 0, block: TargetedAssembly.() -> Unit)

Insert instructions at the first insn node in this target.

Link copied to clipboard
abstract fun insertAfter(offset: Int = 0, block: TargetedAssembly.() -> Unit)

Insert instructions after the last insn node in this target.

Link copied to clipboard
abstract fun insertBefore(offset: Int = 0, block: TargetedAssembly.() -> Unit)

Insert instructions before the target.