[英]Eclipse refactoring: move method inside collaborator
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: 现在,我想通过使用一些自动Eclipse重构来移动
ModelRepository
的getModelCount
方法,以便我最终在控制器中使用它:
public class Controller {
private ModelRepository repository;
public int getModelCount() {
repository.getModelCount();
}
}
Is this possible in Eclipse Indigo? Eclipse Indigo有可能吗? If yes, how?
如果有,怎么样? Thanks!
谢谢!
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 a refactor->extract method
, calling the new method something like countModels
. 首先,突出显示
getModelCount()
方法的内容并执行refactor->extract method
,调用new方法类似于countModels
。
Secondly, do a refactor->move
on the new countModels()
method, selecting the repository
field as the destination. 其次,对新的
countModels()
方法执行refactor->move
,选择repository
字段作为目标。
This will leave you with a method on the ModelRepository
called countModels
rather than getModelCount
. 这将在
ModelRepository
上为您提供一个名为countModels
而不是getModelCount
。 For completeness you could do a refactor->rename
on this, but I prefer countModels
anyway. 为了完整性,你可以对此进行
refactor->rename
,但无论如何我更喜欢countModels
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.