Package spoon.template
Class Substitution
java.lang.Object
spoon.template.Substitution
This class defines the substitution API for templates (see
Template
).-
Method Summary
Modifier and TypeMethodDescriptionstatic <T extends CtType<?>>
TcreateTypeFromTemplate(String qualifiedTypeName, CtType<?> templateOfType, Map<String,Object> templateParameters)
Generates a type (class, interface, enum, ...) from the template model `templateOfType` by by substituting all the template parameters by their values.static <T> CtClass<T>
getTemplateCtClass(Factory factory, Template<?> template)
static <T extends Template<?>>
voidInserts all the methods, fields, constructors, initialization blocks (if target is a class), inner types, and super interfaces (exceptTemplate
) from a given template by substituting all the template parameters by their values.static void
insertAllConstructors(CtType<?> targetType, Template<?> template)
Inserts all constructors and initialization blocks from a given template by substituting all the template parameters by their values.static void
insertAllFields(CtType<?> targetType, Template<?> template)
Inserts all the fields from a given template by substituting all the template parameters by their values.static void
insertAllMethods(CtType<?> targetType, Template<?> template)
Inserts all the methods from a given template by substituting all the template parameters by their values.static void
insertAllNestedTypes(CtType<?> targetType, Template<?> template)
Inserts all the nested types from a given template by substituting all the template parameters by their values.static void
insertAllSuperInterfaces(CtType<?> targetType, Template<?> template)
Inserts all the super interfaces (exceptTemplate
) from a given template by substituting all the template parameters by their values.static <T> CtConstructor<T>
insertConstructor(CtClass<T> targetClass, Template<?> template, CtConstructor<?> sourceConstructor)
Generates a constructor from a template constructor by substituting all the template parameters by their values.static <T> CtConstructor<T>
insertConstructor(CtClass<T> targetClass, Template<?> template, CtMethod<?> sourceMethod)
Generates a constructor from a template method by substituting all the template parameters by their values.static <T> CtField<T>
insertField(CtType<?> targetType, Template<?> template, CtField<T> sourceField)
Generates a field (and its initialization expression) from a template field by substituting all the template parameters by their values.static <T> CtMethod<T>
insertMethod(CtType<?> targetType, Template<?> template, CtMethod<T> sourceMethod)
Generates a method from a template method by substituting all the template parameters by their values.static void
redirectTypeReferences(CtElement element, CtTypeReference<?> source, CtTypeReference<?> target)
A helper method that recursively redirects all the type references from a source type to a target type in the given element.static <E extends CtElement>
Esubstitute(CtType<?> targetType, Template<?> template, E code)
Substitutes all the template parameters in a random piece of code.static <T extends CtType<?>>
Tsubstitute(Template<?> template, T templateType)
Substitutes all the template parameters in a given template type and returns the resulting type.static CtExpression<?>
substituteFieldDefaultExpression(CtType<?> targetType, Template<?> template, String fieldName)
Gets a default expression from a template field with all the template parameters substituted.static CtBlock<?>
substituteMethodBody(CtClass<?> targetClass, Template<?> template, String executableName, CtTypeReference<?>... parameterTypes)
Gets a body from a template executable with all the template parameters substituted.static CtStatement
substituteStatement(CtClass<?> targetClass, Template<?> template, int statementIndex, String executableName, CtTypeReference<?>... parameterTypes)
Gets a statement from a template executable with all the template parameters substituted.
-
Method Details
-
insertAll
Inserts all the methods, fields, constructors, initialization blocks (if target is a class), inner types, and super interfaces (exceptTemplate
) from a given template by substituting all the template parameters by their values. Members annotated withLocal
orParameter
are not inserted.- Parameters:
targetType
- the target typetemplate
- the source template
-
createTypeFromTemplate
public static <T extends CtType<?>> T createTypeFromTemplate(String qualifiedTypeName, CtType<?> templateOfType, Map<String,Object> templateParameters)Generates a type (class, interface, enum, ...) from the template model `templateOfType` by by substituting all the template parameters by their values. Inserts all the methods, fields, constructors, initialization blocks (if target is a class), inner types, super class and super interfaces. Note! This algorithm does NOT handle interfaces or annotationsTemplate
,Local
,TemplateParameter
orParameter
in a special way, it means they all will be added to the generated type too. If you do not want to add them then clone your templateOfType and remove these nodes from that model before.- Parameters:
qualifiedTypeName
- the qualified name of the new typetemplateOfType
- the model used as source of generation.templateParameters
- the substitution parameters
-
insertAllSuperInterfaces
Inserts all the super interfaces (exceptTemplate
) from a given template by substituting all the template parameters by their values.- Parameters:
targetType
- the target typetemplate
- the source template
-
insertAllMethods
Inserts all the methods from a given template by substituting all the template parameters by their values. Members annotated withLocal
orParameter
are not inserted.- Parameters:
targetType
- the target typetemplate
- the source template
-
insertAllFields
Inserts all the fields from a given template by substituting all the template parameters by their values. Members annotated withLocal
orParameter
are not inserted.- Parameters:
targetType
- the target typetemplate
- the source template
-
insertAllNestedTypes
Inserts all the nested types from a given template by substituting all the template parameters by their values. Members annotated withLocal
are not inserted.- Parameters:
targetType
- the target typetemplate
- the source template
-
insertAllConstructors
Inserts all constructors and initialization blocks from a given template by substituting all the template parameters by their values. Members annotated withLocal
orParameter
are not inserted.- Parameters:
targetType
- the target typetemplate
- the source template
-
insertConstructor
public static <T> CtConstructor<T> insertConstructor(CtClass<T> targetClass, Template<?> template, CtMethod<?> sourceMethod)Generates a constructor from a template method by substituting all the template parameters by their values.- Parameters:
targetClass
- the target class where to insert the generated constructortemplate
- the template instance that holds the source template method and that defines the parameter valuessourceMethod
- the source template method- Returns:
- the generated method
-
insertMethod
public static <T> CtMethod<T> insertMethod(CtType<?> targetType, Template<?> template, CtMethod<T> sourceMethod)Generates a method from a template method by substituting all the template parameters by their values.- Parameters:
targetType
- the target type where to insert the generated methodtemplate
- the template instance that holds the source template method and that defines the parameter valuessourceMethod
- the source template method- Returns:
- the generated method
-
insertConstructor
public static <T> CtConstructor<T> insertConstructor(CtClass<T> targetClass, Template<?> template, CtConstructor<?> sourceConstructor)Generates a constructor from a template constructor by substituting all the template parameters by their values.- Parameters:
targetClass
- the target class where to insert the generated constructortemplate
- the template instance that holds the source template constructor and that defines the parameter valuessourceConstructor
- the source template constructor- Returns:
- the generated constructor
-
substituteMethodBody
public static CtBlock<?> substituteMethodBody(CtClass<?> targetClass, Template<?> template, String executableName, CtTypeReference<?>... parameterTypes)Gets a body from a template executable with all the template parameters substituted.- Parameters:
targetClass
- the target classtemplate
- the template that holds the executableexecutableName
- the source executable templateparameterTypes
- the parameter types of the source executable- Returns:
- the body expression of the source executable template with all the template parameters substituted
-
substituteStatement
public static CtStatement substituteStatement(CtClass<?> targetClass, Template<?> template, int statementIndex, String executableName, CtTypeReference<?>... parameterTypes)Gets a statement from a template executable with all the template parameters substituted.- Parameters:
targetClass
- the target classtemplate
- the template that holds the executablestatementIndex
- the statement index in the executable's bodyexecutableName
- the source executable templateparameterTypes
- the parameter types of the source executable- Returns:
- the body expression of the source executable template with all the template parameters substituted
-
substituteFieldDefaultExpression
public static CtExpression<?> substituteFieldDefaultExpression(CtType<?> targetType, Template<?> template, String fieldName)Gets a default expression from a template field with all the template parameters substituted.- Parameters:
targetType
- the target typetemplate
- the template that holds the fieldfieldName
- the template source field- Returns:
- the expression of the template source field with all the template parameters substituted
-
substitute
public static <E extends CtElement> E substitute(CtType<?> targetType, Template<?> template, E code)Substitutes all the template parameters in a random piece of code.- Parameters:
targetType
- the target typetemplate
- the template instancecode
- the code- Returns:
- the code where all the template parameters has been substituted by their values
-
substitute
Substitutes all the template parameters in a given template type and returns the resulting type.- Parameters:
template
- the template instance (holds the parameter values)templateType
- the template type- Returns:
- a copy of the template type where all the parameters has been substituted
-
insertField
public static <T> CtField<T> insertField(CtType<?> targetType, Template<?> template, CtField<T> sourceField)Generates a field (and its initialization expression) from a template field by substituting all the template parameters by their values.- Type Parameters:
T
- the type of the field- Parameters:
targetType
- the target type where the field is insertedtemplate
- the template that defines the source template fieldsourceField
- the source template field- Returns:
- the inserted field
-
redirectTypeReferences
public static void redirectTypeReferences(CtElement element, CtTypeReference<?> source, CtTypeReference<?> target)A helper method that recursively redirects all the type references from a source type to a target type in the given element. -
getTemplateCtClass
- Parameters:
factory
- - the factory, which contains the model of the templatetemplate
- - java instance of the template- Returns:
- - CtClass from the already built spoon model, which represents the template
-