PMD DataflowAnomalyAnalysis之DU - Anomaly
使用PMD时,经常会出现下面异常:
DataflowAnomalyAnalysis: The dataflow analysis tracks local definitions, undefinitions and references to variables on different paths on the data flow. From those informations there can be found various problems. 1. UR - Anomaly: There is a reference to a variable that was not defined before. This is a bug and leads to an error. 2. DU - Anomaly: A recently defined variable is undefined. These anomalies may appear in normal source text. 3. DD - Anomaly: A recently defined variable is redefined. This is ominous but don't have to be a bug.
解决方案 数据流异常分析:数据流分析是跟踪本地的变量定义与否及在数据流中不同路径的变量引用。由此可以发现多种问题:1.UR-异常:指向一个之前没有定义的变量,这是bug且可导致错误2.DU-异常:一个刚刚定义的变量是未定义的。这些异常可能出现在普通的源代码文本中3.DD-异常:一个刚刚定义的变量重新定义。这是不好的但并非一定是个bug。
其中DU - Anomaly是一个刚刚定义的变量是未定义的。这句话不太好理解,举个例子就瞬间明白:
List<Map<String,Object>> lst = ...;
Map<String,Object> map = new HashMap<String,Object>
map = lst.get(0);
这个时候就会出现DU - Anomaly,解决方案为:
Map<String,Object> map;
map = lst.get(0);
这样就好了。其他异常随后补上,欢迎大家拍砖!