-
-
Notifications
You must be signed in to change notification settings - Fork 358
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
feature: template parameters collecting TemplateMatcher #1080
Comments
It's a very interesting improvement of template matchers. |
actually, there a natural way of getting the matched expression: taking the template parameter name:
then, one gets the value of match as follows:
|
I searched through existing code and have found no In Future I would like to use class TemplateMatcher implements Filter {
public boolean match(CtElement element) {
return this.match(element, this.templateRoot);
}
... current code of TemplateMatcher ...
} So it can be used in query to search for elements, which matches the template. In my case I expect that TemplateMatcher will usually match more then one element, so I need TemplateMatcher API, which will give me access to actual template parameters for each element. S1) May be the code based on your TemplateMatcher matcher = new TemplateMatcher(template);
rootPackage.filterChildren(matcher).map(match->{
//this code is called immediately after `matcher` matches the `match` element
//so may be we can access matcher's current state like this
CtElement templateParam1 = matcher.getMatchedElement("_col_");
... do something with templateParam1 ...
}).list(); This approach is good for some use cases. For my use case I need method interface TemplateMatcher{
Map<String,CtElement> getMatchedTemplateParameterElements()
} The later call of aMatcher.getMatchedTemplateParameterElements().get("_col_") ->the matched expression would return same matched expression like in your case. S2) So I will probably write code with following behavior TemplateMatcher matcher = new TemplateMatcher(template);
Map<CtElement,Map<String, CtElement>> matchToParams = new IdentityHashMap<>();
rootPackage.filterChildren(matcher).map(match->{
//this code is called immediately after `matcher` matches the `match` element
//so may be we can access matcher's current state like this
matchToParams.put(match, matcher.getMatchedTemplateParameterElements());
return false;
}).list(); I will then process |
This sounds like a very meaningful and interesting first baby step. |
Good idea. I will make a PR for that |
Will be solved by #1686 |
About advanced matching, see this paper https://homes.cs.washington.edu/~remywang/yoko.pdf and in particular the operators at the end of Section 2.2 |
closed by #1686 |
TemplateMatcher actually returns the list of model elements, which matches the Template T1.
It would be nice if TemplateMatcher would not return only list of matching elements, but for each matched element it might return Template parameters, which can be used to build that element using Template T1.
I am speaking about inverse process to the Template T1.apply(templateParameters TP1) creates an element E1_1.
What it would be good for? Replacing of one design pattern implementation by another implementation.
Example:
code like
might be automatically replaced by modern
for(ItemType item : cllection){...bodyOfLoop...)
Algoritm:
templateOldLoop
oldLoop
create model elementnewLoop
of modern loop usingtemplateModernLoop
+actual template parametersAnd not only that ;-)
The text was updated successfully, but these errors were encountered: