Why is this an issue?
It’s common for methods to check the value of their parameters and throw an IllegalArgumentException
when one of them doesn’t match a
given condition. Those conditions are usually mentioned in the javadoc of the method.
This rule raises an issue when it detects that a method call has an argument which will trigger an IllegalArgumentException
.
Noncompliant code example
private void addFirst(Collection<String> collection, String element) {
if (collection instanceof List) {
((List<String>) collection).add(0, element);
} else {
throw new IllegalArgumentException();
}
}
public void caller() {
// Noncompliant: triggers an IllegalArgumentException because HashSet doesn't implement List
addFirst(new HashSet<>(), "hello");
}