简体   繁体   中英

Refactoring tool in Eclipse

My need is pretty simple: I want to change a method call objClass1.method1() by a call objClass2.method2() in my whole Eclipse project. Unfortunately, I can't find a plugin able to do this. Can you help?

Edit:

To be more accurate, objClass1 is part of a third party library, so I need to change the method calls. I can't start at the method definition. When I right-click on a method1 call, I have no "rename" option in my "Refactor" menu.

I don't want to change or rename my methods. I want to exchange one call by another in my whole project.

An example of what needs to be done:

Before refactoring:

Injector injector=Guice.createInjector(new IContactModule());

After refactoring:

Injector injector=IContactInjectorSingleton.getInjector();

And this needs to be done a several points in my project.

What you ask for is no refactoring. A refactoring is defined as "a change that alters the code while not changing the behavior of the code". In this sense renaming a class or renaming a method is a refactoring (you change the code but the program does the same as before). But what you suggest does NOT preserve the behavior of the code so there will never be a "refactoring" for this.

Of course one might be able to write a plugin that is able to perform the text changes you want in a more or less safe way. But this will only work in very specific circumstances (what if your new method needs an argument the old one dons't need? What if there are more than one method with the same name but different parameters? ...). So I don't believe such a plugin exists, nor it makes much sense to develop such a plugin.

Just right click on the class/method name and choose Refactor > Rename.

EDIT:

To be more accurate, objClass1 is part of a third party library, so I need to change the method calls. I can't start at the method definition. When I right-click on a method1 call, I have no "rename" option in my "Refactor" menu.

Hence I would suggest you to simply make a replacement:

Search menu > File, type the old name, choose the context of the search ("Enclosing project"), click on Replace and type the new name.

EDIT2:

From the example you added to the question I think that a manual replacement, using the tool I just suggested, it's the best way. It's a complex issue, as @Arne pointed out, so it's better to make it in a controlled way. Moreover I doubt it is such a frequent operation to require a plugin to be built.

You could use the eclipse refactoring by selecting the methods name. Right click for context menu or Alt-Shift-R, in the Rename-Dialog a preview dialog is available which shows all suggested changes in one place.

First, move the body of objClass1.method1() into objClass2.method2() , and have method1 simply call method2 . It may not be quite as "simple" as that, if for instance method1 uses fields of Class1 for instance, in which case you should probably include this as a parameter to the new method and perhaps use getters for the fields. If you can make the method static before doing this, it will be easier to avoid those kinds of problems. Anyway, make that transformation, so method1 is just calling method2 . Now use the Inline Method refactoring to make method1 go away. You're done.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM