简体   繁体   English

声纳“源文件不应有任何重复的块”规则不区分不同的文字

[英]Sonar 'Source files should not have any duplicated blocks' rule doesn't distinguish different literals

I have a simple two-level hierarchy made up of an abstract superclass named AbstractTable, and three specific subclasses, Table1, Table2 and Table3.我有一个简单的两级层次结构,由一个名为 AbstractTable 的抽象超类和三个特定的子类 Table1、Table2 和 Table3 组成。

Each subclass provide one different implementation for three absract methods.每个子类为三种抽象方法提供一种不同的实现。
For example, Table1 class says:例如,表 1 class 说:

  @Override
  protected String getSequence() {
    return "seq_table1";
  }
  @Override
  protected String getIdColumn() {
    return "id_table1";
  }
  @Override
  protected String getTable() {
    return "table1";
  }

Table2 and Table3 have similar implementations, they response different literals. Table2 和 Table3 具有相似的实现,它们响应不同的文字。
However, Sonar does not seem to distinguish the different literals and he tells me that all the lines are duplicated.但是,Sonar 似乎没有区分不同的文字,他告诉我所有的行都是重复的。
Rule: Source files should not have any duplicated blocks.规则:源文件不应有任何重复的块。

Why?为什么? I think sonar should consider different literals as different lines.我认为声纳应该将不同的文字视为不同的行。

Is the current operation of the rule correct?规则的当前操作是否正确? Can anyone explain to me how to pass the rule in these situations?谁能向我解释如何在这些情况下通过规则?

Thanks for advance.感谢提前。

The answer in https://github.com/SonarSource/SonarJS/issues/717 is for SonarJS but I guess it also holds true for SonarJava. https://github.com/SonarSource/SonarJS/issues/717中的答案适用于 SonarJS,但我想它也适用于 SonarJava。

As for a way to avoid it: You could perhaps override the constructor for the child classes hand have something like this:至于避免它的方法:您也许可以覆盖子类的构造函数,手上有这样的东西:

public class Table {
    private String sequence;
    private String idColumn;
    private String table;

    public Table(String sequence, String idColumn, String table) {
        this.sequence = sequence;
        this.idColumn = idColumn;
        this.table = table;
    }

    //getters and setters if needed
}

public class Table1 {
    
    public Table1() {
        super("seq_table1", "id_table1", "table1");
    }
} 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在SonarQube中抑制“源文件不应该有任何重复块”的警告? - How is the warning 'Source files should not have any duplicated blocks' suppressed in SonarQube? 从Sonar Eclipse插件未检测到重复的块 - duplicated blocks are not detected from Sonar Eclipse Plugin 自定义Sonar Javascript插件规则,可检查多个源文件中的特定方法调用 - Custom Sonar Javascript plugin rule to check for a particular method call across multiple source files 为什么 Java 没有指定非转义字符串文字的方法? - Why doesn't Java have a way of specifying unescaped String literals? 声纳规则:Lambdas应该用方法引用替换 - Sonar rule: Lambdas should be replaced with method references 不同的对象,同名变量应该有两个不同的值,但不是? - Different Objects, same name variable should have two different values but doesn't? 如何遵守Sonar的规则“应使用SQL绑定机制” - How to comply with Sonar's rule “SQL binding mechanisms should be used” 声纳假阳性规则:不应取消引用空指针 - Sonar false-positive on rule: Null pointers should not be dereferenced Spring '"@RequestMapping" 方法的声纳规则应该是 "public"' 准确吗? - Is the Sonar rule for Spring '"@RequestMapping" methods should be "public"' accurate? 为什么Java SE 1.7中的数字文字中的下划线在Octal和Hexadecimal中有不同的规则? Octal文字是否违反了规则? - Why Underscores in Numeric Literals in Java SE 1.7 Rules are different in Octal and Hexadecimal? Isn't Octal literal violating the rule?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM