Skip to content

75 拦截器的进入与退出 ​

before 在函数体之前执行,after 在调用结束时执行。completion.succeeded() 指出被拦截的调用是否正常完成。

norm
import std.annotation.FunctionInterceptor
import std.annotation.SourceRetention

annotation Trace implements FunctionInterceptor, SourceRetention {
  Void before(FunctionContext context) { printLine("before") }
  Void after(FunctionContext context, FunctionCompletion completion) {
    printLine("after: ${completion.succeeded()}")
  }
}

@Trace()
String greet() {
  printLine("body")
  return "Norm"
}

main() { printLine(greet()) }

预期输出:

text
before
body
after: true
Norm

动手试试:让 greet 抛出异常,观察完成状态。

精确规则:参考。

Norm 0.25