简体   繁体   中英

How does Deprecated ReplaceWith work for Kotlin in intellij?

I wrote this code:

@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)
}

and for some reason when I press Alt+Enter and click "Replace with test2 ", the method test1 disappears and doesn't get replaced, what am I doing wrong?

Edit:

It does work for classes though:

@Deprecated("Old stuff", ReplaceWith("Test2"))
class Test1
class Test2

fun main(args: Array<String>) {
    val a = Test1()
}

You need to tell how it needs to be replaced exactly... While I do not know why it was just completely deleted, I will show you what I mean instead:

If you would use the following instead:

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

it will replace your test1(5) call to test2(5) correctly.

Note also that sometimes you may want to add the package name also if it isn't that clear which replacement should take place, eg:

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

You can also use static values in the replacement in case that is what you need.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM