How to check if a List<Class> contains a Class while avoiding Sonar rule squid:S2175

Tecnogirl :

I have a list of allowed custom annotations and I'm trying to check whether a specific annotation is allowed by calling the contains method on the list. This works but Sonar complains about rule squid:S2175. It says:

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.

How can I fix this?

Here's a minimal example:

private boolean testClassContains(){
        final List<Class<? extends Annotation>> annotations = Arrays.asList(MyAnnotation.class,YourAnnotation.class);

        return annotations.contains(YourAnnotation.class);
}
Alexander Pavlov :

1) First ensure you are using fresh versions of SOnarQUbe/SonarJava plugin

2) SonarQube is software written by people, so it could have errors. For example, similar case https://groups.google.com/forum/#!topic/sonarqube/qyOOOZORBNs I suggest you file issue in their Jira.

3) Meanwhile, just suppress it. Suppressing is not evil if you have reason to do so

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=69793&siteId=1