We provide examples for learning and teaching Spoon in https://github.com/SpoonLabs/spoon-examples/. Don't hesitate to propose new examples as pull-request!

The HelloWorldProcessor example prints hello world with compile-time reflection.

Program Analysis

The CatchProcessor example detects empty catch blocks.

The ReferenceProcessor example detects circular references between packages.

This Factory example example detects wrong uses of the factory pattern.

Program Transformation

Transformation with API

The NotNullProcessor example adds a not-null check for all method parameters.

The LogProcessor example is an example of Spoon for tracing, it adds a log statement when entering a method.

The MutationProcessor example randomly mutates some parts of the abstract syntax tree for mutation testing.

Transformation with Annotations

The Bound example adds runtime checks based on annotations

The Database access example shows how to use annotation processing to add persistence into a POJO, ie. to implement a simple URL with Spoon (also uses templates).

The Nton example introduces a Nton design pattern (extension of singleton but for N instances) into a target class. It inserts static fields, methods, and initializer code into constructors.

The Visitor example implements a visitor pattern by automatically introducing an accept method in a visited type hierarchy.

The Field Access example implements a refactoring that introduces setters and getters for the fields annotated with the Access annotation and that replaces all the direct accesses to these fields by calls to its new getters and setters.

Transformation with Templates

The RetryTemplate example creates retriable methods in case of exceptions if annotated by @RetryOnFailure.

Transformation with Patterns

TBD, see https://github.com/INRIA/spoon/issues/3140

Miscelanous

The Distributed Calculus example creates a fun new language for distributed computing using Java and Spoon.