Skip to content
On this page

Common

InjectApi

top.katton.api.injectcommon/src/main/kotlin/top/katton/api/inject/InjectApi.kt
Handle for a registered injection, used for rollback operations.

InjectionHandle

CommonClass
kotlin
class InjectionHandle internal constructor( val id: String )

Handle for a registered injection, used for rollback operations.

Properties

PropertyDescription
idInjection registration id, used for rollback

InjectionInvocationContext

CommonClass
kotlin
class InjectionInvocationContext internal constructor( private val delegate: InjectionManager.InjectionInvocation )

Invocation context passed to unsafe callbacks.

Provides access to the method being invoked, the receiver instance, arguments, and control over the invocation flow.

Properties

PropertyDescription
delegateThe underlying injection invocation delegate

InjectionInvocationContext.method

CommonProperty
kotlin
val method: Method

Method currently being invoked.

InjectionInvocationContext.instance

CommonProperty
kotlin
val instance: Any?

Method receiver instance, or null for static methods.

InjectionInvocationContext.arguments

CommonProperty
kotlin
val arguments: Array<Any?>

Raw argument array.

InjectionInvocationContext.owner

CommonProperty
kotlin
val owner: String?

Bound script owner for this invocation.

InjectionInvocationContext.setArgument

CommonFunction
kotlin
fun setArgument(index: Int, value: Any?)

Mutates argument at [index] for current invocation.

Parameters

ParameterDescription
indexThe argument index to modify
valueThe new value for the argument

InjectionInvocationContext.cancel

CommonFunction
kotlin
fun cancel()

Cancels current invocation. Return value becomes type default if not overridden.

InjectionInvocationContext.cancelWith

CommonFunction
kotlin
fun cancelWith(returnValue: Any?)

Cancels current invocation and overrides return value immediately.

Parameters

ParameterDescription
returnValueThe value to return instead of executing the method

InjectionInvocationContext.setReturnValue

CommonFunction
kotlin
fun setReturnValue(returnValue: Any?)

Overrides return value in after phase.

Parameters

ParameterDescription
returnValueThe value to return instead of the original result

ConstructorInvocationContext

CommonClass
kotlin
class ConstructorInvocationContext internal constructor( private val delegate: InjectionManager.ConstructorInvocation )

Constructor invocation context passed to unsafe constructor callbacks.

Properties

PropertyDescription
delegateThe underlying constructor invocation delegate

ConstructorInvocationContext.constructor

CommonProperty
kotlin
val constructor: Constructor<*>

Constructor currently being invoked.

ConstructorInvocationContext.instance

CommonProperty
kotlin
val instance: Any?

Constructed instance (this) when available.

ConstructorInvocationContext.arguments

CommonProperty
kotlin
val arguments: Array<Any?>

Raw constructor argument array.

ConstructorInvocationContext.owner

CommonProperty
kotlin
val owner: String?

Bound script owner for this invocation.

injectBefore

CommonFunction
kotlin
fun injectBefore(targetClassName: String, methodName: String, parameterTypeNames: List<String> = emptyList(), owner: String? = null, handler: (InjectionInvocationContext) -> Unit): InjectionHandle

Injects a callback before target method execution (string-based overload).

Parameters

ParameterDescription
targetClassNametarget class fully-qualified name
methodNametarget method name
parameterTypeNamesparameter type names, e.g. int, java.lang.String
ownerscript owner (nullable, auto-resolved from script context if null)
handlerbefore callback

Returns

injection handle, usable by [rollbackUnsafe]

injectBefore

CommonFunction
kotlin
fun injectBefore(method: Method, owner: String? = null, handler: (InjectionInvocationContext) -> Unit): InjectionHandle

Injects a callback before target method execution (Method overload).

Prefer this overload when a reflected [Method] is already available, to avoid string-signature assembly errors.

injectAfter

CommonFunction
kotlin
fun injectAfter(targetClassName: String, methodName: String, parameterTypeNames: List<String> = emptyList(), owner: String? = null, handler: (InjectionInvocationContext, Any?, Throwable?) -> Unit): InjectionHandle

Injects a callback after target method execution (string-based overload).

Parameters

ParameterDescription
targetClassNametarget class fully-qualified name
methodNametarget method name
parameterTypeNamesparameter type names, e.g. int, java.lang.String
ownerscript owner (nullable, auto-resolved from script context if null)
handlerafter callback with result and throwable

Returns

injection handle, usable by [rollbackUnsafe]

injectConstructorBefore

CommonFunction
kotlin
fun injectConstructorBefore(targetClassName: String, parameterTypeNames: List<String> = emptyList(), owner: String? = null, handler: (ConstructorInvocationContext) -> Unit): InjectionHandle

Injects a callback before constructor execution (string-based overload).

Parameters

