Вопрос

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 a refactor->extract method, calling the new method something like countModels.

Secondly, do a refactor->move on the new countModels() method, selecting the repository field as the destination.

This will leave you with a method on the ModelRepository called countModels rather than getModelCount. For completeness you could do a refactor->rename on this, but I prefer countModels anyway.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top