Interface Template<T extends CtElement>
- All Known Implementing Classes:
AbstractTemplate
,BlockTemplate
,ExpressionTemplate
,ExtensionTemplate
,StatementTemplate
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 it is used to rename element of the code such as fields or methods. When it is another primitive type (or a boxing type) representing a literal, or a Class, the template parameter can be directly accessed. To use a standard parameter containing a String type, use a CtLiteral<String>
import spoon.template.Template; import spoon.template.Value; public class SimpleTemplate implements Template { // template parameter fields \@Parameter String _parameter_; \@Parameter CtLiteral<String> _anotherParameter; // parameters binding \@Local public SimpleTemplate(String parameter, CtLiteral<String> anotherParameter) { _parameter_ = parameter; _anotherParameter = anotherParameter; } // template method public void methodwith_parameter_() { System.out.println(_anotherParameter); } }
The template parameters must be bound to their values in the template's
constructor (which should be defined as a template's
Local
. A possible use of a template would be to
insert the template into a target class, by using
Substitution.insertAll(CtType, Template)
:
spoon.reflect.CtClass target=...; CtLiteral<String> anotherParameter = factory.createLiteral(); anotherParameter.setValue("hello templated world"); Template template=new SimpleTemplate("ParameterizedName", anotherParameter); Substitution.insertAll(target,template);
If the target class is an empty class named A
, the resulting
code will be:
public class A { public void methodwithParameterizedName() { System.out.println("hello templated world"); } }
-
Method Summary
Modifier and TypeMethodDescriptionReturns the code which results from applying the template.boolean
if true, the result of the template evaluation is simplified with partial evaluation
-
Method Details
-
apply
Returns the code which results from applying the template.- Parameters:
targetType
- the type that defines the context of the substitution. It may be null for templates with no context.
-
withPartialEvaluation
boolean withPartialEvaluation()if true, the result of the template evaluation is simplified with partial evaluation
-