SlideShare a Scribd company logo
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
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
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 files
Java source files
Usage examples:
- test improvement
- transpilation, e.g. Java to Javascript
- detection of bad smells
- automatic refactoring
3/40
Spoon Overview
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
Building a Spoon model
Inputs:
• Source code (i.e. java files or snippets)
• Many options like classpath, Java version, ...
Output:
• Spoon model
5/40
Spoon AST Metamodel (excerpt)
6/40
Spoon model example
7/40
Query and analyze model
Different ways of doing it:
• Using Query API (filter-chaining API)
• Using XPath-like API
8/40
Transform model
Transforming model involve performing basic CRUD operations on
the nodes of the model.
9/40
Transform model
Transforming model involve performing basic CRUD operations on
the nodes of the model.
• Create a node
10/40
Transform model
Transforming model involve performing basic CRUD operations on
the nodes of the model.
• Create a node
• Update a node
11/40
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
Output the model
Spoon provides a default Java pretty-printer:
• output the model using standard Java code-style
• create files hierarchy based on the compilation units or types
13/40
Quick scenario
Scenario
You want to enforce that all types from the private API are not
returned from the public API.
14/40
Building the model
Building a Spoon model from Maven
From a Maven Project:
Automatically get the libraries from Maven dependencies.
15/40
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
Query and analyze model: scenario
Goal:
1. retrieve all types from the public API package
17/40
Query and analyze model: scenario
Goal:
1. retrieve all types from the public API package
2. retrieve all methods from those types
18/40
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
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
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
Transform model: scenario - Which exception?
What exception should be throw?
We actually already have one!
22/40
Transform model: hands on!
Goal: Get the exception class.
23/40
Transform model: hands on!
Goal: Create an instance of the exception class.
24/40
Transform model: hands on!
Goal: Iterate over the queried methods.
25/40
Transform model: hands on!
Goal: Create comments for all statements.
26/40
Transform model: hands on!
Goal: Create a new throw statement with the call to the new
instance of the exception.
27/40
Transform model: hands on!
Goal: Add a final explanatory comment.
28/40
Let’s output the model
29/40
Output the model: scenario
What about the imports and comments?
30/40
Manage imports and comments
31/40
Output the model with imports and comments
32/40
It looks tedious? Use Processors!
Processors are a way to avoid calling all those steps:
• A processor is created for a specific 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
Processor for our scenario - Structure
34/40
Processor for our scenario - isToBeProcessed
Method contains code to query the model
35/40
Processor for our scenario - process
Method contains code to transform the model
36/40
Processor usage from Java API
37/40
Processor usage from CLI
38/40
Processor usage from Maven Plugin
39/40
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 amplification.
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

More Related Content

Similar to Spoon: Open Source Library to analyze, rewrite, transform, transpile Java Source Code - A Getting Started

Deep Dive in Java 9+
Deep Dive in Java 9+Deep Dive in Java 9+
Deep Dive in Java 9+
Miro Cupak
 
JUG Münster 2014 - Code Recommenders & Codetrails - Wissenstransfer 2.0
JUG Münster 2014 - Code Recommenders & Codetrails - Wissenstransfer 2.0JUG Münster 2014 - Code Recommenders & Codetrails - Wissenstransfer 2.0
JUG Münster 2014 - Code Recommenders & Codetrails - Wissenstransfer 2.0
Marcel Bruch
 
Distributed ML in Apache Spark
Distributed ML in Apache SparkDistributed ML in Apache Spark
Distributed ML in Apache Spark
Databricks
 
Software Design Patterns in Laravel by Phill Sparks
Software Design Patterns in Laravel by Phill SparksSoftware Design Patterns in Laravel by Phill Sparks
Software Design Patterns in Laravel by Phill Sparks
Phill Sparks
 
Automated testing 101
Automated testing 101Automated testing 101
Automated testing 101
Tabitha Chapman
 
