I want to automatically find all occurrences in my kotlin application where i use deprecated code (mostly methods which are annotated as deprecated)
I'm talking not about kotlin code which is deprecated in the kotlin spec, i mean kotlin code which calls a deprecated function/method.
The source where the deprecated function is located could be kotlin- or java- libraries.
I found really great tooling for finding similar issues in java code via intellij idea, like:
But i don't find a way for any similar search option for kotlin.
I tried to build a "deprecated api usage structural search"-template myself, but base functionalities to archive this seems not be supported for kotlin.
The only none manual function which works is the local code analyse of the file i'm currently in (this little hind in the upper right corner where you can see number of issues (and can click on them to have them in the a list))
This works but if i make a code analyse for a hole folder the deprecated usages are not there.
I need a solution for scanning the hole code base and not manually clicking from file to file.
Edit: please note that there is now already a ticket created for jetbrains: https://youtrack.jetbrains.com/issue/IDEA-311206
Because it seems like there is no solution or workaround currently in place.
I now got an answer and a working solution from my (second) ticket at jetbrains for this topic (one ticket for inspect code analyse and one for structural search): https://youtrack.jetbrains.com/issue/KTIJ-24477
It turns out that the structural search template Reference
attribute "Deprecated Method" i use on the $MethodCall$
variable (the same way as it's for java in the existing template "Method calls to deprecate methods"), is not supported for kotlin templates.
In fact this Reference
attribute works differently then i thought but it's also more generic and powerful. You could select any structural search template there as a "preprocessing" search which limits the input on your current template (at least that's for now my conceptual model of it)
This fit's to the solution i got from jetbrains, which means that i'm doing the following:
First make a new template for finding kotlin methods which flagged as deprecated:
@Deprecated($Parameters$)
fun $MethodCall$($Parameter$)
Then saving this with a name like deprecated kotlin methods
.
Then create a new Template for finding code which is calling such deprecated methods. The template looks like:
$MethodCall$.($Parameters$)
Where parameters are set to range between 0 - *
.
And now set the Reference
attribute to the custom template which was wrote beforehand (in this example it's named deprecated kotlin methods
.
Now these calls to deprecated kotlin methods are found!
If you want to find deprecated java code which is called by your kotlin code base (e.g. which are inside of libs) then you could do the same apporoach. But this time you create a custom template for the Reference
attribute which is based on Java and uses the Java Deprecated annotation. Combining this inside a kotlin template seems to works.