diff --git a/src/main/java/spoon/reflect/visitor/filter/SameFilter.java b/src/main/java/spoon/reflect/visitor/filter/SameFilter.java new file mode 100644 index 00000000000..d6fa514d5dc --- /dev/null +++ b/src/main/java/spoon/reflect/visitor/filter/SameFilter.java @@ -0,0 +1,27 @@ +/** + * SPDX-License-Identifier: (MIT OR CECILL-C) + * + * Copyright (C) 2006-2019 INRIA and contributors + * + * Spoon is available either under the terms of the MIT License (see LICENSE-MIT.txt) of the Cecill-C License (see LICENSE-CECILL-C.txt). You as the user are entitled to choose the terms under which to adopt Spoon. + */ +package spoon.reflect.visitor.filter; + +import spoon.reflect.declaration.CtElement; +import spoon.reflect.visitor.Filter; + +/** Finds the element given in parameter, useful for checking if an element is in an ancestor. + * Here "same" refers to the Junit meaning: same object memory, equals with == +*/ +public class SameFilter implements Filter { + private final CtElement argument2; + + public SameFilter(CtElement argument2) { + this.argument2 = argument2; + } + + @Override + public boolean matches(CtElement element) { + return element == argument2; + } +} diff --git a/src/test/java/spoon/test/replace/ReplaceParametrizedTest.java b/src/test/java/spoon/test/replace/ReplaceParametrizedTest.java index 34f2664bf98..2cff5fcabc0 100644 --- a/src/test/java/spoon/test/replace/ReplaceParametrizedTest.java +++ b/src/test/java/spoon/test/replace/ReplaceParametrizedTest.java @@ -38,7 +38,7 @@ import spoon.reflect.reference.CtTypeReference; import spoon.reflect.visitor.CtScanner; import spoon.reflect.visitor.CtVisitable; -import spoon.reflect.visitor.Filter; +import spoon.reflect.visitor.filter.SameFilter; import java.util.ArrayList; import java.util.Collection; @@ -174,12 +174,7 @@ public void scan(CtRole role, CtElement e) { argument.replace(argument2); // the new element is indeed now in this AST - assertTrue(receiver.getClass().getSimpleName() + " failed for " + mmField, receiver.getElements(new Filter() { - @Override - public boolean matches(CtElement element) { - return element == argument2; - } - }).size() == 1); + assertTrue(receiver.getClass().getSimpleName() + " failed for " + mmField, receiver.getElements(new SameFilter(argument2)).size() == 1); } if (!problems.isEmpty()) { fail(getReport(problems));