Effective Android Development. UA Mobile 2016.
Effective Android Development. UA Mobile 2016.Effective Android Development. UA Mobile 2016.
Effective Android Development. UA Mobile 2016.
UA Mobile
 
Developing Interactive systems - lesson 2
Developing Interactive systems - lesson 2Developing Interactive systems - lesson 2
Developing Interactive systems - lesson 2
Sónia
 
Apache Spark Model Deployment
Apache Spark Model Deployment Apache Spark Model Deployment
Apache Spark Model Deployment
Databricks
 
How to use the Eclipse E4 model editor for modular RCP applications
How to use the Eclipse E4 model editor for modular RCP applicationsHow to use the Eclipse E4 model editor for modular RCP applications
How to use the Eclipse E4 model editor for modular RCP applications
Patrik Suzzi
 
Coding Standard And Code Review
Coding Standard And Code ReviewCoding Standard And Code Review
Coding Standard And Code Review
Milan Vukoje
 
Few minutes To better Code - Refactoring
Few minutes To better Code - RefactoringFew minutes To better Code - Refactoring
Few minutes To better Code - Refactoring
Diaa Al-Salehi
 
Lap around ALM with Visual Studio and TFS 2013
Lap around ALM with Visual Studio and TFS 2013Lap around ALM with Visual Studio and TFS 2013
Lap around ALM with Visual Studio and TFS 2013
Paul Hacker
 
Design Patterns
Design PatternsDesign Patterns
Design Patterns
Srikrishnan Suresh
 
Setting Up CircleCI Workflows for Your Salesforce Apps
Setting Up CircleCI Workflows for Your Salesforce AppsSetting Up CircleCI Workflows for Your Salesforce Apps
Setting Up CircleCI Workflows for Your Salesforce Apps
Daniel Stange
 
Azure Resource Manager templates: Improve deployment time and reusability
Azure Resource Manager templates: Improve deployment time and reusabilityAzure Resource Manager templates: Improve deployment time and reusability
Azure Resource Manager templates: Improve deployment time and reusability
Stephane Lapointe
 
Model-Based Systems Engineering Tool How To Use Innoslate Pt. 2
Model-Based Systems Engineering Tool How To Use Innoslate Pt. 2Model-Based Systems Engineering Tool How To Use Innoslate Pt. 2
Model-Based Systems Engineering Tool How To Use Innoslate Pt. 2
Elizabeth Steiner
 
Concurrency Programming in Java - 02 - Essentials of Java Part 1
Concurrency Programming in Java - 02 - Essentials of Java Part 1Concurrency Programming in Java - 02 - Essentials of Java Part 1
Concurrency Programming in Java - 02 - Essentials of Java Part 1
Sachintha Gunasena
 
iPhone Development Tools
iPhone Development ToolsiPhone Development Tools
iPhone Development Tools
Omar Cafini
 
Design p atterns
Design p atternsDesign p atterns
Design p atterns
Amr Abd El Latief
 
INTP-362 Course Introduction.pptx
INTP-362 Course Introduction.pptxINTP-362 Course Introduction.pptx
INTP-362 Course Introduction.pptx
PrinceBansal40
 

Similar to Spoon: Open Source Library to analyze, rewrite, transform, transpile Java Source Code - A Getting Started (20)

Deep Dive in Java 9+
Deep Dive in Java 9+Deep Dive in Java 9+
Deep Dive in Java 9+
 
JUG Münster 2014 - Code Recommenders & Codetrails - Wissenstransfer 2.0
JUG Münster 2014 - Code Recommenders & Codetrails - Wissenstransfer 2.0JUG Münster 2014 - Code Recommenders & Codetrails - Wissenstransfer 2.0
JUG Münster 2014 - Code Recommenders & Codetrails - Wissenstransfer 2.0
 
Distributed ML in Apache Spark
Distributed ML in Apache SparkDistributed ML in Apache Spark
Distributed ML in Apache Spark
 
