This maker interface should be implemented by the classes containing some
template code, so that they can be used as template parameter values holder
for the
Substitution methods.
A template code is simply a piece of code that uses a
TemplateParameter 's instance. It must then invoke the
TemplateParameter.S method.
When the template parameter is a String or a primitive type (or a boxing
type) representing a literal, or a Class, the template parameter can be
directly accessed.
import spoon.template.Template;
import spoon.template.Value;
public class SimpleTemplate implements Template {
// template parameter fields
\@Parameter String _parameter_;
// parameters binding
\@Local
public SimpleTemplate(String parameter) {
_parameter_ = parameter;
}
// template method
public void simpleTemplateMethod() {
System.out.println(_parameter_);
}
}
The template parameters must be bound to their values in the template's
constructor (which should be defined as a template's
spoon.template.Local . A possible use of a template would be to
insert the template into a target class, by using
Substitution.insertAll(CtTypeTemplate) :
spoon.reflect.CtClass target=...;
Template template=new SimpleTemplate("hello templated world");
Substitution.insertAll(target,template);
If the target class is an empty class named A , the resulting
code will be:
public class A {
public void insertedMethod() {
System.out.println("hello templated world");
}
}
|