我有一个允许的自定义注释列表,我正在尝试通过调用列表中的 contains
方法来检查是否允许特定注释。
这有效,但 Sonar 提示规则 squid:S2175
。它说:
A "List<Class>" cannot contain a "Class"
A couple Collection methods can be called with arguments of an incorrect type, but doing so is pointless and likely the result of using the wrong argument. This rule will raise an issue when the type of the argument to List.contains or List.remove is unrelated to the type used for the list declaration.
我该如何解决这个问题?
这是一个最小的例子:
private boolean testClassContains(){
final List<Class<? extends Annotation>> annotations = Arrays.asList(MyAnnotation.class,YourAnnotation.class);
return annotations.contains(YourAnnotation.class);
}
最佳答案
1) 首先确保您使用的是最新版本的 SONarQUbe/SonarJava 插件
2) SonarQube 是人写的软件,所以可能会有错误。比如类似案例https://groups.google.com/forum/#!topic/sonarqube/qyOOOZORBNs 我建议你在他们的 Jira 中提交问题。
3) 同时,压制它。有理有据,压抑不是恶
关于java - 如何在避免 Sonar 规则鱿鱼 :S2175 的同时检查 List<Class> 是否包含类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55812657/