-
-
Notifications
You must be signed in to change notification settings - Fork 344
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: reusable and efficient CtQueries (#1090)
* Support of CtScanner based queries - Extraction of code to CtBaseQuery to make code cleaner and to allow reuse of queries - CtFunction can return Iterator now too * added concept diagram * Simplified and thread safe implementation of CtBaseQueryImpl * fix exception handling. Improve tests * Remove generic from CtQueryable, add CtQuery#list(Class) * rename CtBaseQuery#apply to evaluate Change CtConsumable#accept ... CtConsumer<R> to CtConsumer<Object> * added sample query: OverridenMethodQuery + tests * fix compilation error * fix compilation error * add missing javadoc and CtQuery#setInput * fix return type of setInput * merge CtBaseQueryImpl into CtQueryImpl * removes Step + doc * improve query test * return CtQueryImpl instead of CtQuery * fix checkstyle problem * remove by mistake committed test * pass IntercessionTest * improve tests and fix NPE * <R extends Object> List<R> list() * remove useless generic <T extends CtQuery> T setInput(Object... input); * remove query-elements.png * add useless generic to pass the test * update tests * update doc * fix the documentation * javaDoc JDK 8 compatibility * improve CtQueryImpl code and fix documentation
- Loading branch information
1 parent
802860f
commit 319cb70
Showing
10 changed files
with
722 additions
and
308 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
39 changes: 39 additions & 0 deletions
39
src/main/java/spoon/reflect/visitor/chain/CtConsumableFunction.java
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,39 @@ | ||
/** | ||
* 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.chain; | ||
|
||
/** | ||
* Represents a function, as {@link CtFunction}. However, the main difference is that | ||
* while a {@link CtFunction} returns something with a standard Java return keyword, | ||
* a {@link CtConsumableFunction} returns something by passing the returned object | ||
* as parameter to the given outpuConsumer#accept. This enables to write efficient and concise code in certain situations. | ||
* It also enables one to emulate several returns, by simply calling several times accept, while not paying | ||
* the code or performance price of creating a list or an iterable object. | ||
* | ||
* It is typically used as parameter of {@link CtQueryable#map(CtConsumableFunction)}, can be written as one-liners | ||
* with Java8 lambdas:.`cls.map((CtClass&t;?> c, CtConsumer&t;Object> out)->out.accept(c.getParent()))` | ||
* | ||
* @param <T> the type of the input to the function | ||
*/ | ||
public interface CtConsumableFunction<T> { | ||
/** | ||
* Evaluates the function on the given input. | ||
* @param input the input of the function | ||
* @param outputConsumer the consumer which accepts the results of this function. | ||
*/ | ||
void apply(T input, CtConsumer<Object> outputConsumer); | ||
} |
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,27 @@ | ||
/** | ||
* 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.chain; | ||
|
||
/** | ||
* The functional interface used to receive objects. | ||
* It is used for example to receive results of the query in {@link CtQuery#forEach(CtConsumer)} | ||
* | ||
* @param <T> - the type of accepted elements | ||
*/ | ||
public interface CtConsumer<T> { | ||
void accept(T t); | ||
} |
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.