diff --git a/src/main/java/spoon/reflect/visitor/Filter.java b/src/main/java/spoon/reflect/visitor/Filter.java index 1b3142e1bdc..b1aef0ed2d4 100644 --- a/src/main/java/spoon/reflect/visitor/Filter.java +++ b/src/main/java/spoon/reflect/visitor/Filter.java @@ -16,6 +16,8 @@ */ package spoon.reflect.visitor; +import java.util.function.Predicate; + import spoon.reflect.declaration.CtElement; /** @@ -25,11 +27,15 @@ * the type of the filtered elements (an element belonging to the * filtered element must be assignable from T). */ -public interface Filter { +public interface Filter extends Predicate { /** * Tells if the given element matches. * @param element - the element to be checked for a match. Parameter element is never null if {@link Query} is used. */ boolean matches(T element); + @Override + default boolean test(T element) { + return matches(element); + } } diff --git a/src/test/java/spoon/test/filters/FilterTest.java b/src/test/java/spoon/test/filters/FilterTest.java index 19262fe521f..1cdedd6e631 100644 --- a/src/test/java/spoon/test/filters/FilterTest.java +++ b/src/test/java/spoon/test/filters/FilterTest.java @@ -4,6 +4,7 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertSame; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.fail; @@ -14,12 +15,12 @@ import java.util.Iterator; import java.util.List; import java.util.TreeSet; +import java.util.function.Predicate; import org.junit.Before; import org.junit.Test; import spoon.Launcher; -import spoon.SpoonException; import spoon.reflect.code.CtCFlowBreak; import spoon.reflect.code.CtExpression; import spoon.reflect.code.CtFieldAccess; @@ -1284,4 +1285,13 @@ public void testNameFilterWithGenericType() { assertEquals(1, ctFields.size()); assertTrue(ctFields.get(0) instanceof CtField); } + + @Test + public void testFilterAsPredicate() throws Exception { + CtClass foo = factory.Package().get("spoon.test.filters").getType("Foo"); + //contract: Spoon Filter is compatible with java.util.function.Predicate + Predicate predicate = new NamedElementFilter<>(CtClass.class, "Foo"); + assertTrue(predicate.test(foo)); + assertFalse(predicate.test(foo.getTypeMembers().get(0))); + } }