周末没啥事,就看了看idea的文档,其中发现了一个感觉挺不错的功能,可以用来分析代码的影响范围,在这里分享记录一下
官方文档说明地址
https://www.jetbrains.com/help/idea/analyzing-data-flow.html
其中对于这个功能的描述为:
When working with large codebases, it is sometimes difficult to figure out how data is processed and how the workflows could be improved to make the code more performant and readable. To facilitate this, IntelliJ IDEA dataflow analysis enables you to trace all the possible data transformations without running the program. The information can be used to improve the design of the app and diagnose bugs before they manifest themselves.
自我翻译加理解一下大概就是: 可以用它来跟踪数据流。
其功能点的入口官网文档上也有相应的描述:
From the main menu select Code | Analyze Code | Data Flow to Here to analyze data upstream (producers) or Code | Analyze Code | Data Flow from Here to analyze data downstream (consumers).
也就是在主菜单的【Code】->【Analyze Code】下,有两个菜单分别是:【Data Flow to Here】和【Data Flow from Here】
其中的【Data Flow to Here】是用来查看上行数据的(producers)
其中的【Data Flow from Here】是用来查看下行数据的(consumers)
示例说明
看描述可能还不太明白,接下来举两个实例演示一下。 新建一个TestDaoBean和TestServiceImpl
public class TestDaoBean {
private String filed1;
private String filed2;
private String filed3;
public String getFiled1() {
return filed1;
}
public void setFiled1(String filed1) {
this.filed1 = filed1;
}
public String getFiled2() {
return filed2;
}
public void setFiled2(String filed2) {
this.filed2 = filed2;
}
public String getFiled3() {
return filed3;
}
public void setFiled3(String filed3) {
this.filed3 = filed3;
}
}
@Service
public class TestServiceImpl implements TestService {
private String queryResult(Integer flag) {
TestDaoBean testDaoBean = new TestDaoBean();
testDaoBean.setFiled1("123");
return flag + testDaoBean.getFiled1();
}
……
}
Data Flow to Here
选中TestDaoBean类中的filed1字段,查看它的【Data Flow to Here】,其效果如下: 从图上可以看出,展示出了在项目中具体有哪些代码给field1这个字段设置了值(修改)(producers)
Data Flow from Here
选中TestDaoBean类中的filed1字段,查看它的【Data Flow from Here】,其效果如下:
从图上可以看出,展示出了在项目中具体有哪些代码取用了filed1这个字段(读取)(consumers)
|