Software Design Patterns in Laravel by Phill Sparks
Software Design Patterns in Laravel by Phill SparksSoftware Design Patterns in Laravel by Phill Sparks
Software Design Patterns in Laravel by Phill Sparks
 
Automated testing 101
Automated testing 101Automated testing 101
Automated testing 101
 
Effective Android Development. UA Mobile 2016.
Effective Android Development. UA Mobile 2016.Effective Android Development. UA Mobile 2016.
Effective Android Development. UA Mobile 2016.
 
Developing Interactive systems - lesson 2
Developing Interactive systems - lesson 2Developing Interactive systems - lesson 2
Developing Interactive systems - lesson 2
 
Apache Spark Model Deployment
Apache Spark Model Deployment Apache Spark Model Deployment
Apache Spark Model Deployment
 
How to use the Eclipse E4 model editor for modular RCP applications
How to use the Eclipse E4 model editor for modular RCP applicationsHow to use the Eclipse E4 model editor for modular RCP applications
How to use the Eclipse E4 model editor for modular RCP applications
 
Coding Standard And Code Review
Coding Standard And Code ReviewCoding Standard And Code Review
Coding Standard And Code Review
 
Few minutes To better Code - Refactoring
Few minutes To better Code - RefactoringFew minutes To better Code - Refactoring
Few minutes To better Code - Refactoring
 
Lap around ALM with Visual Studio and TFS 2013
Lap around ALM with Visual Studio and TFS 2013Lap around ALM with Visual Studio and TFS 2013
Lap around ALM with Visual Studio and TFS 2013
 
Design Patterns
Design PatternsDesign Patterns
Design Patterns
 
Setting Up CircleCI Workflows for Your Salesforce Apps
Setting Up CircleCI Workflows for Your Salesforce AppsSetting Up CircleCI Workflows for Your Salesforce Apps
Setting Up CircleCI Workflows for Your Salesforce Apps
 
Azure Resource Manager templates: Improve deployment time and reusability
Azure Resource Manager templates: Improve deployment time and reusabilityAzure Resource Manager templates: Improve deployment time and reusability
Azure Resource Manager templates: Improve deployment time and reusability
 
Model-Based Systems Engineering Tool How To Use Innoslate Pt. 2
Model-Based Systems Engineering Tool How To Use Innoslate Pt. 2Model-Based Systems Engineering Tool How To Use Innoslate Pt. 2
Model-Based Systems Engineering Tool How To Use Innoslate Pt. 2
 
Concurrency Programming in Java - 02 - Essentials of Java Part 1
Concurrency Programming in Java - 02 - Essentials of Java Part 1Concurrency Programming in Java - 02 - Essentials of Java Part 1
Concurrency Programming in Java - 02 - Essentials of Java Part 1
 
iPhone Development Tools
iPhone Development ToolsiPhone Development Tools
iPhone Development Tools
 
Design p atterns
Design p atternsDesign p atterns
Design p atterns
 
INTP-362 Course Introduction.pptx
INTP-362 Course Introduction.pptxINTP-362 Course Introduction.pptx
INTP-362 Course Introduction.pptx
 

More from Simon Urli

How to Design a Program Repair Bot? Insights from the Repairnator Project
How to Design a Program Repair Bot? Insights from the Repairnator ProjectHow to Design a Program Repair Bot? Insights from the Repairnator Project
How to Design a Program Repair Bot? Insights from the Repairnator Project
Simon Urli
 
How to Design a Program Repair Bot? Insights from the Repairnator Project
How to Design a Program Repair Bot? Insights from the Repairnator ProjectHow to Design a Program Repair Bot? Insights from the Repairnator Project
How to Design a Program Repair Bot? Insights from the Repairnator Project
Simon Urli
 
A Visual Support for Decomposing Complex Feature Models
A Visual Support for Decomposing Complex Feature ModelsA Visual Support for Decomposing Complex Feature Models
A Visual Support for Decomposing Complex Feature Models
Simon Urli
 
