如何在避免 Sonar 规则 squid:S2175 的同时检查 List<Class> 是否包含 Class
How to check if a List<Class> contains a Class while avoiding Sonar rule squid:S2175
我有一个允许的自定义注释列表,我正在尝试通过调用列表中的 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) 同时,压制它。有理有据的压制也不是坏事
我有一个允许的自定义注释列表,我正在尝试通过调用列表中的 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) 同时,压制它。有理有据的压制也不是坏事