IntelliJ重构使用LoD [英] IntelliJ Refactor to use LoD

查看:182
本文介绍了IntelliJ重构使用LoD的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一些课Foo

class Foo {
    protected String x = "x";

    public String getX() {
        return x;
    }
}

我有一个使用Foo并违反LoD的程序

I have a program that uses Foo and violates LoD

class Bar {
    protected Foo foo;

    public Bar() {
        this.foo = new Foo();
    }

    public Foo getFoo() {
        return foo;
    }
}

public static void main(String [] args) {
    Bar bar = new Bar();
    String x = bar.getFoo().getX(); 
}

重构使用LoD如下所示:

Refactoring to use LoD looks like this:

class Bar {
    protected Foo foo;

    public Bar() {
        this.foo = new Foo()
    }

    public String getFooX {
        return foo.getX();
    }
}

public static void main(String [] args) {
    Bar bar = new Bar();
    String x = bar.getFooX();
}

IntelliJ-IDEA有很多重构方法(例如提取到方法,提取变量,内联)。

IntelliJ-IDEA has a lot of refactoring methods (e.g. extract to method, extract to variable, inline).

IntelliJ-IDEA中是否有一个重构代码的方法,如 bar.getFoo()。getX()看起来像 bar.getFooX()

Is there a method in IntelliJ-IDEA that will refactor code like bar.getFoo().getX() to look like bar.getFooX()?

推荐答案

假设你的示例是Java代码,您可以执行以下操作:

Assuming that your example is Java code, you could do the following:


  1. 上提取方法bar.getFoo()。getX( )(创建 getFooX()

  2. 移动创建的方法 getFooX( ) Bar 如有必要

  3. 调用查找和替换代码重复项 getFooX()

  4. getFooX()<上调用转换为实例方法 / code>

  5. 可选择结构替换 $ a $ .getFoo()。getX() $ a $ .getFooX()如果您忘记了第3步; - )

  6. 内联所有的调用getFoo()(应仅在 getFooX()

  1. Extract method on bar.getFoo().getX() (creating getFooX())
  2. Move the created method getFooX() to Bar if necessary
  3. Invoke Find and Replace Code Duplicates on getFooX()
  4. Invoke Convert To Instance Method on getFooX()
  5. Optionally Structural Replace $a$.getFoo().getX() with $a$.getFooX() if you forgot step 3;-)
  6. Inline all invocations of getFoo() (should be only in getFooX())

这篇关于IntelliJ重构使用LoD的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