GDR GPL 2015 - Processus de Configuration Flexible pour Lignes de Produits Lo...
GDR GPL 2015 - Processus de Configuration Flexible pour Lignes de Produits Lo...GDR GPL 2015 - Processus de Configuration Flexible pour Lignes de Produits Lo...
GDR GPL 2015 - Processus de Configuration Flexible pour Lignes de Produits Lo...
Simon Urli
 
Handling Complex Configurations in Software Product Lines: a Tooled Approach
Handling Complex Configurations in Software Product Lines: a Tooled ApproachHandling Complex Configurations in Software Product Lines: a Tooled Approach
Handling Complex Configurations in Software Product Lines: a Tooled Approach
Simon Urli
 
Managing a Software Ecosystem Using a Multiple Software Product Line: a Case ...
Managing a Software Ecosystem Using a Multiple Software Product Line: a Case ...Managing a Software Ecosystem Using a Multiple Software Product Line: a Case ...
Managing a Software Ecosystem Using a Multiple Software Product Line: a Case ...
Simon Urli
 
Vers des interfaces graphiques flexibles de configurations
Vers des interfaces graphiques flexibles de configurationsVers des interfaces graphiques flexibles de configurations
Vers des interfaces graphiques flexibles de configurations
Simon Urli
 
Using Composite Feature Models to Support Agile Software Product Line Evoluti...
Using Composite Feature Models to Support Agile Software Product Line Evoluti...Using Composite Feature Models to Support Agile Software Product Line Evoluti...
Using Composite Feature Models to Support Agile Software Product Line Evoluti...
Simon Urli
 

More from Simon Urli (8)

How to Design a Program Repair Bot? Insights from the Repairnator Project
How to Design a Program Repair Bot? Insights from the Repairnator ProjectHow to Design a Program Repair Bot? Insights from the Repairnator Project
How to Design a Program Repair Bot? Insights from the Repairnator Project
 
How to Design a Program Repair Bot? Insights from the Repairnator Project
How to Design a Program Repair Bot? Insights from the Repairnator ProjectHow to Design a Program Repair Bot? Insights from the Repairnator Project
How to Design a Program Repair Bot? Insights from the Repairnator Project
 
A Visual Support for Decomposing Complex Feature Models
A Visual Support for Decomposing Complex Feature ModelsA Visual Support for Decomposing Complex Feature Models
A Visual Support for Decomposing Complex Feature Models
 
GDR GPL 2015 - Processus de Configuration Flexible pour Lignes de Produits Lo...
GDR GPL 2015 - Processus de Configuration Flexible pour Lignes de Produits Lo...GDR GPL 2015 - Processus de Configuration Flexible pour Lignes de Produits Lo...
GDR GPL 2015 - Processus de Configuration Flexible pour Lignes de Produits Lo...
 
Handling Complex Configurations in Software Product Lines: a Tooled Approach
Handling Complex Configurations in Software Product Lines: a Tooled ApproachHandling Complex Configurations in Software Product Lines: a Tooled Approach
Handling Complex Configurations in Software Product Lines: a Tooled Approach
 
Managing a Software Ecosystem Using a Multiple Software Product Line: a Case ...
Managing a Software Ecosystem Using a Multiple Software Product Line: a Case ...Managing a Software Ecosystem Using a Multiple Software Product Line: a Case ...
Managing a Software Ecosystem Using a Multiple Software Product Line: a Case ...
 
Vers des interfaces graphiques flexibles de configurations
Vers des interfaces graphiques flexibles de configurationsVers des interfaces graphiques flexibles de configurations
Vers des interfaces graphiques flexibles de configurations
 
Using Composite Feature Models to Support Agile Software Product Line Evoluti...
Using Composite Feature Models to Support Agile Software Product Line Evoluti...Using Composite Feature Models to Support Agile Software Product Line Evoluti...
Using Composite Feature Models to Support Agile Software Product Line Evoluti...
 

