Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: add utility class SameFilter #3391

Merged
merged 3 commits into from
Jun 4, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/main/java/spoon/reflect/visitor/filter/SameFilter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* 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 */
Copy link
Collaborator

@MartinWitt MartinWitt Jun 4, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe add in the doc, that you check with ==. The term "same" is sadly not well known and sometimes used instead of equals. "Two elements are the same if a == b is true"

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed, thanks!

public class SameFilter implements Filter<CtElement> {
private final CtElement argument2;

public SameFilter(CtElement argument2) {
this.argument2 = argument2;
}

@Override
public boolean matches(CtElement element) {
return element == argument2;
}
}
9 changes: 2 additions & 7 deletions src/test/java/spoon/test/replace/ReplaceParametrizedTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<CtElement>() {
@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));
Expand Down