Arthur Eirich
Arthur Eirich

Reputation: 3638

Intellij IDEA: How to change all constructor calls after refactoring the constructor itself?

In my java project I have a constructor with a single object 'A' as a parameter and many unit tests instantiating an object via this constructor and passing an object 'A' to it. After refactoring the constructor it now gets an object of type 'B' holding a collection of objects of type 'A' as an instance variable. How can I bulk refactor all the constructor calls in my unit tests so all the calls now pass an object of type 'B' with a collection of objects of type 'A' instead of a single instance of 'A' using Intellij IDEA ultimate edition? I haven't found anything in this direction thatswhy I decided to aks here.

Upvotes: 0

Views: 333

Answers (1)

user3707125
user3707125

Reputation: 3474

Edit -> Find -> Replace Structurally...

Find:

new MyClass($parameter$);

Replacement:

new MyClass(java.util.Collections.singleton($parameter$));

In "Edit Variables" you can specify regex for $parameter$ type in case if constructor is overloaded. Most likely you would want to untick "Use static import if possible".

Nevertheless I suggest you to overload the constructor rather than doing this, if possible.

Upvotes: 1

Related Questions