-
-
Notifications
You must be signed in to change notification settings - Fork 351
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a6fb067
commit cb4deb3
Showing
11 changed files
with
522 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** | ||
* Copyright (C) 2006-2016 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; | ||
|
||
import spoon.reflect.declaration.CtElement; | ||
|
||
/** | ||
* Allows functional processing of elements. | ||
* The client algorithm can call {@link #accept(CtElement)} | ||
* with some input element and ElementPipe implementation will call | ||
* output {@link ElementConsumer} zero, one or more times | ||
* with input element or any other computed element. | ||
* | ||
* The {@link ElementPipe} can be used in QueryBuilder on place of {@link Filter} | ||
* | ||
* @param <T> type of elements produced by this ElementPipe | ||
*/ | ||
public interface ElementPipe<I extends CtElement, O extends CtElement> extends ElementConsumer<I>, ElementSupplier<O> { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/** | ||
* Copyright (C) 2006-2016 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; | ||
|
||
import spoon.reflect.declaration.CtElement; | ||
|
||
/** | ||
* Allows functional processing of elements. | ||
* For example the QueryBuilder can accept implementation of this inteface | ||
* and use it produce elements for next level of filtering | ||
* | ||
* @param <T> type of elements produced by this ElementPipe | ||
*/ | ||
public interface ElementSupplier<T extends CtElement> { | ||
/** | ||
* Is usually called once to initialize output ElementConsumer. | ||
* @param output | ||
*/ | ||
void setOutput(ElementConsumer<T> output); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package spoon.reflect.visitor; | ||
|
||
import spoon.reflect.declaration.CtElement; | ||
import spoon.reflect.visitor.chain.ElementPipeImpl; | ||
import spoon.reflect.visitor.filter.AbstractFilter; | ||
import spoon.support.util.RtHelper; | ||
import spoon.support.util.SafeInvoker; | ||
|
||
/** | ||
* | ||
* @param <T> type of element produced by this pipe | ||
*/ | ||
public class FilterPipe<T extends CtElement> extends ElementPipeImpl<T> { | ||
|
||
private Filter<? super CtElement> filter; | ||
private SafeInvoker<Filter<? super CtElement>> invoke_matches = new SafeInvoker<>(CtElement.class, "matches"); | ||
private SafeInvoker<Filter<? super CtElement>> invoke_setFilterInput = new SafeInvoker<>(CtElement.class, "setFilterInput"); | ||
/* | ||
* Type which is accepted by the Filter#matches() | ||
*/ | ||
private Class<? super CtElement> filterType; | ||
|
||
public FilterPipe() { | ||
} | ||
|
||
public FilterPipe(Filter<? super CtElement> filter) { | ||
setFilter(filter); | ||
} | ||
|
||
@Override | ||
public void accept(CtElement element) { | ||
if (element == null) { | ||
return; | ||
} | ||
//first check if filter matches | ||
boolean matches = true; | ||
if (filter != null) { | ||
try { | ||
if ((filterType == null || filterType.isAssignableFrom(element.getClass()))) { | ||
matches = filter.matches(element); | ||
} | ||
} catch (ClassCastException e) { | ||
// expected, some elements are not of type T | ||
// Still need to protect from CCE, if users extend Filter (instead of AbstractFilter) directly, | ||
// but with concrete type parameter | ||
matches = false; | ||
} | ||
} | ||
if (matches) { | ||
//it matches. Send the element to output | ||
fireElement(element); | ||
} | ||
} | ||
|
||
public Filter<? super CtElement> getFilter() { | ||
return filter; | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public void setFilter(Filter<? super CtElement> filter) { | ||
this.filter = filter; | ||
if (filter != null) { | ||
if (filter instanceof AbstractFilter) { | ||
filterType = ((AbstractFilter<? super CtElement>) filter).getType(); | ||
} else { | ||
Class<?>[] params = RtHelper.getMethodParameterTypes(filter.getClass(), "matches", 1); | ||
filterType = (Class<? super CtElement>) params[0]; | ||
} | ||
} else { | ||
filterType = null; | ||
} | ||
} | ||
|
||
public Class<? super CtElement> getFilterType() { | ||
return filterType; | ||
} | ||
|
||
public void runOnScanner(CtElement inputElement, QueryVisitor<T> p_queryVisitor) { | ||
if (filter != null) { | ||
if (filter instanceof ChainableFilter && inputElement != null) { | ||
try { | ||
((ChainableFilter<CtElement, CtElement>) filter).setFilterInput(inputElement); | ||
} catch (ClassCastException e) { | ||
//the filter does not accepts this as input. Ignore this not matching element | ||
return; | ||
} | ||
} | ||
if (filter instanceof ScopeAwareFilter) { | ||
queryVisitor.scan(((ScopeAwareFilter) filter).getSearchScope()); | ||
} else { | ||
queryVisitor.scan(inputElement); | ||
} | ||
} | ||
scan(element); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.