Series on program transformation systems: Spoon (2005)

This is the next entry in the series on program transformation systems. This article describes Spoon, a system for Java source code. It has been in development since 2005 and is actively being developed to this day.

One part of Spoon is an API to modify the abstract syntax tree of a Java program. For example, the API has methods to add or delete classes, methods, methods, and statements. An example shown on Spoon’s website checks for empty catch blocks using an API for visiting tree nodes. As noted, “[t]he concept of a CtScanner is a “visitor pattern”, which can be applied to processing of Abstract [S]yntax [T]ree.”

The second part of Spoon uses Java Annotations. An example they give shows how to insert a check into the code for a null parameter check. They note that “[t]he implementation of such an annotation would not be straightforward using Java’s processing API since it would not allow us to just insert the NULL check in the body of the annotated method.” It isn’t clear to me what API they are referring to, or why Spoon’s API for modifying an AST could not be used to insert a null check.

Observations and notes

  • Spoon is a compiler system that parses Java source code, computes ASTs, provides for program transformation on that AST using an AST visitor API. But, as noted with C-Breeze, there is no pattern matching/rewrite language for ASTs. So, it is very low level.

References

Pawlak, R., Monperrus, M., Petitprez, N., Noguera, C. and Seinturier, L., 2016. Spoon: A library for implementing analyses and transformations of java source code. Software: Practice and Experience46(9), pp.1155-1179.

Additional Reading

http://spoon.gforge.inria.fr/

Posted in Tip