Recently uploaded

Dublin_mulesoft_meetup_Mulesoft_Salesforce_Integration (1).pptx
Dublin_mulesoft_meetup_Mulesoft_Salesforce_Integration (1).pptxDublin_mulesoft_meetup_Mulesoft_Salesforce_Integration (1).pptx
Dublin_mulesoft_meetup_Mulesoft_Salesforce_Integration (1).pptx
Kunal Gupta
 
"Mastering Graphic Design: Essential Tips and Tricks for Beginners and Profes...
"Mastering Graphic Design: Essential Tips and Tricks for Beginners and Profes..."Mastering Graphic Design: Essential Tips and Tricks for Beginners and Profes...
"Mastering Graphic Design: Essential Tips and Tricks for Beginners and Profes...
Anant Gupta
 
Girls Call Churchgate 9910780858 Provide Best And Top Girl Service And No1 in...
Girls Call Churchgate 9910780858 Provide Best And Top Girl Service And No1 in...Girls Call Churchgate 9910780858 Provide Best And Top Girl Service And No1 in...
Girls Call Churchgate 9910780858 Provide Best And Top Girl Service And No1 in...
maigasapphire
 
Semantic-Aware Code Model: Elevating the Future of Software Development
Semantic-Aware Code Model: Elevating the Future of Software DevelopmentSemantic-Aware Code Model: Elevating the Future of Software Development
Semantic-Aware Code Model: Elevating the Future of Software Development
Baishakhi Ray
 
BT & Neo4j: Knowledge Graphs for Critical Enterprise Systems.pptx.pdf
BT & Neo4j: Knowledge Graphs for Critical Enterprise Systems.pptx.pdfBT & Neo4j: Knowledge Graphs for Critical Enterprise Systems.pptx.pdf
BT & Neo4j: Knowledge Graphs for Critical Enterprise Systems.pptx.pdf
Neo4j
 
(CISOPlatform Summit & SACON 2024) Orientation by CISO Platform_ Using CISO P...
(CISOPlatform Summit & SACON 2024) Orientation by CISO Platform_ Using CISO P...(CISOPlatform Summit & SACON 2024) Orientation by CISO Platform_ Using CISO P...
(CISOPlatform Summit & SACON 2024) Orientation by CISO Platform_ Using CISO P...
Priyanka Aash
 
Acumatica vs. Sage Intacct vs. NetSuite _ NOW CFO.pdf
Acumatica vs. Sage Intacct vs. NetSuite _ NOW CFO.pdfAcumatica vs. Sage Intacct vs. NetSuite _ NOW CFO.pdf
Acumatica vs. Sage Intacct vs. NetSuite _ NOW CFO.pdf
BrainSell Technologies
 
leewayhertz.com-AI agents for healthcare Applications benefits and implementa...
leewayhertz.com-AI agents for healthcare Applications benefits and implementa...leewayhertz.com-AI agents for healthcare Applications benefits and implementa...
leewayhertz.com-AI agents for healthcare Applications benefits and implementa...
alexjohnson7307
 
How to Build a Profitable IoT Product.pptx
How to Build a Profitable IoT Product.pptxHow to Build a Profitable IoT Product.pptx
How to Build a Profitable IoT Product.pptx
Adam Dunkels
 
Russian Girls Call Navi Mumbai 🎈🔥9920725232 🔥💋🎈 Provide Best And Top Girl Ser...
Russian Girls Call Navi Mumbai 🎈🔥9920725232 🔥💋🎈 Provide Best And Top Girl Ser...Russian Girls Call Navi Mumbai 🎈🔥9920725232 🔥💋🎈 Provide Best And Top Girl Ser...
Russian Girls Call Navi Mumbai 🎈🔥9920725232 🔥💋🎈 Provide Best And Top Girl Ser...
bellared2
 
Vertex AI Agent Builder - GDG Alicante - Julio 2024
Vertex AI Agent Builder - GDG Alicante - Julio 2024Vertex AI Agent Builder - GDG Alicante - Julio 2024
Vertex AI Agent Builder - GDG Alicante - Julio 2024
Nicolás Lopéz
 
Three New Criminal Laws in India 1 July 2024
Three New Criminal Laws in India 1 July 2024Three New Criminal Laws in India 1 July 2024
Three New Criminal Laws in India 1 July 2024
aakash malhotra
 
Girls call Kolkata 👀 XXXXXXXXXXX 👀 Rs.9.5 K Cash Payment With Room Delivery
Girls call Kolkata 👀 XXXXXXXXXXX 👀 Rs.9.5 K Cash Payment With Room Delivery Girls call Kolkata 👀 XXXXXXXXXXX 👀 Rs.9.5 K Cash Payment With Room Delivery
Girls call Kolkata 👀 XXXXXXXXXXX 👀 Rs.9.5 K Cash Payment With Room Delivery
sunilverma7884
 
BLOCKCHAIN TECHNOLOGY - Advantages and Disadvantages
BLOCKCHAIN TECHNOLOGY - Advantages and DisadvantagesBLOCKCHAIN TECHNOLOGY - Advantages and Disadvantages
BLOCKCHAIN TECHNOLOGY - Advantages and Disadvantages
SAI KAILASH R
 
Connector Corner: Leveraging Snowflake Integration for Smarter Decision Making
Connector Corner: Leveraging Snowflake Integration for Smarter Decision MakingConnector Corner: Leveraging Snowflake Integration for Smarter Decision Making
Connector Corner: Leveraging Snowflake Integration for Smarter Decision Making
DianaGray10
 
Opencast Summit 2024 — Opencast @ University of Münster
Opencast Summit 2024 — Opencast @ University of MünsterOpencast Summit 2024 — Opencast @ University of Münster
Opencast Summit 2024 — Opencast @ University of Münster
Matthias Neugebauer
 
Feature sql server terbaru performance.pptx
Feature sql server terbaru performance.pptxFeature sql server terbaru performance.pptx
Feature sql server terbaru performance.pptx
ssuser1915fe1
 
Mastering OnlyFans Clone App Development: Key Strategies for Success
Mastering OnlyFans Clone App Development: Key Strategies for SuccessMastering OnlyFans Clone App Development: Key Strategies for Success
Mastering OnlyFans Clone App Development: Key Strategies for Success
David Wilson
 
Evolution of iPaaS - simplify IT workloads to provide a unified view of data...
Evolution of iPaaS - simplify IT workloads to provide a unified view of  data...Evolution of iPaaS - simplify IT workloads to provide a unified view of  data...
Evolution of iPaaS - simplify IT workloads to provide a unified view of data...
Torry Harris
 
Use Cases & Benefits of RPA in Manufacturing in 2024.pptx
Use Cases & Benefits of RPA in Manufacturing in 2024.pptxUse Cases & Benefits of RPA in Manufacturing in 2024.pptx
Use Cases & Benefits of RPA in Manufacturing in 2024.pptx
SynapseIndia
 

Recently uploaded (20)

Dublin_mulesoft_meetup_Mulesoft_Salesforce_Integration (1).pptx
Dublin_mulesoft_meetup_Mulesoft_Salesforce_Integration (1).pptxDublin_mulesoft_meetup_Mulesoft_Salesforce_Integration (1).pptx
Dublin_mulesoft_meetup_Mulesoft_Salesforce_Integration (1).pptx
 
"Mastering Graphic Design: Essential Tips and Tricks for Beginners and Profes...
"Mastering Graphic Design: Essential Tips and Tricks for Beginners and Profes..."Mastering Graphic Design: Essential Tips and Tricks for Beginners and Profes...
"Mastering Graphic Design: Essential Tips and Tricks for Beginners and Profes...
 
