SlideShare ist ein Scribd-Unternehmen logo
1 von 43
Downloaden Sie, um offline zu lesen
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 les
Java source les
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 nal 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 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

Weitere ähnliche Inhalte

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

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
 
Developing Interactive systems - lesson 2
Developing Interactive systems - lesson 2Developing Interactive systems - lesson 2
Developing Interactive systems - lesson 2
SĂłnia
 

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

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

Mehr von 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
 
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
 

Mehr von 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...
 

KĂźrzlich hochgeladen

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

KĂźrzlich hochgeladen (20)

Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 

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
  • 7. Spoon AST Metamodel (excerpt) 6/40
  • 9. Query and analyze model Different ways of doing it: • Using Query API (lter-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 les 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 nal 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 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
  • 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 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