Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to replace class by another one in Intellij Idea?

Guys, here is my problem: I have some class which is used in many places in my project. And I must replace this class with another from jar provided. Is there any ways to refactor this? I suppose this is a simple problem, but I dont know how to solve it.

Its not about replacing source code - what I want is to replace all class usages by class from my library and be able to completely remove my own class. Imagine I have created my own StringUtils and have found out that there is a apache.common StringUtils library, and now I want to use it everywhere in my code. And the signatures of class methods are not a problem: they coincide.

?

like image 799
KutaBeach Avatar asked Oct 28 '11 09:10

KutaBeach


People also ask

How to rename a class in IntelliJ IDEA?

Rename Dialog for a Class or an Interface. Shift+F6. Use this dialog to rename a class or an interface. In addition to renaming the class or the interface itself, IntelliJ IDEA can also look for the usages of the class or the interface name. If found, the changes you are making to the class or the interface name can also be applied to these usages.

How to find the name of a string in IntelliJ IDEA?

This is more likely to "find and replace" operation, to find something like Name:String = "***"; and replace it with Name:String = ""; in all cases. How can i do this with Intellij IDEA? if it is in one file : ctrl + r check the tick box Regex and look for Name:String = " [^"]*" : this should match what you want.

How does IntelliJ IDEA work?

IntelliJ IDEA lists the search strings and the files that contain them. If the search string is found several times on the same line of code, IntelliJ IDEA merges the results in one line.

How do I search multiple lines of code in IntelliJ?

IntelliJ IDEA lists the search strings and the files that contain them. If the search string is found several times on the same line of code, IntelliJ IDEA merges the results in one line. To do a multi-line search, click the icon to enter a new line, and press Ctrl+Alt+Down / Ctrl+Alt+Up to browse through occurrences.


2 Answers

There is this "migrate" function. Right click in whatever class -> Refactor -> Migrate.

It's a bit annoying that you have to create a new migrate set and you can't run it from scratch. But it does exactly what you need.

You pick class or package to migrate and tell it to which class or package it should change. Press run and all usages are rewritten. Then you're free to delete the old class because there will be no usages.

EDIT: In the newer versions it isn't in the context menu anymore. Just go to the menu on top - Refactor - Migrate (or simply pres shift twice and type migrate).

Cheers!

like image 178
Frank Avatar answered Oct 06 '22 17:10

Frank


If you are using static methods (like your StringUtils example suggests), delegate to the new class in your previous implementation like

public static String myOldMethod(String argument) {
    return MyNewClass.myNewMethod(argument);
}

then select Refactor->Inline and select "All invocations and remove the method" in the option dialog. In this way you can handle method name changes and argument order changes are well.

like image 21
Ingo Kegel Avatar answered Oct 06 '22 18:10

Ingo Kegel