简体   繁体   中英

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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM