【发布时间】:2013-02-13 06:53:11
【问题描述】:
我已经对 sonar cpd 如何检测重复块进行了很多分析。但我无法准确触发检测块或代码行所需的过程。是否有任何最小行数。
例如,如果我写如下,即使我重复超过 20 次,它也没有检测到任何代码重复。
System.out.println("this is good");
System.out.println("this is good");
System.out.println("this is good");
System.out.println("this is good");
System.out.println("this is good");
System.out.println("this is good");
System.out.println("this is good");
System.out.println("this is good");
System.out.println("this is good");
System.out.println("this is good");
后来我尝试给块重复
try
{
connection = null;
}
catch(Exception e){
e.printStackTrace();
}
try
{
connection = null;
}
catch(Exception e){
e.printStackTrace();
}
try{
connection = null;
}
catch(Exception e){
e.printStackTrace();
}
try{
connection = null;
}
catch(Exception e){
e.printStackTrace();
}
这里虽然有很多块,但它被认为是两个块。
请让我知道声纳 3.4.1 在此重复检测中遵循的确切过程
在这 http://docs.sonarsource.org/3.1/apidocs/src-html/org/sonar/plugins/cpd/SonarEngine.html
我发现一个恒定的块大小为 10。但我能够在我的观察中将其联系起来。
【问题讨论】:
-
这是一个所谓的“基于令牌的”克隆检测器的标志,它匹配令牌序列。这些往往具有较高的误报检测率; } catch ( Exception 在您的示例中被认为是此类检测器的一个小克隆。为了反驳这些检测器往往被配置为仅接受具有一些可能的附加限制的相当大的克隆,请参阅 Andras 的回答. 基于 AST 的克隆检测器仅匹配语言结构;它们永远不会将 } catch (Exception 视为克隆。因此,它们可以找到粒度小得多的克隆。