Open monperrus opened 6 years ago
I am not sure if I understood you well. But have a look at
https://github.com/pvojtechovsky/spoon/blob/5919ed784e5fde86e4b9246c0ea24c989f269a45/src/test/java/spoon/test/template/CodeReplaceTest.java testTemplateReplace
There are two patterns, first is matching (founds a code location and takes current parameters) and second generates new code (using own template and current parameters) and then old code is replaced by new code.
Have a look at https://github.com/pvojtechovsky/spoon/blob/5919ed784e5fde86e4b9246c0ea24c989f269a45/src/main/java/spoon/pattern/node/RootNode.java too It is the core interface of Pattern node, which shows that Pattern can always do both matching and generating:
public interface RootNode extends Matchers {
/**
* Generates zero, one or more target depending on kind of this {@link RootNode}, expected `result` and input `parameters`
* @param generator {@link Generator} which drives generation process
* @param result holder for the generated objects
* @param parameters a {@link ParameterValueProvider} holding parameters
*/
<T> void generateTargets(Generator generator, ResultHolder<T> result, ParameterValueProvider parameters);
/**
* @param targets to be matched target nodes and input parameters
* @param nextMatchers Chain of matchers which has to be processed after this {@link RootNode}
* @return new parameters and container with remaining targets
*/
TobeMatched matchTargets(TobeMatched targets, Matchers nextMatchers);
Patterns (#1686) are almost this. The final step is to given then a Coccinelle look'n'feel.
Documentation about SmPL: http://coccinelle.lip6.fr/documentation.php
See also Automating Program Transformation for Java Using Semantic Patches https://hal.inria.fr/hal-02023368v1
FYI new version of @JuliaLawall's paper: Semantic Patches for Java Program Transformation
FYI, a paper on that topic "Semantic Patches for Adaptation of JavaScript Programs to Evolving Libraries." https://cs.au.dk/~amoeller/papers/jsfix/paper.pdf
Imagine that you want to match a code location and transform it at the same time, this is sometimes called a "semantic patch" (as opposed to a line-based patch).
In the C world, there is an excellent tool for this, called Coccinelle, see http://coccinelle.lip6.fr/.
In Spoon, it would mean creating an abstraction that would gather a
TemplateMatcher
andTemplate
in a same object. In other terms, we are really close to have "semantic patches" in Spoon.@pvojtechovsky since you're extensively working on this part, what do you think?