Is there a way to replace all usages of one annotation with another in IntelliJ IDEA?

375 views Asked by At

We are currently using the JCIP annotations for concurrency checking and wanted to try switching to the JSR 305 annotations for the same purpose. We will later do the same thing to switch from the JetBrains annotations to the JSR 305 equivalents, which will be a somewhat bigger task because they're being used more heavily.

I have tried a number of ways to do this.

If I do the find and replace using naive text replacement, the import statements end up out of order. In almost all cases, this breaks the code style because of import order. In some situations, IDEA appears to be unable to see that they're out of order and won't offer me to fix it, so I would have to manually go through all files, in which case I might as well do the whole job manually.

I tried Replace Structurally and found that whole feature just as unusable as always. It didn't even seem to be able to find the annotation.

I eventually tried removing the @ from what I was searching for and it found the annotations (it's a bit ironic that even though the point of this feature is to find things structurally, when you actually try to find a bit of structure, it suddenly won't find anything.) It then replaced them with a fully-qualified version of the new annotation even though I had told it to shorten the names. To make matters worse, it doesn't detect these names as unnecessarily fully qualified, so it isn't offering the automated fix for that, meaning that again, I can't automatically run it over all files. (It also shows the new annotation as a syntax error, which I can only presume is a bug.)

I also tried the Migrate feature, adding all the old and classnames in and telling it to migrate across. I found that this had the same effect as Replace Structurally - it didn't put in imports, leaving me to fix all of those manually.

Maybe there is another way to do it?

0

There are 0 answers