kotlin - 在 Kotlin IR 编译器中使用“this”关键字
问题描述
我正在为 Kotlin IR 编译器(版本 1.4.21)开发一个插件。该插件基于bnorm/debuglog (在此博客文章系列中有很好的描述)。
我想转换一个方法,以便它打印调用该函数的对象的类名,有点像这样:
原始方法:
@MyAnnotation
fun foo() {
// Do something
}
变换方法:
@MyAnnotation
fun foo() {
// Do something
println(this::class.java.simpleName)
}
我的问题
我不知道如何使它与继承一起工作。
我试图function.parentClassOrNull?.name
在我的 IR 编译器函数的覆盖版本中获取类名visitFunctionNew(...)
,但这会将“声明”类的名称(在源代码中明确声明该方法的类)硬编码到 print 语句中。
我还尝试找到并使用 Kotlinthis
关键字的 IR 等效项,但没有成功。
这是我想要实现的示例:
源代码:
open class Superclass {
@MyAnnotation
fun foo() {
// Do something
}
}
class Subclass : Superclass()
执行时:
Superclass().foo()
>>> Superclass
Subclass().foo()
>>> Subclass
谢谢你的帮助!
解决方案
推荐阅读
- javascript - 如何在 JavaScript 中获取 JSON 数据数组?
- github - 你如何建议对 Github 中的 repo 描述进行编辑
- javascript - 带有 HTML 和 PHP 的动态表
- jquery - 如何强制白色背景中的元素覆盖其他元素?
- c# - NotifyIcon 点击事件在表单停用之后触发
- python - 正则表达式从 Python 中的文件中提取多行行
- react-native - 使用 react-native Navigation 时带有 RnGestureHandler 的未定义对象
- python-3.x - 根据索引更新 pandas DataFrame
- matlab - 2x2系统的MATLAB集成
- python - 为什么在计算反转布尔系列的总和时需要括号或新变量?