Spoon is an open source library for analyzing, rewriting, transforming, and transpiling Java source code. It builds an abstract syntax tree model of Java source files that can then be queried, analyzed, and transformed before outputting modified source code. The presentation demonstrated how to use Spoon for a scenario where all types from a private API are prevented from being returned from a public API by throwing a dedicated exception from the identified methods. This was achieved by building a model from source code, querying the model, transforming nodes by adding throw statements, and outputting the modified source files. Processors provide an alternative way to perform transformations by attaching behavior to specific node types during model traversal.
Spoon: Open Source Library to analyze, rewrite, transform, transpile Java Source Code - A Getting Started
1. Spoon: Open Source Library to Analyze,
Rewrite, Transform, Transpile Java Source
Code - A getting started
Simon Urli
simon.urli@inria.fr
https://github.com/INRIA/spoon
7th, June 2018
OW2Conâ18
1/40
2. History & Community
⢠2005: project creation at
INRIA Lille
⢠2014: Spoon on Github
⢠2016: Spoon at OW2
⢠2017: Community Award at
OW2Con
351 stars
1895 commits since
2014-04-01
30 contributors
21 releases
351 stars
1895 commits since
2014-04-01
30 contributors
21 releases
351 stars
1895 commits since
2014-04-01
30 contributors
21 releases
351 stars
1895 commits since
2014-04-01
30 contributors
21 releases26 releases
40 contributors
516 stars
(+333)2,228 commits
since last year
(+165)
(+10)
(+5)
since January 1st 2014
2/40
3. Spoon in a nutshell
A library to write your own analysis and transformation in java.
build
Spoon Model
tranform
analysis
data, e.g.
metrics
pretty print
analysis
Java source ďŹles
Java source ďŹles
Usage examples:
- test improvement
- transpilation, e.g. Java to Javascript
- detection of bad smells
- automatic refactoring
3/40
5. Spoon standard process
1. Build a model of your project
2. Query and analyze interesting parts
3. Transform what needs to be transformed
4. Output a transformed source code
4/40
6. Building a Spoon model
Inputs:
⢠Source code (i.e. java ďŹles or snippets)
⢠Many options like classpath, Java version, ...
Output:
⢠Spoon model
5/40
12. Transform model
Transforming model involve performing basic CRUD operations on
the nodes of the model.
⢠Create a node
⢠Update a node
11/40
13. Transform model
Transforming model involve performing basic CRUD operations on
the nodes of the model.
⢠Create a node
⢠Update a node
⢠Delete a node
12/40
14. Output the model
Spoon provides a default Java pretty-printer:
⢠output the model using standard Java code-style
⢠create ďŹles hierarchy based on the compilation units or types
13/40
18. Building a Spoon model from Maven
From a Maven Project:
Automatically get the libraries from Maven dependencies.
15/40
19. Building a Spoon model from sources
From input source of a project:
Be careful with the classpath of your project. Donât hesitate to use
noclasspath mode. (default in next version of Spoon)
16/40
20. Query and analyze model: scenario
Goal:
1. retrieve all types from the public API package
17/40
21. Query and analyze model: scenario
Goal:
1. retrieve all types from the public API package
2. retrieve all methods from those types
18/40
22. Query and analyze model: scenario
Goal:
1. retrieve all types from the public API package
2. retrieve all methods from those types
3. retrieve methods that are public
19/40
23. Query and analyze model: scenario
Goal:
1. retrieve all types from the public API package
2. retrieve all methods from those types
3. retrieve methods that are public
4. and returns a type from the private API package
20/40
24. Transform model: scenario - Plan
Goal: prevent using the detected methods
How to do it?
⢠Comments all statements
⢠Throw a dedicated RuntimeException
⢠Add an explanatory comment
Remember: itâs only an educational example :-)
21/40
25. Transform model: scenario - Which exception?
What exception should be throw?
We actually already have one!
22/40
36. It looks tedious? Use Processors!
Processors are a way to avoid calling all those steps:
⢠A processor is created for a speciďŹc kind of node in Spoon
Model
⢠A process method is called each time the node type is
encountered in the model
⢠We can then check properties and/or transform the node or
the model itself
Letâs try it!
33/40
43. Conclusion
Spoon is a multi-tool for Java Projects
This presentation was only about a basic usage of Spoon.
Projects are using Spoon for code analysis, program repair, code
transpiling, or test ampliďŹcation.
Use it in your own projects: for architecture checking, code
refactoring, test enforcing, ...
New features are incoming to help you there!
We have an incredible community! Come help us improving Spoon
API and get ready for Java 11 ;-)
40/40