Academic-owned Java library transforms, rewrites, analyzes, and transpiles code
Today’s spotlight lands on Spoon: a Java library that allows you to transform and analyze Java code. This library is as brainy and academic as it is powerful. Give it a stir!
There’s no shortage of helpful open source Java libraries! We love diving deep and today we looking at at Spoon. Created by Inria, the French National Institute for computer science and applied mathematics, Spoon allows you to transform and analyze Java code.
Have you used this academic library yet? Come and get your check-out card.
The library is open (source)
Spoon’s lengthy history passed it through several hands. GitHub user tdurieux commented on November 3, 2018 about Spoon’s creation. “The Spoon project started at the beginning of 2007 with Renaud Pawlak as a tool for research…The Spoon architecture did not massively change since that period.” After this fact, it was paid for by Inria. However, as tdurieux goes on to explain, now Spoon is maintained by the open source community: “half academic and half industry”. (You can read more about the timeline here.)
So, what does Spoon do?
Spoon is a library to analyze, transform, rewrite, transpile Java source code (incl Java 9 & Java 10). It parses source files to build a well-designed AST with powerful analysis and transformation API. Made at Inria with ❤️, 🍻 and ✨. http://spoon.gforge.inria.fr/
According to Spoon’s design philosophy, it stays as true as possible to the original Java language concepts.
Powerful, yet easy transformations
SEE ALSO: How to fix a VirtualMachineError in Java
The latest version, Spoon 7.1.0 released on October 10, 2018. With the latest update, a host of new features, improved documentation, and bug fixes (what’s an update without a few?) came to the table. This includes updated performance issues, added support for javadoc inline tags, and added encoding detection callback.
View an example and learn how to make your first transformation!
For academic usage, you must cite “Spoon: A Library for Implementing Analyses and Transformations of Java Source Code” in your work. (Of course, for those interested in this library and computer science this paper is a treasure trove to explore! We highly recommend giving it at least a good browse.)
Just a few features worth stirring around:
- Queries: Added in Spoon 5.5, queries can be chained, and reused
- Filters: A Filter defines a predicate of the form of a
matchesmethod that returns
trueif an element has to be selected in the filtering operation.
- Matching elements
- Patterns: Create a Spoon pattern for finding elements with PatternBuilder
Compiling Spoon requires the JDK and Maven.
- Command line and jar file
- Project file list
- Latest version for Maven
- Gradle README and documentation
- All about testing transformations
Thankfully, the community for Spoon is fairly large and active. Since it has been around for so long, experts aplenty have advice and know-how. (Not to mention that some of the top minds helped create and support it!) Pull requests welcome!
- The [inria-spoon] tag on StackOverflow provides plenty of answers
- Simon Urli discusses getting started with Spoon at OW2Con’18 in Paris
- Benjamin Danglot presents how to generate test assertions with Spoon at OW2Con’17 in Paris
- Examples on how to use the library on GitHub
- SpoonLabs: peruse through experiments in Spoon
- What projects use Spoon? View the ecosystem and find inspiration from other successful projects.