Eclipse 可以将方法转换/重构为类吗?
这看起来应该是相当简单的,但我看不到任何明显的东西。我基本上想做的是指向一个方法并重构->提取类。这会将相关方法带到一个新类,并将该方法作为顶级公共 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为这种重构还不存在。
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.
I mention a workaround in this SO answer.
在 Eclipse 3.7.1 中,有一个选项可以将方法和字段移出类。为此:
现在,您的成员已提取。修复任何可见性问题(“源”>“生成 Getters 和 Setters”对此非常有用),然后就一切就绪了。
In Eclipse 3.7.1 there is an option to move methods and fields out of a class. To do so:
Your members are now extracted. Fix any visibility issues (Source > Generate Getters and Setters is very useful for this) and you are all set.
实际上,Extract Class 是最困难的重构之一。即使在移动单个方法及其依赖项的简单示例中,也可能会出现复杂情况:
这就是为什么我鼓励大家投票给 bug 312347得到修复。
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:
This is why I encourage everybody to vote for bug 312347 to get fixed.
您是否尝试过
Refactor
组的Move
功能?您可以创建一个helper类并将您想要的任何内容移到那里。Have you tried the
Move
feature of theRefactor
group ? You can create a helper class and move there anything you want.