Skip to content
On this page
Common

InjectApi

top.katton.api.injectcommon/src/main/kotlin/top/katton/api/inject/InjectApi.kt
注册注入的句柄,用于回滚操作。

InjectionHandle ​

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

注册注入的句柄,用于回滚操作。

Properties ​

PropertyDescription
id注入注册 ID,用于回滚。

InjectionInvocationContext ​

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

传给不安全回调的调用上下文。

可访问正在调用的方法、接收实例、参数,并控制调用流程。

Properties ​

PropertyDescription
delegate底层的注入调用委托。

InjectionInvocationContext.method ​

InjectionInvocationContext.methodCommonProperty
kotlin
val method: Method

当前正在调用的方法。

InjectionInvocationContext.instance ​

InjectionInvocationContext.instanceCommonProperty
kotlin
val instance: Any?

方法接收实例;对于静态方法则为 null。

InjectionInvocationContext.arguments ​

InjectionInvocationContext.argumentsCommonProperty
kotlin
val arguments: Array<Any?>

原始参数数组。

InjectionInvocationContext.owner ​

InjectionInvocationContext.ownerCommonProperty
kotlin
val owner: String?

这次调用绑定的脚本归属。

InjectionInvocationContext.setArgument ​

InjectionInvocationContext.setArgumentCommonFunction
kotlin
fun setArgument(index: Int, value: Any?)

修改当前调用中指定位置的参数。

Parameters ​

ParameterDescription
index要修改的参数下标。
value参数的新值。

InjectionInvocationContext.cancel ​

InjectionInvocationContext.cancelCommonFunction
kotlin
fun cancel()

取消当前调用。若未覆写返回值,则使用该类型的默认值。

InjectionInvocationContext.cancelWith ​

InjectionInvocationContext.cancelWithCommonFunction
kotlin
fun cancelWith(returnValue: Any?)

取消当前调用,并立即指定返回值。

Parameters ​

ParameterDescription
returnValue用来替代方法执行结果的返回值。

InjectionInvocationContext.setReturnValue ​

InjectionInvocationContext.setReturnValueCommonFunction
kotlin
fun setReturnValue(returnValue: Any?)

在 after 阶段覆写返回值。

Parameters ​

ParameterDescription
returnValue用来替代原始结果的返回值。

ConstructorInvocationContext ​

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

传给不安全构造器回调的构造器调用上下文。

Properties ​

PropertyDescription
delegate底层的构造器调用委托。

ConstructorInvocationContext.constructor ​

ConstructorInvocationContext.constructorCommonProperty
kotlin
val constructor: Constructor<*>

当前正在调用的构造器。

ConstructorInvocationContext.instance ​

ConstructorInvocationContext.instanceCommonProperty
kotlin
val instance: Any?

已构造的实例(this),如果可用。

ConstructorInvocationContext.arguments ​

ConstructorInvocationContext.argumentsCommonProperty
kotlin
val arguments: Array<Any?>

Raw constructor argument array.

ConstructorInvocationContext.owner ​

ConstructorInvocationContext.ownerCommonProperty
kotlin
val owner: String?

Bound script owner for this invocation.

injectBefore ​

injectBeforeCommonFunction
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 ​

injectBeforeCommonFunction
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 ​

injectAfterCommonFunction
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 ​

injectConstructorBeforeCommonFunction
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 ​

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

Injects a callback before constructor execution (Constructor overload).

injectConstructorAfter ​

injectConstructorAfterCommonFunction
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 ​

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

Injects a callback after constructor execution (Constructor overload).

injectAfter ​

injectAfterCommonFunction
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 ​

replaceCommonFunction
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 ​

replaceCommonFunction
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 ​

redirectCommonFunction
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 ​

redirectCommonFunction
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 ​

rollbackUnsafeCommonFunction
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 ​

rollbackUnsafeByOwnerCommonFunction
kotlin
fun rollbackUnsafeByOwner(owner: String)

Rolls back all unsafe injections by owner.

Parameters ​

ParameterDescription
ownerThe script owner whose injections should be removed