DirectoryStreamFilters.anyOf and allOf are not safe
Rémi Forax
forax at univ-mlv.fr
Wed Oct 22 15:23:12 PDT 2008
There is three differents bugs :
1) anyOf() of use a raw type as return type:
<T extends FileRef> DirectoryStream.Filter anyOf(final
DirectoryStream.Filter<? extends T>... filters)
--------------------------------------------------------^
there is no T here !
This bug is not currently detected by javac but will be soon,
see http://blogs.sun.com/mcimadamore/entry/diagnosing_raw_types
2) Java doesn't allow to create an array of parameteried types so
declaring a varargs of parameterized type is not a good idea.
It's better to use an Iterable here.
3) casting a DirectoryStream.Filter<? extends T> to a
DirectoryStream.Filter<T> is unsafe and will lead to CCE.
As Mark Thornton wrote the JDK should use DirectoryStream.Filter<?
super T>.
anyOf should be written like that:
public static <T extends FileRef> DirectoryStream.Filter<T>
anyOf(final Iterable<? extends DirectoryStream.Filter<? super T>>
filters) {
return new DirectoryStream.Filter<T>() {
@Override
public boolean accept(T entry) {
for (DirectoryStream.Filter<? super T> filter: filters) {
if (filter.accept(entry))
return true;
}
return false;
}
};
}
Rémi
More information about the nio-dev
mailing list