Girls Call Churchgate 9910780858 Provide Best And Top Girl Service And No1 in...
Girls Call Churchgate 9910780858 Provide Best And Top Girl Service And No1 in...Girls Call Churchgate 9910780858 Provide Best And Top Girl Service And No1 in...
Girls Call Churchgate 9910780858 Provide Best And Top Girl Service And No1 in...
 
Semantic-Aware Code Model: Elevating the Future of Software Development
Semantic-Aware Code Model: Elevating the Future of Software DevelopmentSemantic-Aware Code Model: Elevating the Future of Software Development
Semantic-Aware Code Model: Elevating the Future of Software Development
 
BT & Neo4j: Knowledge Graphs for Critical Enterprise Systems.pptx.pdf
BT & Neo4j: Knowledge Graphs for Critical Enterprise Systems.pptx.pdfBT & Neo4j: Knowledge Graphs for Critical Enterprise Systems.pptx.pdf
BT & Neo4j: Knowledge Graphs for Critical Enterprise Systems.pptx.pdf
 
(CISOPlatform Summit & SACON 2024) Orientation by CISO Platform_ Using CISO P...
(CISOPlatform Summit & SACON 2024) Orientation by CISO Platform_ Using CISO P...(CISOPlatform Summit & SACON 2024) Orientation by CISO Platform_ Using CISO P...
(CISOPlatform Summit & SACON 2024) Orientation by CISO Platform_ Using CISO P...
 
Acumatica vs. Sage Intacct vs. NetSuite _ NOW CFO.pdf
Acumatica vs. Sage Intacct vs. NetSuite _ NOW CFO.pdfAcumatica vs. Sage Intacct vs. NetSuite _ NOW CFO.pdf
Acumatica vs. Sage Intacct vs. NetSuite _ NOW CFO.pdf
 
leewayhertz.com-AI agents for healthcare Applications benefits and implementa...
leewayhertz.com-AI agents for healthcare Applications benefits and implementa...leewayhertz.com-AI agents for healthcare Applications benefits and implementa...
leewayhertz.com-AI agents for healthcare Applications benefits and implementa...
 
How to Build a Profitable IoT Product.pptx
How to Build a Profitable IoT Product.pptxHow to Build a Profitable IoT Product.pptx
How to Build a Profitable IoT Product.pptx
 
Russian Girls Call Navi Mumbai 🎈🔥9920725232 🔥💋🎈 Provide Best And Top Girl Ser...
Russian Girls Call Navi Mumbai 🎈🔥9920725232 🔥💋🎈 Provide Best And Top Girl Ser...Russian Girls Call Navi Mumbai 🎈🔥9920725232 🔥💋🎈 Provide Best And Top Girl Ser...
Russian Girls Call Navi Mumbai 🎈🔥9920725232 🔥💋🎈 Provide Best And Top Girl Ser...
 
Vertex AI Agent Builder - GDG Alicante - Julio 2024
Vertex AI Agent Builder - GDG Alicante - Julio 2024Vertex AI Agent Builder - GDG Alicante - Julio 2024
Vertex AI Agent Builder - GDG Alicante - Julio 2024
 
Three New Criminal Laws in India 1 July 2024
Three New Criminal Laws in India 1 July 2024Three New Criminal Laws in India 1 July 2024
Three New Criminal Laws in India 1 July 2024
 
Girls call Kolkata 👀 XXXXXXXXXXX 👀 Rs.9.5 K Cash Payment With Room Delivery
Girls call Kolkata 👀 XXXXXXXXXXX 👀 Rs.9.5 K Cash Payment With Room Delivery Girls call Kolkata 👀 XXXXXXXXXXX 👀 Rs.9.5 K Cash Payment With Room Delivery
Girls call Kolkata 👀 XXXXXXXXXXX 👀 Rs.9.5 K Cash Payment With Room Delivery
 
