Reputation: 251
I am trying to use predicates, but I can't because method overloading is acting up...
I want to use filter with an array (varargs), and I want to use the built-in method in predicates, that filters the array as converted to a list.
This is the error: The method filter(Iterable, Predicate) in the type Predicates is not applicable for the arguments (Class[], Predicate)
private static final Predicate<Method> isTestMethod = new Predicate<Method>() {
@Override
public boolean evaluate(Method input) {
return input.isAnnotationPresent(Test.class);
}
};
public static void testClasses(Class<?>... classes) {
for (Method method : filter(classes, isTestMethod)) {
}
}
This is the predicates methods:
/**
* Returns the elements of <tt>unfiltered</tt> that satisfy a predicate.
*
* @param unfiltered An iterable containing objects of any type
* that will be filtered and used as the result.
* @param predicate The predicate to use for evaluation.
* @return An iterable containing all objects which passed the predicate's evaluation.
*/
public static <T> Iterable<T> filter(Iterable<T> unfiltered, Predicate<T> predicate) {
checkNotNull(unfiltered);
checkNotNull(predicate);
List<T> result = new ArrayList<T>();
Iterator<T> iterator = unfiltered.iterator();
while (iterator.hasNext()) {
T next = iterator.next();
if (predicate.evaluate(next)) {
result.add(next);
}
}
return result;
}
/**
* Returns the elements of <tt>unfiltered</tt> that satisfy a predicate.
*
* @param unfiltered An array containing objects of any type
* that will be filtered and used as the result.
* @param predicate The predicate to use for evaluation.
* @return An iterable containing all objects which passed the predicate's evaluation.
*/
public static <T> Iterable<T> filter(T[] unfiltered, Predicate<T> predicate) {
return filter(Arrays.asList(unfiltered), predicate);
}
Upvotes: 1
Views: 973
Reputation: 251
Nevermind. I am an idiot.
for (Class<?> testClass : classes) {
for (Method method : filter(testClass.getClass().getMethods(), isTestMethod)) {
}
}
Upvotes: 2
Reputation: 1500525
Your filter is applicable to methods - but you have a collection of classes. You can't apply your isTestMethod
predicate to a class...
What did you anticipate it would do? Were you perhaps looking for a filter to match classes which had any test methods?
Upvotes: 4