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

Overriding/OverriddenMethodFilter #452

Merged
merged 4 commits into from
Dec 14, 2015
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions doc/filter.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ Filter class | Description
`NameFilter` ([javadoc](http://spoon.gforge.inria.fr/mvnsites/spoon-core/apidocs/spoon/reflect/visitor/filter/NameFilter.html)) | filters elements by name.
`RegexFilter` ([javadoc](http://spoon.gforge.inria.fr/mvnsites/spoon-core/apidocs/spoon/reflect/visitor/filter/RegexFilter.html)) | filters elements with a regular expression on the element's code.
`CompositeFilter` ([javadoc](http://spoon.gforge.inria.fr/mvnsites/spoon-core/apidocs/spoon/reflect/visitor/filter/CompositeFilter.html)) | defines a composite filter, which can compose several filters together by using `FilteringOperator` ([javadoc](http://spoon.gforge.inria.fr/mvnsites/spoon-core/apidocs/spoon/reflect/visitor/filter/FilteringOperator.html)).
`OverridingMethodFilter` ([javadoc](http://spoon.gforge.inria.fr/mvnsites/spoon-core/apidocs/spoon/reflect/visitor/filter/OverridingMethodFilter.html)) | get all overriding methods from the method given.
`OverriddenMethodFilter` ([javadoc](http://spoon.gforge.inria.fr/mvnsites/spoon-core/apidocs/spoon/reflect/visitor/filter/OverriddenMethodFilter.html)) | get all overridden methods from the method given.

See below a code example about the usage of these filters. Three filters of
them are used. The first returns all AST nodes of type `CtAssignment` ([javadoc](http://spoon.gforge.inria.fr/mvnsites/spoon-core/apidocs/spoon/reflect/code/CtAssignment.html)).
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/spoon/reflect/visitor/filter/InvocationFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package spoon.reflect.visitor.filter;

import spoon.reflect.code.CtInvocation;
import spoon.reflect.declaration.CtMethod;
import spoon.reflect.reference.CtExecutableReference;
import spoon.reflect.visitor.Filter;

Expand All @@ -38,6 +39,16 @@ public InvocationFilter(CtExecutableReference<?> executable) {
this.executable = executable;
}

/**
* Creates a new invocation filter.
*
* @param method
* the executable to be tested for being invoked.
*/
public InvocationFilter(CtMethod<?> method) {
this(method.getReference());
}

@Override
public boolean matches(CtInvocation<?> invocation) {
return invocation.getExecutable().isOverriding(executable);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* Copyright (C) 2006-2015 INRIA and contributors
* Spoon - http://spoon.gforge.inria.fr/
*
* This software is governed by the CeCILL-C License under French law and
* abiding by the rules of distribution of free software. You can use, modify
* and/or redistribute the software under the terms of the CeCILL-C license as
* circulated by CEA, CNRS and INRIA at http://www.cecill.info.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the CeCILL-C License for more details.
*
* The fact that you are presently reading this means that you have had
* knowledge of the CeCILL-C license and that you accept its terms.
*/
package spoon.reflect.visitor.filter;

import spoon.reflect.declaration.CtMethod;
import spoon.reflect.declaration.CtType;
import spoon.reflect.visitor.Filter;

/**
* Gets all overridden method from the method given.
*/
public class OverriddenMethodFilter implements Filter<CtMethod<?>> {
private final CtMethod<?> method;

/**
* Creates a new overridden method filter.
*
* @param method
* the executable to be tested for being invoked
*/
public OverriddenMethodFilter(CtMethod<?> method) {
this.method = method;
}

@Override
public boolean matches(CtMethod<?> element) {
final CtType expectedParent = method.getParent(CtType.class);
final CtType<?> currentParent = element.getParent(CtType.class);
return expectedParent.isAssignableFrom(currentParent.getReference()) //
&& !currentParent.equals(expectedParent) //
&& method.getReference().isOverriding(element.getReference());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* Copyright (C) 2006-2015 INRIA and contributors
* Spoon - http://spoon.gforge.inria.fr/
*
* This software is governed by the CeCILL-C License under French law and
* abiding by the rules of distribution of free software. You can use, modify
* and/or redistribute the software under the terms of the CeCILL-C license as
* circulated by CEA, CNRS and INRIA at http://www.cecill.info.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the CeCILL-C License for more details.
*
* The fact that you are presently reading this means that you have had
* knowledge of the CeCILL-C license and that you accept its terms.
*/
package spoon.reflect.visitor.filter;

import spoon.reflect.declaration.CtMethod;
import spoon.reflect.declaration.CtType;
import spoon.reflect.visitor.Filter;

/**
* Gets all overriding method from the method given.
*/
public class OverridingMethodFilter implements Filter<CtMethod<?>> {
private final CtMethod<?> method;

/**
* Creates a new overriding method filter.
*
* @param method
* the executable to be tested for being invoked
*/
public OverridingMethodFilter(CtMethod<?> method) {
this.method = method;
}

@Override
public boolean matches(CtMethod<?> element) {
final CtType expectedParent = method.getParent(CtType.class);
final CtType<?> currentParent = element.getParent(CtType.class);
return currentParent.isAssignableFrom(expectedParent.getReference()) //
&& !currentParent.equals(expectedParent) //
&& element.getReference().isOverriding(method.getReference());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -215,10 +215,16 @@ public <S extends T> CtExecutableReference<S> getOverridingExecutable(

@Override
public boolean isOverriding(CtExecutableReference<?> executable) {
if (!this.getDeclaringType().isSubtypeOf(executable.getDeclaringType())) {
if (getDeclaringType().isAnonymous()) {
if (!getDeclaringType().getDeclaringType().isSubtypeOf(executable.getDeclaringType())) {
return false;
}
} else if (!getDeclaringType().isSubtypeOf(executable.getDeclaringType())) {
return false;
}
return getSimpleName().equals(executable.getSimpleName());
return getSimpleName().equals(executable.getSimpleName())
&& getParameters().equals(executable.getParameters())
&& getActualTypeArguments().equals(executable.getActualTypeArguments());
}

@Override
Expand Down
Loading