How to replace all instances of multiple classes?

We have a bunch of classes containing their own static inner class called "Id", which is almost empty:

    public class Monkey {
        public static class Id extends UUID {
            public Id() {
            }

            public Id(UUID id) {
                assign(id);
            }
        }

        Monkey.Id id;
    }


I would like to replace all instances of Monkey.Id in my codebase with UUID<Monkey>

I can do this with structural search and replace, by doing:

Search:

com.example.zoo.animals.Monkey.Id

Replace

com.example.zoo.UUID<com.example.zoo.animals.Monkey>


But I want to do this replacement for every single class in the com.example.zoo.animals package. The problem is that we have a lot of these classes.

From what I can see IntelliJ's macro support is too simple to be able to repeat this for every class in the package (or "all animals in the zoo")

Is there an easy way to automate this?

4 comments

Hello Jeremy,

What about setting up an appropriate scope, then restricting the structural
search & replace to that scope?

But I want to do this replacement for every single class in the
com.example.zoo.animals package. The problem is that we have a lot
of these classes.



0

Thanks for the quick reply Chris,

I do actually want the replacement to happen throughout the entire codebase.

My problem is that I also want to do a similar replacement for Elephant.Id and Tiger.Id --- I want the replacement for every class in that package (they all have their own static inner class called "Id").

I'm hoping there's some way I can do this automatically -- or at least easier than manually doing a hundred structural search and replaces (one per class) - which would be a lot of typing,

Edit: The ability to write IntelliJ Macros in a scripting language would be perfect, but as far as I can see I'd have to write a whole plugin.

0

An alternative approach to solve it is if I could use a backreference, much like regular search and replace. A structural search and replace version of this:

Search

com\.example\.zoo\.animals\.(\w+)\.Id

Replace
com.example.zoo.UUID<com.example.zoo.animals.$1>
0

Hello Jeremy,

Ahh I see now. Sorry, I'd misunderstood your question. No immediate solution
springs to mind, other than to put all the classnames into a text file, then
use a macro to navigate to the text file, cut the first line (a classname),
paste it into your structural replace dialog, run the replace. Not sure how
well or if that'll work in practice though.

Chris

Thanks for the quick reply Chris,

I do actually want the replacement to happen throughout the entire
codebase.

My problem is that I also want to do a similar replacement for
Elephant.Id and Tiger.Id --- I want the replacement for every class in
that package (they all have their own static inner class called "Id").

I'm hoping there's some way I can do this automatically -- or at least
easier than manually doing a hundred structural search and replaces
(one per class) - which would be a lot of typing,



0

Please sign in to leave a comment.