已弃用的 ReplaceWith 如何在 Intellij 中为 Kotlin 工作?



我写了这段代码:

@Deprecated("Old stuff", ReplaceWith("test2"))
fun test1(i: Int) {
println("old Int = $i")
}
fun test2(i: Int) {
println("new Int = $i")
}
fun main(args: Array<String>) {
test1(3)
}

由于某种原因,当我按 Alt+Enter 并单击"替换为test2"时,该方法test1消失并且不会被替换,我做错了什么?

编辑:

不过,它确实适用于类:

@Deprecated("Old stuff", ReplaceWith("Test2"))
class Test1
class Test2
fun main(args: Array<String>) {
val a = Test1()
}

你需要确切地告诉它需要如何更换......虽然我不知道为什么它被完全删除,但我会告诉你我的意思:

如果您改用以下内容:

@Deprecated("Old stuff", ReplaceWith("test2(i)"))

它将正确替换您的test1(5)呼叫test2(5)

另请注意,有时如果不清楚应该进行哪种替换,您可能还需要添加包名称,例如:

@Deprecated("Old stuff", ReplaceWith("org.example.test2(i)"))
// or just use:
@Deprecated("Old stuff", ReplaceWith("test2(i)", /* here come the imports */ "org.example.test2"))

如果需要,您还可以在替换中使用静态值。

最新更新