[英]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.