简体   繁体   English

Eclipse中的重构工具

[英]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. 我的需求非常简单:我想在整个Eclipse项目中通过调用objClass2.method2()来更改方法调用objClass1.method1() 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. 为了更准确, objClass1是第三方库的一部分,所以我需要更改方法调用。 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. 当我右键单击method1调用时,我的“重构”菜单中没有“重命名”选项。

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. 为了更准确,objClass1是第三方库的一部分,所以我需要更改方法调用。 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. 当我右键单击method1调用时,我的“重构”菜单中没有“重命名”选项。

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: 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. 这是一个复杂的问题,正如@Arne指出的那样,所以最好以受控方式制作它。 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. 您可以通过选择方法名称来使用eclipse重构。 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. 右键单击上下文菜单或Alt-Shift-R,在Rename-Dialog中,可以使用预览对话框,在一个位置显示所有建议的更改。

First, move the body of objClass1.method1() into objClass2.method2() , and have method1 simply call method2 . 首先,将objClass1.method1()的主体移动到objClass2.method2() ,并让method1简单地调用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. 它可能不像那样“简单”,例如,例如, method1使用Class1字段,在这种情况下,您可能应该将this作为新方法的参数包含在内,并且可能使用字段的getter。 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 . 无论如何,进行转换,因此method1只是调用method2 Now use the Inline Method refactoring to make method1 go away. 现在使用内联方法重构,使method1走开。 You're done. 你完成了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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