Class Substitution

java.lang.Object
spoon.template.Substitution

public abstract class Substitution extends Object
This class defines the substitution API for templates (see Template).
  • Method Details

    • insertAll

      public static <T extends Template<?>> void insertAll(CtType<?> targetType, T template)
      Inserts all the methods, fields, constructors, initialization blocks (if target is a class), inner types, and super interfaces (except Template) from a given template by substituting all the template parameters by their values. Members annotated with Local or Parameter are not inserted.
      Parameters:
      targetType - the target type
      template - 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 annotations Template, Local, TemplateParameter or Parameter 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 type
      templateOfType - the model used as source of generation.
      templateParameters - the substitution parameters
    • insertAllSuperInterfaces

      public static void insertAllSuperInterfaces(CtType<?> targetType, Template<?> template)
      Inserts all the super interfaces (except Template) from a given template by substituting all the template parameters by their values.
      Parameters:
      targetType - the target type
      template - the source template
    • insertAllMethods

      public static void insertAllMethods(CtType<?> targetType, Template<?> template)
      Inserts all the methods from a given template by substituting all the template parameters by their values. Members annotated with Local or Parameter are not inserted.
      Parameters:
      targetType - the target type
      template - the source template
    • insertAllFields

      public static void insertAllFields(CtType<?> targetType, Template<?> template)
      Inserts all the fields from a given template by substituting all the template parameters by their values. Members annotated with Local or Parameter are not inserted.
      Parameters:
      targetType - the target type
      template - the source template
    • insertAllNestedTypes

      public 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. Members annotated with Local are not inserted.
      Parameters:
      targetType - the target type
      template - the source template
    • insertAllConstructors

      public 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. Members annotated with Local or Parameter are not inserted.
      Parameters:
      targetType - the target type
      template - 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 constructor
      template - the template instance that holds the source template method and that defines the parameter values
      sourceMethod - 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 method
      template - the template instance that holds the source template method and that defines the parameter values
      sourceMethod - 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 constructor
      template - the template instance that holds the source template constructor and that defines the parameter values
      sourceConstructor - 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 class
      template - the template that holds the executable
      executableName - the source executable template
      parameterTypes - 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 class
      template - the template that holds the executable
      statementIndex - the statement index in the executable's body
      executableName - the source executable template
      parameterTypes - 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 type
      template - the template that holds the field
      fieldName - 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 type
      template - the template instance
      code - the code
      Returns:
      the code where all the template parameters has been substituted by their values
    • substitute

      public static <T extends CtType<?>> T substitute(Template<?> template, T templateType)
      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 inserted
      template - the template that defines the source template field
      sourceField - 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

      public static <T> CtClass<T> getTemplateCtClass(Factory factory, Template<?> template)
      Parameters:
      factory - - the factory, which contains the model of the template
      template - - java instance of the template
      Returns:
      - CtClass from the already built spoon model, which represents the template