此問題是後續for this。IntelliJ中的簡單自定義重構
假設我有一些Foo類。
class Foo {
protected String x = "x";
public String getX() {
return x;
}
}
我有一個使用Foo和違反毀滅之王(Law of Demeter)的程序。
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。
- ⌥⌘米
bar.getFoo().getX()
- >getFooX(bar)
(提取物的方法,還查找和替換出現) - F6
getFooX(bar)
- >bar.getFooX()
(移至實例方法,還查找和替換髮生)
該程序使用Bar
不再違反LoD。
class Bar {
protected Foo foo;
public Bar() {
this.foo = new Foo();
}
public Foo getFoo() {
return foo;
}
public String getFooX() {
return foo.getX();
}
}
public static void main(String [] args) {
Bar bar = new Bar();
String x = bar.getFooX();
}
我想知道是否有一種方法可以使的IntelliJ一個自定義的重構方法,將鞏固這兩個步驟合爲一體。
編輯 我收到JetBrains的回覆,其中包含一個指向預先存在的功能請求的鏈接。如果您覺得這個有用,請投票。
您好邁克爾,
看來我們必須在YouTrack類似的要求: https://youtrack.jetbrains.com/issue/IDEA-122400。隨意爲其投票 並發表評論。
最好的問候,雅羅斯拉夫Bedrov JetBrains公司
編輯 至少有一種方式來檢查德米特的法律問題。 screenshot http://i59.tinypic.com/2s80ft0.png
Here is a gist其中包含檢查配置文件,只會查找LoD違規。你可以import it into IntelliJ。
我想知道是否可以用當前的IDEA版本(v14)在一個步驟中完成,我沒有找到任何合適的單一命令。如果此功能真的缺失,提交功能請求可能是一個好主意。 – erkfel
@erkfel我目前正在審查。鏈接看起來並不活躍,但它應該在這裏:https://intellij-support.jetbrains.com/hc/en-us/requests/66429 – michaelsnowden
@erkfel查看我的編輯,並簽出https:// youtrack .jetbrains.com /問題/ IDEA-122400 – michaelsnowden