类型迁移
类型迁移重构允许您自动更改成员类型(例如从 integer 到 string),以及整个项目中数据流依赖的类型条目(例如方法返回类型、局部变量、参数等)。
它还允许您在数组和集合之间自动转换变量或方法返回类型。 如果发现任何冲突,IntelliJ IDEA 将显示相应的消息。
在编辑器中,突出显示或将文本光标放置在您要重构的类型上。
请按 Ctrl+Shift+F6 或在主菜单中,进入 。
在打开的对话框中,指定新类型和要查找用法的范围。
示例
f: int -> String
之前 | 之后 |
|---|---|
int f;
void bar(int i) {}
void foo() {
bar(f);
}
|
String f;
void bar(String i) {}
void foo() {
bar(f);
}
|
I<String> -> I<Integer>
之前 | 之后 |
|---|---|
interface I<T> {
T foo(T t);
}
class A implements I<String> {
String myString;
public String foo(final String s) {
if (s == null) {
return
myString;
}
return s;
}
}
|
interface I<T> {
T foo(T t);
}
class A implements I<Integer> {
Integer myString;
public Integer foo(final Integer s) {
if (s == null) {
return
myString;
}
return s;
}
}
|
myResult: ArrayList<String> -> String[]
之前 | 之后 |
|---|---|
public class ResultContainer {
private ArrayList<String> myResult;
public String[] getResult() {
return myResult.toArray(new String[myResult.size()]);
}
}
|
public class ResultContainer {
private String[] myResult;
public String[] getResult() {
return myResult;
}
}
|
类型迁移对话框
当您调用 Type Migration重构时,将出现此对话框。
条目 | 描述 |
|---|---|
迁移类型 | 使用此列表来指定新类型。 |
选择范围 | 使用此列表指定迁移范围。 如果需要,请点击 浏览 |
重构 | 点击此按钮以在指定范围内启动针对所有用法的重构。 如果有任何冲突,IntelliJ IDEA 会通知您。 |
预览 | 点击此按钮以浏览需更改的项目,将其排除或包含在重构中,并查看检测到的冲突。 |
最后修改日期: 2025年 4月 24日