ParameterDescription
targetClassNametarget class fully-qualified name
parameterTypeNamesconstructor parameter type names, e.g. int, java.lang.String
ownerscript owner (nullable, auto-resolved from script context if null)
handlerconstructor-before callback

Returns

injection handle, usable by [rollbackUnsafe]

injectConstructorBefore

CommonFunction
kotlin
fun injectConstructorBefore(constructor: Constructor<*>, owner: String? = null, handler: (ConstructorInvocationContext) -> Unit): InjectionHandle

Injects a callback before constructor execution (Constructor overload).

injectConstructorAfter

CommonFunction
kotlin
fun injectConstructorAfter(targetClassName: String, parameterTypeNames: List<String> = emptyList(), owner: String? = null, handler: (ConstructorInvocationContext) -> Unit): InjectionHandle

Injects a callback after constructor execution (string-based overload).

Parameters

ParameterDescription
targetClassNametarget class fully-qualified name
parameterTypeNamesconstructor parameter type names, e.g. int, java.lang.String
ownerscript owner (nullable, auto-resolved from script context if null)
handlerconstructor-after callback

Returns

injection handle, usable by [rollbackUnsafe]

injectConstructorAfter

CommonFunction
kotlin
fun injectConstructorAfter(constructor: Constructor<*>, owner: String? = null, handler: (ConstructorInvocationContext) -> Unit): InjectionHandle

Injects a callback after constructor execution (Constructor overload).

injectAfter

CommonFunction
kotlin
fun injectAfter(method: Method, owner: String? = null, handler: (InjectionInvocationContext, Any?, Throwable?) -> Unit): InjectionHandle

Injects a callback after target method execution (Method overload).

Prefer this overload when a reflected [Method] is already available.

Parameters

ParameterDescription
methodThe target Method to inject into
ownerScript owner (nullable, auto-resolved from script context if null)
handlerAfter callback with result and throwable

Returns

Injection handle, usable by [rollbackUnsafe]

replace

CommonFunction
kotlin
fun replace(targetClassName: String, methodName: String, parameterTypeNames: List<String> = emptyList(), owner: String? = null, handler: (InjectionInvocationContext) -> Any?): InjectionHandle

Replaces entire target method body (string-based overload).

The handler return value becomes the method return value. This completely bypasses the original method implementation.

Parameters

ParameterDescription
targetClassNameTarget class fully-qualified name
methodNameTarget method name
parameterTypeNamesParameter type names, e.g. int, java.lang.String
ownerScript owner (nullable, auto-resolved from script context if null)
handlerReplacement handler that returns the method result

Returns

Injection handle, usable by [rollbackUnsafe]

replace

CommonFunction
kotlin
fun replace(method: Method, owner: String? = null, handler: (InjectionInvocationContext) -> Any?): InjectionHandle

Replaces entire target method body (Method overload).

Parameters

ParameterDescription
methodThe target Method to replace
ownerScript owner (nullable, auto-resolved from script context if null)
handlerReplacement handler that returns the method result

Returns

Injection handle, usable by [rollbackUnsafe]

redirect

CommonFunction
kotlin
fun redirect(sourceClassName: String, sourceMethodName: String, sourceParameterTypeNames: List<String> = emptyList(), targetClassName: String, targetMethodName: String, targetParameterTypeNames: List<String> = emptyList(), owner: String? = null): InjectionHandle

Redirects a source method to another target method (string-based overload).

All calls to the source method will be redirected to the target method instead.

Parameters

ParameterDescription
sourceClassNameSource class fully-qualified name
sourceMethodNameSource method name to redirect from
sourceParameterTypeNamesSource method parameter type names
targetClassNameTarget class fully-qualified name
targetMethodNameTarget method name to redirect to
targetParameterTypeNamesTarget method parameter type names
ownerScript owner (nullable, auto-resolved from script context if null)

Returns

Injection handle, usable by [rollbackUnsafe]

redirect

CommonFunction
kotlin
fun redirect(sourceMethod: Method, targetMethod: Method, owner: String? = null): InjectionHandle

Redirects a source method to another target method (Method overload).

Parameters

ParameterDescription
sourceMethodThe source Method to redirect from
targetMethodThe target Method to redirect to
ownerScript owner (nullable, auto-resolved from script context if null)

Returns

Injection handle, usable by [rollbackUnsafe]

rollbackUnsafe

CommonFunction
kotlin
fun rollbackUnsafe(handle: InjectionHandle): Boolean

Rolls back one unsafe injection by handle.

Parameters

ParameterDescription
handleThe injection handle to roll back

Returns

true if the injection was found and removed, false otherwise

rollbackUnsafeByOwner

CommonFunction
kotlin
fun rollbackUnsafeByOwner(owner: String)

Rolls back all unsafe injections by owner.

Parameters

ParameterDescription
ownerThe script owner whose injections should be removed