Skip to content
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

Closed
monperrus opened this issue Nov 3, 2018 · 12 comments
Closed

preparation of a post with @ftomassetti #2742

monperrus opened this issue Nov 3, 2018 · 12 comments
Labels
community Related to the Spoon community itself.

Comments

@monperrus
Copy link
Collaborator

@ftomassetti, we'd be happy to answer here to your questions and debate about source code analysis and transformation 🎺

@monperrus monperrus added the community Related to the Spoon community itself. label Nov 3, 2018
@ftomassetti
Copy link

Hi, I would instead have the questions and debate on a google doc for two reasons:

  1. Comments and discussions are easier in my view
  2. SEO Reasons: if here we write the same content I will copy in the post this could look as a copy of an existing page, which could be bad

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

@ftomassetti
Copy link

I started planning a general structure of the post here. What do you think?

@monperrus
Copy link
Collaborator Author

Hi Federico, I've just added some content in the Gdoc. WDYT?

@ftomassetti
Copy link

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:

  1. In general make people aware of the fact that some things can be done by processing code. This would be relevant even for developers that use C# or Scala and therefore would not be directly interested in Spoon
  2. At the same time we can describe how to achieve these things using Spoon. This would not be a giant tutorial on Spoon, but it should just give an idea of what Spoon can do and how difficult would be to perform a certain task using Spoon

I will also copy this description in the google doc

@monperrus
Copy link
Collaborator Author

monperrus commented Nov 7, 2018 via email

@ftomassetti
Copy link

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

@pvojtechovsky
Copy link
Collaborator

@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:

  1. search for all occurrences of OldPattern
  2. for each match of OldPattern remember matched elements and matching parameters
  3. generate new elements by NewPattern and matching parameters
  4. replace matched elements by newly generated elements

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.

@ftomassetti
Copy link

Looking at this example I do not see where the pattern is described. Is that in the class OldPattern?

@pvojtechovsky
Copy link
Collaborator

pvojtechovsky commented Nov 10, 2018

where the pattern is described?

OldPattern#patternModel and NewPattern#patternModel are the patterns

@monperrus
Copy link
Collaborator Author

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!

@ftomassetti
Copy link

We want to write the most memorable and interesting post on the subject of code processing & Spoon :)

@monperrus
Copy link
Collaborator Author

The post is online see https://tomassetti.me/analyze-generate-and-transform-java-code/

See also discussion in #2744

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
community Related to the Spoon community itself.
Projects
None yet
Development

No branches or pull requests

3 participants