Eclipse 重构:在协作者内部移动方法
我有以下场景:
public class Controller {
private ModelRepository repository;
public int getModelCount() {
int count = 0;
List<Model> models = repository.getModels();
for (Model model : models) {
if (model.somePredicate()) {
count++;
}
}
return count;
}
}
现在,我想通过使用一些自动 Eclipse 重构将 getModelCount
方法移到 ModelRepository
内,以便我最终在控制器中得到这个:
public class Controller {
private ModelRepository repository;
public int getModelCount() {
repository.getModelCount();
}
}
这在 Eclipse Indigo 中可能吗?如果是,怎么办?谢谢!
I have the following scenario:
public class Controller {
private ModelRepository repository;
public int getModelCount() {
int count = 0;
List<Model> models = repository.getModels();
for (Model model : models) {
if (model.somePredicate()) {
count++;
}
}
return count;
}
}
Now, I'd like to move the getModelCount
method inside ModelRepository
by using some automated Eclipse refactoring so that I end up with this in the controller:
public class Controller {
private ModelRepository repository;
public int getModelCount() {
repository.getModelCount();
}
}
Is this possible in Eclipse Indigo? If yes, how? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不认为存在单跳重构,但你可以分两步进行。
首先,突出显示
getModelCount()
方法的内容并执行refactor->extract method
,调用新方法(例如countModels
)。其次,对新的
countModels()
方法执行refactor->move
,选择repository
字段作为目标。这将在
ModelRepository
上留下一个名为countModels
而不是getModelCount
的方法。为了完整起见,您可以对此进行refactor->rename
,但无论如何我更喜欢countModels
。I don't think there is a single-hop refactor, but you can do it in two.
First, highlight the contents of the
getModelCount()
method and do arefactor->extract method
, calling the new method something likecountModels
.Secondly, do a
refactor->move
on the newcountModels()
method, selecting therepository
field as the destination.This will leave you with a method on the
ModelRepository
calledcountModels
rather thangetModelCount
. For completeness you could do arefactor->rename
on this, but I prefercountModels
anyway.