BLOCKCHAIN TECHNOLOGY - Advantages and Disadvantages
BLOCKCHAIN TECHNOLOGY - Advantages and DisadvantagesBLOCKCHAIN TECHNOLOGY - Advantages and Disadvantages
BLOCKCHAIN TECHNOLOGY - Advantages and Disadvantages
 
Connector Corner: Leveraging Snowflake Integration for Smarter Decision Making
Connector Corner: Leveraging Snowflake Integration for Smarter Decision MakingConnector Corner: Leveraging Snowflake Integration for Smarter Decision Making
Connector Corner: Leveraging Snowflake Integration for Smarter Decision Making
 
Opencast Summit 2024 — Opencast @ University of Münster
Opencast Summit 2024 — Opencast @ University of MünsterOpencast Summit 2024 — Opencast @ University of Münster
Opencast Summit 2024 — Opencast @ University of Münster
 
Feature sql server terbaru performance.pptx
Feature sql server terbaru performance.pptxFeature sql server terbaru performance.pptx
Feature sql server terbaru performance.pptx
 
Mastering OnlyFans Clone App Development: Key Strategies for Success
Mastering OnlyFans Clone App Development: Key Strategies for SuccessMastering OnlyFans Clone App Development: Key Strategies for Success
Mastering OnlyFans Clone App Development: Key Strategies for Success
 
Evolution of iPaaS - simplify IT workloads to provide a unified view of data...
Evolution of iPaaS - simplify IT workloads to provide a unified view of  data...Evolution of iPaaS - simplify IT workloads to provide a unified view of  data...
Evolution of iPaaS - simplify IT workloads to provide a unified view of data...
 
Use Cases & Benefits of RPA in Manufacturing in 2024.pptx
Use Cases & Benefits of RPA in Manufacturing in 2024.pptxUse Cases & Benefits of RPA in Manufacturing in 2024.pptx
Use Cases & Benefits of RPA in Manufacturing in 2024.pptx
 

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 files Java source files 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 files or snippets) • Many options like classpath, Java version, ... Output: • Spoon model 5/40
  • 7. Spoon AST Metamodel (excerpt) 6/40
  • 9. Query and analyze model Different ways of doing it: • Using Query API (filter-chaining API) • Using XPath-like API 8/40
  • 10. Transform model Transforming model involve performing basic CRUD operations on the nodes of the model. 9/40
  • 11. Transform model Transforming model involve performing basic CRUD operations on the nodes of the model. • Create a node 10/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 files hierarchy based on the compilation units or types 13/40
  • 16. Scenario You want to enforce that all types from the private API are not returned from the public API. 14/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
  • 26. Transform model: hands on! Goal: Get the exception class. 23/40
  • 27. Transform model: hands on! Goal: Create an instance of the exception class. 24/40
  • 28. Transform model: hands on! Goal: Iterate over the queried methods. 25/40
  • 29. Transform model: hands on! Goal: Create comments for all statements. 26/40
  • 30. Transform model: hands on! Goal: Create a new throw statement with the call to the new instance of the exception. 27/40
  • 31. Transform model: hands on! Goal: Add a final explanatory comment. 28/40
  • 32. Let’s output the model 29/40
  • 33. Output the model: scenario What about the imports and comments? 30/40
  • 34. Manage imports and comments 31/40
  • 35. Output the model with imports and comments 32/40
  • 36. It looks tedious? Use Processors! Processors are a way to avoid calling all those steps: • A processor is created for a specific 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
  • 37. Processor for our scenario - Structure 34/40
  • 38. Processor for our scenario - isToBeProcessed Method contains code to query the model 35/40
  • 39. Processor for our scenario - process Method contains code to transform the model 36/40
  • 40. Processor usage from Java API 37/40
  • 41. Processor usage from CLI 38/40
  • 42. Processor usage from Maven Plugin 39/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 amplification. 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