News

Star Spoon on Github: https://github.com/INRIA/spoon :-)

  • August 8, 2014: Spoon 2.1 is in beta! (fixes minor bugs)
  • April 2, 2014: Spoon 2.0 is released!
  • March 2014: Spoon 2.0 is in beta.
  • 30-09-2013: Spoon 1.6 is released.
  • 12-04-2012: Spoon 1.5 is released.

Spoon Presentation

Spoon enables you to transform (see below) and analyze Java source code (see example) . Spoon provides a complete and fine-grained Java metamodel where any program element (classes, methods, fields, statements, expressions...) can be accessed both for reading and modification. Spoon takes as input source code and produces transformed source code ready to be compiled.

Technical presentation: http://spoon.gforge.inria.fr/source-code-analysis-transformation-spoon.pdf

Slides: http://www.monperrus.net/martin/lecture-slides-source-code-analysis-and-transformation.pdf

Spoon is Free and Open Source (CeCILL-C license - French equivalent to LGPL).

Getting started / Tutorial

To automatically insert a bound check in the following code:

public void openUserSpacePort(@Bound(min = 1025) int a) {
  // code to open a port
}

The following Spoon code has to be written:

public @interface Bound {
  double min(); 
}

// we only process method parameters (CtParameter) annotated with @Bound 
public class Bound2Processor extends
    AbstractAnnotationProcessor<Bound, CtParameter<?>> {
  public void process(Bound annotation, CtParameter<?> element) {
    // we declare a new snippet of code to be inserted
    CtCodeSnippetStatement snippet = getFactory().Core().createCodeSnippetStatement();

    // this snippet contains an if check
    snippet.setValue("if(" + element.getSimpleName() + " < "
        + annotation.min()
        + ") throw new RuntimeException(\"[Spoon check] Bound violation\");");

    // we insert the snippet at the beginning of the method boby
    element.getParent(CtMethod.class).getBody().insertBegin(snippet);
  }
}

and launched as follows (the main class is spoon.Launcher): $ java -jar spoon-core-2.0-jar-with-dependencies.jar -i sourceFolder -p Bound2Processor

Spoon processes all source files of sourceFolder and writes to the resulting code to the folder "spooned". For our example:

public void openUserSpacePort(int a) {
  if(a < 1025) throw new RuntimeException("[Spoon check] Bound violation");
  // code to open a port
}

Downloads

Spoon (standalone), download, javadoc

Spoon with Maven

The latest snapshot version:

 
	<dependencies>
		<dependency>
			<groupId>fr.inria.gforge.spoon</groupId>
			<artifactId>spoon-core</artifactId>
			<version>3.0-SNAPSHOT</version>
		</dependency>
	</dependencies>
	<repositories>
		<repository>
			<id>gforge.inria.fr-snapshot</id>
			<name>Maven Repository for Spoon Snapshot</name>
			<url>http://spoon.gforge.inria.fr/repositories/snapshots/</url>
			<snapshots />
		</repository>
	</repositories>

The stable version:

 
	<dependencies>
		<dependency>
			<groupId>fr.inria.gforge.spoon</groupId>
			<artifactId>spoon-core</artifactId>
			<version>2.1</version>
		</dependency>
	</dependencies>
	<repositories>
		<repository>
			<id>gforge.inria.fr-release</id>
			<name>Maven Repository for Spoon</name>
			<url>http://spoon.gforge.inria.fr/repositories/releases/</url>
			<releases />
		</repository>
	</repositories>