Anyone an expert in using the `@Deprecated` annota...
# android
d
Anyone an expert in using the
@Deprecated
annotation with
ReplaceWith
? I want to replace a function which has some default values and when I run my current replace it always adds the default values so if I have this
Copy code
@Deprecated(
    message = "Use NewFunction instead",
    replaceWith = ReplaceWith(
        expression = "NewFunction(value1 = value1, value2 = value2, image = image?.let{ ImageVector.vectorResource(id = it) })",
    ),
)
DeprecatedFunction(
    value1 : String, 
    value2: Boolean = true, 
    @DrawableRes image: Int? = null
) { ... }

NewFunction(
    value1 : String, 
    value2: Boolean = true, 
    image: ImageVector? = null
) { ... }
and I run this on the following code
Copy code
DeprecatedFunction(
    value1 = "some value",
)
I end up with
Copy code
NewFunction(
    value1 = "some value",
    value2 = true,
    image = null?.let<Int, ImageVector?> { ImageVector.vectorResource(id = it) },
)
while I expect the replace to generate this
Copy code
NewFunction(
    value1 = "some value",
)
👀 1
e
I had a similar experience. I'm looking for a structural replacement or even trying an open rewrite tool
d
In the end I just replaced all and the opened each file and alt+enter and it was done. Was a bit tedious but faster the. Writing everything myself
👍🏼 1
o
The problem is in the
image
parameter which has different type and has some transformations, so replacer is not smart enough to understand that the default value is actually the same. You could create a copy of your deprecated function, but without this parameter and then apply the suggestion for it and for your original function separately:
Copy code
@Deprecated(
    "Use NewFunction instead",
    ReplaceWith("NewFunction(value1 = value1, value2 = value2) })"),
)
DeprecatedFunction(
    value1 : String, 
    value2: Boolean = true, 
) { ... }