Eclipse 可以将方法转换/重构为类吗?

发布于 2024-09-13 00:38:22 字数 233 浏览 6 评论 0原文

这看起来应该是相当简单的,但我看不到任何明显的东西。我基本上想做的是指向一个方法并重构->提取类。这会将相关方法带到一个新类,并将该方法作为顶级公共 API。重构还会将所有必需的方法和变量拖到新类中,如果旧类中没有其他内容正在使用它们,则将它们从旧类中删除。

这是我在重构遗留代码时经常遇到的重复任务。不管怎样,我目前正在使用 Eclipse 3.0.2,但如果它在更新版本的 Eclipse 中可用,我仍然会对答案感兴趣。谢谢!

This seems like it should be fairly straight-forward, but I can't see anything obvious. What I basically want to do it to point at a method and refactor->extract class. This would take the method in question to a new class with that method as top level public API. The refactoring would also drag any required methods and variables along with it to the new class, deleting them from the old class if nothing else in the old class is using it.

This is a repetitive task I often encounter when refactoring legacy code. Anyway, I'm currently using Eclipse 3.0.2, but would still be interested in the answer if its available in a more recent version of eclipse. Thanks!

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

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

发布评论

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

评论(4

如若梦似彩虹 2024-09-20 00:38:22

我认为这种重构还不存在。

Bug 225716 已记录该问题某种功能(自 2008 年初以来)。
Bug 312347 也不错实施这样的重构。

“创建一个新类,并将相关字段和方法从旧类移动到新类中。”

我在此 SO 答案中提到了一种解决方法

I don't think this kind of refactoring exists yet.

Bug 225716 has been log for that kind of feature (since early 2008).
Bug 312347 would also be a good implementation of such a refactoring.

"Create a new class and move the relevant fields and methods from the old class into the new class."

I mention a workaround in this SO answer.

清晨说晚安 2024-09-20 00:38:22

在 Eclipse 3.7.1 中,有一个选项可以将方法和字段移出类。为此:

  1. 确保目标类存在(空类就可以,只要它存在于项目中即可)。
  2. 在源类中,选择要删除的方法(大纲视图非常适合此操作),右键单击所选内容,然后选择“移动”
  3. 在下拉列表中选择目标类/浏览

现在,您的成员已提取。修复任何可见性问题(“源”>“生成 Getters 和 Setters”对此非常有用),然后就一切就绪了。

In Eclipse 3.7.1 there is an option to move methods and fields out of a class. To do so:

  1. Make sure the destination class exists (empty class is fine, just as long as it exists in the project).
  2. In the source class, select the methods that you want to remove (the outline view works great for this), right click on the selection, and choose Move
  3. Select the destination class in the drop down/Browse

Your members are now extracted. Fix any visibility issues (Source > Generate Getters and Setters is very useful for this) and you are all set.

|煩躁 2024-09-20 00:38:22

这看起来应该是公平的
直接...

实际上,Extract Class 是最困难的重构之一。即使在移动单个方法及其依赖项的简单示例中,也可能会出现复杂情况:

  1. 如果移动的方法可能在您不知道的代码中使用,则您需要在原始类中拥有一个代理方法,该代理方法将委托给(调用)移动的方法。 (如果您的应用程序是独立的,或者如果您知道被移动方法的所有客户端,那么重构代码可能会更新调用代码。)
  2. 如果被移动方法是接口的一部分或者被移动方法是继承的,那么您还需要有一个“代理方法”。
  3. 您的方法可能会调用其他方法调用的私有方法/字段。您需要为被调用的成员选择一个类(可能是最常使用它的类)。您需要将访问权限从“私有”更改为更通用的权限。
  4. 根据原始类和提取的类需要了解彼此的程度,其中一个或两个可能需要初始化指向另一个的字段。
  5. 等等。

这就是为什么我鼓励大家投票给 bug 312347得到修复。

This seems like it should be fairly
straight-forward...

Actually, Extract Class is one of the more difficult refactorings. Even in your simple example of moving a single method and its dependencies, there are possible complications:

  1. If the moved method might be used in code you don't know about, you need to have a proxy method in the original class that will delegate to (call) the moved method. (If your application is self-contained or if you know all the clients of the moved method, then the refactoring code could update the calling code.)
  2. If the moved method is part of an interface or if the moved method is inherited, then you will also need to have a "proxy method".
  3. Your method may call a private method/field that some other method calls. You need to choose a class for the called member (maybe in the class that uses it the most). You will need to change access from "private" to something more general.
  4. Depending on how much the original class and the extracted class need to know about each other, one or both may need to have fields initialized that point to the other.
  5. Etc.

This is why I encourage everybody to vote for bug 312347 to get fixed.

听不够的曲调 2024-09-20 00:38:22

您是否尝试过Refactor组的Move功能?您可以创建一个helper类并将您想要的任何内容移到那里。

Have you tried the Move feature of the Refactor group ? You can create a helper class and move there anything you want.

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