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!
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->moveon the newcountModels()method, selecting therepositoryfield as the destination.This will leave you with a method on the
ModelRepositorycalledcountModelsrather thangetModelCount. For completeness you could do arefactor->renameon this, but I prefercountModelsanyway.