Eclipse 中的重构工具

发布于 2024-10-29 23:30:48 字数 563 浏览 2 评论 0原文

我的需求非常简单:我想通过在整个 Eclipse 项目中调用 objClass2.method2() 来更改方法调用 objClass1.method1() 。不幸的是,我找不到能够执行此操作的插件。你能帮忙吗?

编辑:

更准确地说,objClass1是第三方库的一部分,因此我需要更改方法调用。我无法从方法定义开始。当我右键单击 method1 调用时,“重构”菜单中没有“重命名”选项。

我不想改变或重命名我的方法。我想在整个项目中与另一个呼叫进行交换。

需要做的事情的一个例子:

重构之前:

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

重构之后:

Injector injector=IContactInjectorSingleton.getInjector();

这需要在我的项目中完成几个点。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

太阳公公是暖光 2024-11-05 23:30:48

您要求的不是重构。重构被定义为“改变代码但不改变代码行为的改变”。从这个意义上说,重命名类或重命名方法是一种重构(您更改了代码,但程序与以前相同)。但是您的建议不会保留代码的行为,因此永远不会对此进行“重构”。

当然,人们也许能够编写一个插件,能够以或多或少安全的方式执行您想要的文本更改。但这仅适用于非常特定的情况(如果您的新方法需要旧方法不需要的参数怎么办?如果有多个具有相同名称但不同参数的方法怎么办?...)。所以我不相信这样的插件存在,开发这样的插件也没有多大意义。

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.

原来分手还会想你 2024-11-05 23:30:48

只需右键单击类/方法名称并选择 Refactor >重命名。

编辑:

更准确地说,objClass1是第三方库的一部分,因此我需要更改方法调用。我无法从方法定义开始。当我右键单击 method1 调用时,“重构”菜单中没有“重命名”选项。

因此我建议您简单地进行替换:

搜索菜单>文件,输入旧名称,选择搜索上下文(“封闭项目”),单击“替换”并输入新名称。

编辑2:

从您添加到问题的示例中,我认为手动替换,使用我刚刚建议的工具,这是最好的方法。正如@Arne 指出的那样,这是一个复杂的问题,因此最好以受控的方式解决它。此外,我怀疑需要构建插件是一个如此频繁的操作。

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.

看海 2024-11-05 23:30:48

您可以通过选择方法名称来使用 Eclipse 重构。右键单击上下文菜单或 Alt-Shift-R,在重命名对话框中可以使用预览对话框,该对话框在一处显示所有建议的更改。

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.

我很坚强 2024-11-05 23:30:48

首先,将 objClass1.method1() 的主体移至 objClass2.method2() 中,并让 method1 只需调用 method2代码>.它可能并不那么“简单”,例如,如果 method1 使用 Class1 的字段,在这种情况下,您可能应该包含 this 作为新方法的参数,并且可能对字段使用 getter。如果您可以在执行此操作之前将方法设置为静态,则可以更轻松地避免此类问题。无论如何,进行该转换,因此 method1 只是调用 method2。现在使用内联方法重构来使 method1 消失。你完成了。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文