The use of varargs parameters in the factories for aggregate matchers lead to a
compiler warning when using non reifiable types. For example:
import static java.util.Arrays.asList;
import static org.hamcrest.Matchers.contains;
...
contains(asList(new Object()), asList(new Object()));
In some cases, I think the methods can be annotated with @SafeVarargs,
Matchers.contains being a good example, because the equals matcher will
correctly fail to match rather than throw an exception if the varargs arguments
have inconsistent types. As it stands, a method like this is needed to
side-step the warning:
@SafeVarargs
public static <T> Matcher<Iterable<? extends T>> contains(final T... contents) {
return Matchers.contains(new ArrayList<Matcher<? super T>>() {{
for (T content : contents) {
add(Matchers.equalTo(content));
}
}});
}
Alternatively, if the contains methods and friends were overloaded with an
implementation that took an Iterable<E> rather than an E... (which might
require changing the signature of the varargs version to E, E...), the problem
could be pushed out of the library's domain.
Original issue reported on code.google.com by ms5...@gmail.com on 22 Dec 2012 at 4:24
Original issue reported on code.google.com by
ms5...@gmail.com
on 22 Dec 2012 at 4:24