IntelliJ可以将属性(获取/设置)重构为字段吗? [英] Can IntelliJ refactor properties (get/setters) to fields?
问题描述
比方说,我有一堆实现了Javabean风格的类,我想摆脱getters/setter方法,而只拥有公共字段.有什么方法可以在不中断编译的情况下使用IntelliJ 14有效地重构它?
Let's say I have a bunch of classes implemented javabean-style and I want to get rid of getters/setters and just have public fields instead. Any way to effectively refactor that using IntelliJ 14 while not breaking compilation?
示例-之前:
class Baratheon {
private String stannis;
public String getStannis() {
return stannis;
}
public void setStannis(String stannis) {
this.stannis = stannis;
}
}
class Lannister {
public Lannister() {
Baratheon b = new Baratheon();
b.setStannis("dead");
}
}
-之后:
class Baratheon {
public String stannis;
}
class Lannister {
public Lannister() {
Baratheon b = new Baratheon();
b.stannis = "dead";
}
}
推荐答案
重构中显而易见的步骤是:
The obvious steps involved in your refactoring are:
-
公开字段,保留访问者.
make field public, retain accessors.
将客户端代码更改为直接访问字段,而不是通过访问器.
change client code to access field directly rather than via accessors.
删除访问器.
您可以利用intellij中的"inline ..."重构(Ctrl + Shift + n)来帮助执行步骤2.
You could leverage the 'inline...' refactoring in intellij (Ctrl + Shift + n) to help with step 2.
我不认为intellij中只有一个重构工具可以完成全部工作,请记住,只有整个受影响的代码库都在一个项目中,这才是真正可能的.您可以考虑编写一个插件,做这项工作.
I don't think there's a single refactoring tool in intellij which does the whole job, bearing in mind that it's only really possible if the entire affected codebase is in a single project. You could look into writing a plugin which does the job.
这篇关于IntelliJ可以将属性(获取/设置)重构为字段吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!