简体   繁体   English

Eclipse重构:在协作者中移动方法

[英]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重构来移动ModelRepositorygetModelCount方法,以便我最终在控制器中使用它:

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.

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