-
-
Notifications
You must be signed in to change notification settings - Fork 357
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
preparation of a post with @ftomassetti #2742
Comments
Hi, I would instead have the questions and debate on a google doc for two reasons:
So I would work on a list of questions and put them in a google doc that I would share here and which could be accessible to any member of your team |
I started planning a general structure of the post here. What do you think? |
Hi Federico, I've just added some content in the Gdoc. WDYT? |
I think we are progressing. In my opinion the core part of the article should be the development of some use cases. The goals would be two:
I will also copy this description in the google doc |
Those two goals are quite different, but both or them are interesting.
Hence, I would propose to do two different posts:
* When and why using source code transformation? Joint post by the JavaParser and the Spoon teams.
* A short introduction to spoon (where we would not repeat again all possible usages as we do
currently, we could replace this by a highlight on the key features)
|
In my experience it is better to have one long and complete article instead of two. Search engines like them more and also readers. In addition to that having a separate article on using source code transformations would have the risk of being too theoretical, while if we could show how to implement these techniques using SPOON it would be much more actionable. Also for users who are not going to use Spoon directly would still be useful to understand how these techniques look like in practice and what kind of effort is required to use them |
@ftomassetti Here is an example of bulk refactoring. Most of the changes of DefaultJavaPrettyPrinter.java in this PR #1956 were done automatically by Spoon by these steps:
Here is the code which does matching and replacing //create spoon model, which contains to be refactored code, OldPattern and NewPattern.
Factory factory = null;
//old code pattern
Pattern oldPattern = OldPattern.createPatternFromMethodPatternModel(factory);
//new code pattern
Pattern newPattern = NewPattern.createPatternFromNewPattern(factory);
//search AST of root package (contains types, their members, statements, ...) for oldPattern
oldPattern.forEachMatch(factory.getModel().getRootPackage(), (Match match) -> {
//this is called for each match of the oldPattern
//list of matching elements. This code matches old code pattern
List<CtElement> oldCode = match.getMatchingElements();
//generate elements of new code, which should be used instead of oldCode
List<CtStatement> newCode = newPattern.generator().generate(CtStatement.class, match.getParameters());
for (int i = 0; i < oldCode.size(); i++) {
if (i == 0) {
//replace first element of old code by generated code
oldCode.get(i).replace(newCode);
} else {
//delete all other elements of old code
oldCode.get(i).delete();
}
}
}); Note: at the time of #1956, I had to adjust formatting manually after automatic replace. But now we have sniper printer, which keeps source of untouched AST fragments same as original, so it is even less work to do such refactoring on real projects. |
Looking at this example I do not see where the pattern is described. Is that in the class |
|
OK for keeping one post. Note that properly covering one single use case would take at least 2-3 paragraphs and 1-2 listings. Since we have already listed 11 usages, this is a lot of work and content! |
We want to write the most memorable and interesting post on the subject of code processing & Spoon :) |
The post is online see https://tomassetti.me/analyze-generate-and-transform-java-code/ See also discussion in #2744 |
@ftomassetti, we'd be happy to answer here to your questions and debate about source code analysis and transformation 🎺
The text was updated successfully, but these errors were encountered: