简体   繁体   English

Eclipse中的方法重构

[英]Method refactoring in Eclipse

I try to do the following refactoring steps in Eclipse IDE (JDT) and can not find the required refactoring and can not remember the name of all of the steps. 我尝试在Eclipse IDE(JDT)中执行以下重构步骤,但找不到所需的重构,并且无法记住所有步骤的名称。 I checked the refactoring at SourceMacking and do not find the correct one. 我在SourceMacking上检查了重构,但找不到正确的重构。

Let's take for example the following scenario: 让我们以以下情形为例:

class A {

    method(B b) {
      doSomethingWithA();
      b.doSomethingWithB();
    }

    [...]
}

class B {
    [...]
}


1) Make method static (missing name of the refactoring?): 1)使方法静态 (缺少重构名称?):

class A {

    static method(A a, B b) {
      a.doSomethingWithA();
      b.doSomethingWithB();
    }

    [...]
}

class B {
    [...]
}


2) Move method: 2) 移动方式:

class A {
    [...]
}

class B {

    static method(A a, B b) {
      a.doSomethingWithA();
      b.doSomethingWithB();
    }

    [...]
}


3) Convert to instance method : 3)转换为实例方法

class A {
    [...]
}

class B {

    method(A a) {
      a.doSomethingWithA();
      doSomethingWithB();
    }

    [...]
}


So anyone knowing something to do this step by step in Eclipse or do know the name of the refactoring is welcome. 因此,欢迎任何知道在Eclipse中逐步执行此操作的人或知道重构名称的人。 The goal is to have IDE support for every step. 目标是为每个步骤都提供IDE支持。

Unfortunately, Eclipse's refactoring functionality is not as complete as other IDEs (for example Jetbrains' IntelliJ ). 不幸的是,Eclipse的重构功能不如其他IDE(例如Jetbrains的IntelliJ )完整。 I'll include instructions on how to perform each of the refactorings you requested with both IntelliJ and Eclipse. 我将提供有关如何执行IntelliJ和Eclipse请求的每个重构的说明。

With IntelliJ 使用IntelliJ

  1. Make Method Static 使方法静态
  2. Move Instance Method 移动实例方法
  3. Convert to Instance Method 转换为实例方法

With Eclipse 使用Eclipse

  1. Make Method Static: Eclipse doesn't directly support it, but we can achieve this using two other refactorings. 将方法设为静态:Eclipse不直接支持它,但是我们可以使用另外两个重构来实现。

    1.1. 1.1。 Introduce Indirection 介绍间接

    Eclipse的引入间接重构

    Result 结果

     public static void method(A a, B b) { a.method(b); } public void method(B b){ doSomethingWithA(); b.doSomethingWithB(); } 

    1.2. 1.2。 Inline 排队

    Eclipse的内联重构

    Result 结果

     public static void method(A a, B b) { a.doSomethingWithA(); b.doSomethingWithB(); } 
  2. Move Static Members 移动静态成员

  3. Convert to Instance Method: Now, this is where it gets tricky. 转换为实例方法:现在,这变得棘手了。 If you want to go from step 1 to step 3, you could just use Eclipse's Move Method and it'll handle everything perfectly fine. 如果您想从第1步转到第3步,则可以使用Eclipse的Move方法 ,它将完美地处理所有事情。 However, there are no ways that I know of to go from step 2 to step 3 using Eclipse's automated refactorings. 但是,我没有使用Eclipse的自动重构从第2步到第3步的方法。

After having learned the refactoring is called 'Convert to Instance Method' I searched the bug database of Eclipse JDT and I found bad news: 在了解到重构被称为“转换为实例方法”之后,我搜索了Eclipse JDT的错误数据库,发现了一个坏消息:

Bug 10605 Bug 118032 Bug 338449 错误10605 错误118032 错误338449

So basically it is a Won't-Fix noone cares feature request and so it might be time that I also switch to IntelliJ. 因此,基本上,这是一个无需解决的功能,无人问津,因此也许我该改用IntelliJ了。 I have to contemplate about this... . 我必须考虑一下……。

Emond Papegaaij suggested in the discussion of Bug 118032 a work around: Emond Papegaaij在Bug 118032的讨论中建议了以下解决方法:

A simple workaround is to create the static method, call this static method from the method you want to become static and inline the method call. 一个简单的解决方法是创建静态方法,从要变为静态的方法中调用此静态方法,然后内联该方法调用。 This works for me in 4.3.1. 这在4.3.1中对我有效。

This is interesting but again would not be an automatic refactoring and defeat the purpose of refactoring in the first place. 这很有趣,但同样不会自动重构,并且首先会破坏重构的目的。 Adding someone's own code introduce the chance of failure and requires the rerun of the test-suite resulting in no chance of safely refactoring legacy code. 添加某人自己的代码会导致失败的可能性,并且需要重新运行测试套件,从而导致无法安全地重构遗留代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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