【问题标题】:Sonar CPD detecting blocks duplications声纳 CPD 检测块重复
【发布时间】: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 视为克隆。因此,它们可以找到粒度小得多的克隆。

标签: java sonarqube cpd


【解决方案1】:

块是平衡大括号之间的代码行。恒定的块大小意味着在块中必须有 10 行代码才能匹配。所以要复制试试这个。

public void foo(){
//... 10 lines of code
}

private void bar(){
//.... the same 10 lines of code
}

【讨论】:

  • 我给出了两个函数,在里面我给出了两个函数中重复的 15 个打印语句。但在声纳分析中没有观察到任何重复
  • 两个函数体是一样的吗?
  • 是的,这两个函数都具有相同的主体
  • 对不起,在那种情况下,我不知道不计算明显的错误(例如你的代码没有被分析)。
  • 我只在某些情况下得到重复,但在这种情况下没有。而且我的代码分析成功
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-14
  • 2017-05-09
  • 2021-07-14
  • 2017-06-11
  • 2017-05-17
  • 2016-08-24
相关资源
最近更新 更多