Skip to content
On this page
通用

InjectApi

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

InjectionHandle ​

InjectionHandle通用类
kotlin
class InjectionHandle internal constructor( val id: String )

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

属性 ​

属性说明
id注入注册 ID,用于回滚。

InjectionInvocationContext ​

InjectionInvocationContext通用类
kotlin
class InjectionInvocationContext internal constructor( private val delegate: InjectionManager.InjectionInvocation )

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

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

属性 ​

属性说明
delegate底层的注入调用委托。

InjectionInvocationContext.method ​

InjectionInvocationContext.method通用属性
kotlin
val method: Method

当前正在调用的方法。

InjectionInvocationContext.instance ​

InjectionInvocationContext.instance通用属性
kotlin
val instance: Any?

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

InjectionInvocationContext.arguments ​

InjectionInvocationContext.arguments通用属性
kotlin
val arguments: Array<Any?>

原始参数数组。

InjectionInvocationContext.owner ​

InjectionInvocationContext.owner通用属性
kotlin
val owner: String?

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

InjectionInvocationContext.setArgument ​

InjectionInvocationContext.setArgument通用函数
kotlin
fun setArgument(index: Int, value: Any?)

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

参数 ​

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

InjectionInvocationContext.cancel ​

InjectionInvocationContext.cancel通用函数
kotlin
fun cancel()

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

InjectionInvocationContext.cancelWith ​

InjectionInvocationContext.cancelWith通用函数
kotlin
fun cancelWith(returnValue: Any?)

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

参数 ​

参数说明
returnValue用来替代方法执行结果的返回值。

InjectionInvocationContext.setReturnValue ​

InjectionInvocationContext.setReturnValue通用函数
kotlin
fun setReturnValue(returnValue: Any?)

在 after 阶段覆写返回值。

参数 ​

参数说明
returnValue用来替代原始结果的返回值。

ConstructorInvocationContext ​

ConstructorInvocationContext通用类
kotlin
class ConstructorInvocationContext internal constructor( private val delegate: InjectionManager.ConstructorInvocation )

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

属性 ​

属性说明
delegate底层的构造器调用委托。

ConstructorInvocationContext.constructor ​

ConstructorInvocationContext.constructor通用属性
kotlin
val constructor: Constructor<*>

当前正在调用的构造器。

ConstructorInvocationContext.instance ​

ConstructorInvocationContext.instance通用属性
kotlin
val instance: Any?

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

ConstructorInvocationContext.arguments ​

ConstructorInvocationContext.arguments通用属性
kotlin
val arguments: Array<Any?>

Raw constructor argument array.

ConstructorInvocationContext.owner ​

ConstructorInvocationContext.owner通用属性
kotlin
val owner: String?

Bound script owner for this invocation.

injectBefore ​

injectBefore通用函数
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).

参数 ​

参数说明
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

返回值 ​

injection handle, usable by [rollbackUnsafe]

injectBefore ​

injectBefore通用函数
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 ​

injectAfter通用函数
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).

参数 ​

参数说明
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

返回值 ​

injection handle, usable by [rollbackUnsafe]

injectConstructorBefore ​

injectConstructorBefore通用函数
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).

参数 ​

参数说明
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

返回值 ​

injection handle, usable by [rollbackUnsafe]

injectConstructorBefore ​

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

Injects a callback before constructor execution (Constructor overload).

injectConstructorAfter ​

injectConstructorAfter通用函数
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).

参数 ​

参数说明
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

返回值 ​

injection handle, usable by [rollbackUnsafe]

injectConstructorAfter ​

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

Injects a callback after constructor execution (Constructor overload).

injectAfter ​

injectAfter通用函数
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.

参数 ​

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

返回值 ​

Injection handle, usable by [rollbackUnsafe]

replace ​

replace通用函数
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.

参数 ​

参数说明
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

返回值 ​

Injection handle, usable by [rollbackUnsafe]

replace ​

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

Replaces entire target method body (Method overload).

参数 ​

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

返回值 ​

Injection handle, usable by [rollbackUnsafe]

redirect ​

redirect通用函数
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.

参数 ​

参数说明
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)

返回值 ​

Injection handle, usable by [rollbackUnsafe]

redirect ​

redirect通用函数
kotlin
fun redirect(sourceMethod: Method, targetMethod: Method, owner: String? = null): InjectionHandle

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

参数 ​

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

返回值 ​

Injection handle, usable by [rollbackUnsafe]

rollbackUnsafe ​

rollbackUnsafe通用函数
kotlin
fun rollbackUnsafe(handle: InjectionHandle): Boolean

Rolls back one unsafe injection by handle.

参数 ​

参数说明
handleThe injection handle to roll back

返回值 ​

true if the injection was found and removed, false otherwise

rollbackUnsafeByOwner ​

rollbackUnsafeByOwner通用函数
kotlin
fun rollbackUnsafeByOwner(owner: String)

Rolls back all unsafe injections by owner.

参数 ​

参数说明
ownerThe script owner whose injections should be removed