Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Annotations Reference

Complete reference for bytekin annotations.

@ModifyClass

Purpose

Marks a class as containing hook methods for bytecode transformation.

Usage

@ModifyClass("com.example.TargetClass")
public class MyHooks {
    // Hook methods here
}

Parameters

ParameterTypeRequiredDescription
classNameStringYesFully qualified name of the target class

Scope

Applied to class types only.

@Inject

Purpose

Inject code at specific points in methods.

Usage

@Inject(
    methodName = "myMethod",
    methodDesc = "(I)Ljava/lang/String;",
    at = At.HEAD
)
public static CallbackInfo hook(int param) { }

Parameters

ParameterTypeRequiredDescription
methodNameStringYesTarget method name
methodDescStringYesMethod descriptor (JVM format)
atAtYesWhere to inject code

Scope

Applied to methods only.

Return Type

Must return CallbackInfo.

@Invoke

Purpose

Intercept method calls.

Usage

@Invoke(
    targetMethodName = "parentMethod",
    targetMethodDesc = "()V",
    invokeMethodName = "childMethod",
    invokeMethodDesc = "(I)V",
    shift = Shift.BEFORE
)
public static CallbackInfo hook() { }

Parameters

ParameterTypeRequiredDescription
targetMethodNameStringYesMethod containing the call
targetMethodDescStringYesDescriptor of target method
invokeMethodNameStringYesName of method being called
invokeMethodDescStringYesDescriptor of called method
shiftShiftYesBEFORE or AFTER the call

Scope

Applied to methods only.

@Redirect

Purpose

Redirect method calls to different target.

Usage

@Redirect(
    targetMethodName = "oldMethod",
    targetMethodDesc = "()V",
    redirectMethodName = "newMethod",
    redirectMethodDesc = "()V"
)
public static void hook() { }

Parameters

ParameterTypeRequiredDescription
targetMethodNameStringYesMethod containing the call
targetMethodDescStringYesDescriptor of target method
redirectMethodNameStringYesName of redirect method
redirectMethodDescStringYesDescriptor of redirect method

@ModifyConstant

Purpose

Modify constant values in bytecode.

Usage

@ModifyConstant(
    methodName = "getConfig",
    oldValue = "dev",
    newValue = "prod"
)
public static CallbackInfo hook() { }

Parameters

ParameterTypeRequiredDescription
methodNameStringYesMethod containing the constant
oldValueObjectYesOriginal constant value
newValueObjectYesNew constant value

@ModifyVariable

Purpose

Modify local variable values.

Usage

@ModifyVariable(
    methodName = "process",
    variableIndex = 1
)
public static void hook(String param) { }

Parameters

ParameterTypeRequiredDescription
methodNameStringYesTarget method name
variableIndexintYesLocal variable slot index

Enum: At

Values

ValueDescription
HEADAt start of method, before all code
RETURNBefore each return statement
TAILAt end of method

Enum: Shift

Values

ValueDescription
BEFOREExecute hook before method call
AFTERExecute hook after method call

Next Steps