如何避免此 PMD 错误 - 字符串文字 "TEST" 在此文件中出现 X 次;
How to avoid this PMD error - The String literal "TEST" appears X times in this file;
如何避免我的 Java 代码中的 PMD 错误?
public enum testEnum {
TEST1(1L, "TEST", "random1");
TEST2(2L, "TEST", "random2");
TEST3(3L, "TEST", "random3");
TEST4(4L, "TEST", "random4");
TEST5(5L, "TEST", "random5");
TEST6(6L, "TEST", "random6");
TEST7(7L, "OTHER STRING", "random7");
private Long id;
private String type;
private String text;
private testEnum(Long id, String type, String text){
this.id = id;
this.type = type;
this.text = text;
}
}
当 运行 PMD 检查时会抛出这些错误:
The String literal "TEST" appears 6 times in this file; the first occurrence is on line 10
除了使用 @SuppressWarnings("PMD")
之外,还有什么方法可以避免它吗?
你应该private static final String = "TEST"
用它来做一个。重复相同的字符串是不好的做法。
避免这种情况的最佳方法是使用更有用的字符串而不是 "TEST"。
最好使用常量而不是在多个地方重复相同的字符串,这对堆和维护代码来说更好。您只需在一个地方进行修改,而不必查看使用了 "TEST" 更改的位置。
您可以使用 :
私人静态最终字符串测试 = "TEST";
有时有充分的理由禁用这个或那个 PMD 警告。禁用特定警告的方法是将显式规则添加到抑制中。这是:
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
这也适用于所有其他规则。
如何避免我的 Java 代码中的 PMD 错误?
public enum testEnum {
TEST1(1L, "TEST", "random1");
TEST2(2L, "TEST", "random2");
TEST3(3L, "TEST", "random3");
TEST4(4L, "TEST", "random4");
TEST5(5L, "TEST", "random5");
TEST6(6L, "TEST", "random6");
TEST7(7L, "OTHER STRING", "random7");
private Long id;
private String type;
private String text;
private testEnum(Long id, String type, String text){
this.id = id;
this.type = type;
this.text = text;
}
}
当 运行 PMD 检查时会抛出这些错误:
The String literal "TEST" appears 6 times in this file; the first occurrence is on line 10
除了使用 @SuppressWarnings("PMD")
之外,还有什么方法可以避免它吗?
你应该private static final String = "TEST"
用它来做一个。重复相同的字符串是不好的做法。
避免这种情况的最佳方法是使用更有用的字符串而不是 "TEST"。
最好使用常量而不是在多个地方重复相同的字符串,这对堆和维护代码来说更好。您只需在一个地方进行修改,而不必查看使用了 "TEST" 更改的位置。 您可以使用 : 私人静态最终字符串测试 = "TEST";
有时有充分的理由禁用这个或那个 PMD 警告。禁用特定警告的方法是将显式规则添加到抑制中。这是:
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
这也适用于所有其他规则。