Class CodeFactory

java.lang.Object
spoon.reflect.factory.SubFactory
spoon.reflect.factory.CodeFactory

public class CodeFactory extends SubFactory
This sub-factory contains utility methods to create code elements. To avoid over-using reflection, consider using Template.
  • Constructor Details

  • Method Details

    • createBinaryOperator

      public <T> CtBinaryOperator<T> createBinaryOperator(CtExpression<?> left, CtExpression<?> right, BinaryOperatorKind kind)
      Creates a binary operator.
      Type Parameters:
      T - the type of the expression
      Parameters:
      left - the left operand
      right - the right operand
      kind - the operator kind
      Returns:
      a binary operator expression
    • createTypeAccess

      public <T> CtTypeAccess<T> createTypeAccess(CtTypeReference<T> accessedType)
      Creates a accessed type.

      This method sets a clone of the given accessedType object to the accessedType field of the returned CtTypeAccess. If the given accessedType is unique and cloning is not needed, use createTypeAccessWithoutCloningReference(CtTypeReference) instead of this method.

      Type Parameters:
      T - the type of the expression.
      Parameters:
      accessedType - a type reference to the accessed type.
      Returns:
      a accessed type expression.
    • createTypeAccess

      public <T> CtTypeAccess<T> createTypeAccess(CtTypeReference<T> accessedType, boolean isImplicit)
      Creates a accessed type.

      This method sets a clone of the given accessedType object to the accessedType field of the returned CtTypeAccess. If the given accessedType is unique and cloning is not needed, use createTypeAccessWithoutCloningReference(CtTypeReference) instead of this method.

      Type Parameters:
      T - the type of the expression.
      Parameters:
      accessedType - a type reference to the accessed type.
      isImplicit - type of the type access is implicit or not.
      Returns:
      a accessed type expression.
    • createTypeAccessWithoutCloningReference

      public <T> CtTypeAccess<T> createTypeAccessWithoutCloningReference(CtTypeReference<T> accessedType)
      Creates a accessed type, see createTypeAccess(CtTypeReference) for details.
      Type Parameters:
      T - the type of the expression.
      Parameters:
      accessedType - a type reference to the accessed type.
      Returns:
      a accessed type expression.
    • createClassAccess

      public <T> CtFieldAccess<Class<T>> createClassAccess(CtTypeReference<T> type)
      Creates a class access expression of the form C.class.
      Type Parameters:
      T - the actual type of the accessed class if available
      Parameters:
      type - a type reference to the accessed class
      Returns:
      the class access expression.
    • createConstructorCall

      public <T> CtConstructorCall<T> createConstructorCall(CtTypeReference<T> type, CtExpression<?>... parameters)
      Creates a constructor call. The correct constructor is inferred based on parameters
      Type Parameters:
      T - the actual type of the decelerating type of the constructor if available
      Parameters:
      type - the decelerating type of the constructor
      parameters - the arguments of the constructor call
      Returns:
      the constructor call
    • createNewClass

      public <T> CtNewClass<T> createNewClass(CtType<T> superClass, CtExpression<?>... parameters)
      Creates a new anonymous class.
    • createInvocation

      public <T> CtInvocation<T> createInvocation(CtExpression<?> target, CtExecutableReference<T> executable, CtExpression<?>... arguments)
      Creates an invocation (can be a statement or an expression).
      Type Parameters:
      T - the return type of the invoked method
      Parameters:
      target - the target expression
      executable - the invoked executable
      arguments - the argument list
      Returns:
      the new invocation
    • createInvocation

      public <T> CtInvocation<T> createInvocation(CtExpression<?> target, CtExecutableReference<T> executable, List<CtExpression<?>> arguments)
      Creates an invocation (can be a statement or an expression).
      Type Parameters:
      T - the return type of the invoked method
      Parameters:
      target - the target expression (may be null for static methods)
      executable - the invoked executable
      arguments - the argument list
      Returns:
      the new invocation
    • createLiteral

      public <T> CtLiteral<T> createLiteral(T value)
      Creates a literal with a given value.
      Type Parameters:
      T - the type of the literal
      Parameters:
      value - the value of the literal
      Returns:
      a new literal
    • createTextBlock

      public CtTextBlock createTextBlock(String value)
      Creates a TextBlock with the given string value.
      Parameters:
      value - the string value of the literal
      Returns:
      a new literal
    • createLiteralArray

      public <T> CtNewArray<T[]> createLiteralArray(T[] value)
      Creates a one-dimension array that must only contain literals.
    • createLocalVariable

      public <T> CtLocalVariable<T> createLocalVariable(CtTypeReference<T> type, String name, CtExpression<T> defaultExpression)
      Creates a local variable declaration.
      Type Parameters:
      T - the local variable type
      Parameters:
      type - the reference to the type
      name - the name of the variable
      defaultExpression - the assigned default expression
      Returns:
      a new local variable declaration
    • createLocalVariableReference

      public <T> CtLocalVariableReference<T> createLocalVariableReference(CtLocalVariable<T> localVariable)
      Creates a local variable reference that points to an existing local variable (strong referencing).
    • createLocalVariableReference

      public <T> CtLocalVariableReference<T> createLocalVariableReference(CtTypeReference<T> type, String name)
      Creates a local variable reference with its name an type (weak referencing).
    • createCatchVariable

      public <T> CtCatchVariable<T> createCatchVariable(CtTypeReference<T> type, String name, ModifierKind... modifierKinds)
      Creates a catch variable declaration.
      Type Parameters:
      T - the catch variable type
      Parameters:
      type - the reference to the type
      name - the name of the variable
      modifierKinds - Modifiers of the catch variable
      Returns:
      a new catch variable declaration
    • createCatchVariableReference

      public <T> CtCatchVariableReference<T> createCatchVariableReference(CtCatchVariable<T> catchVariable)
      Creates a catch variable reference that points to an existing catch variable (strong referencing).
    • createStatementList

      public <R> CtStatementList createStatementList(CtBlock<R> block)
      Creates a new statement list from an existing block.
    • createThisAccess

      public <T> CtThisAccess<T> createThisAccess(CtTypeReference<T> type)
      Creates an explicit access to a this variable (of the form type.this).
      Type Parameters:
      T - the actual type of this
      Parameters:
      type - the reference to the type that holds the this variable
      Returns:
      a type.this expression
    • createThisAccess

      public <T> CtThisAccess<T> createThisAccess(CtTypeReference<T> type, boolean isImplicit)
      Creates an access to a this variable (of the form type.this).
      Type Parameters:
      T - the actual type of this
      Parameters:
      type - the reference to the type that holds the this variable
      isImplicit - type of the this access is implicit or not.
      Returns:
      a type.this expression
    • createVariableRead

      public <T> CtVariableAccess<T> createVariableRead(CtVariableReference<T> variable, boolean isStatic)
      Creates a variable access for read.
    • createVariableReads

      public List<CtExpression<?>> createVariableReads(List<? extends CtVariable<?>> variables)
      Creates a list of variable accesses for read.
      Parameters:
      variables - the variables to be accessed
    • createVariableWrite

      public <T> CtVariableAccess<T> createVariableWrite(CtVariableReference<T> variable, boolean isStatic)
      Creates a variable access for write.
    • createVariableAssignment

      public <A,​ T extends A> CtAssignment<A,​T> createVariableAssignment(CtVariableReference<A> variable, boolean isStatic, CtExpression<T> expression)
      Creates a variable assignment (can be an expression or a statement).
      Type Parameters:
      T - the type of the assigned variable
      Parameters:
      variable - a reference to the assigned variable
      isStatic - tells if the assigned variable is static or not
      expression - the assigned expression
      Returns:
      a variable assignment
    • createVariableAssignments

      public <T> CtStatementList createVariableAssignments(List<? extends CtVariable<T>> variables, List<? extends CtExpression<T>> expressions)
      Creates a list of statements that contains the assignments of a set of variables.
      Parameters:
      variables - the variables to be assigned
      expressions - the assigned expressions
      Returns:
      a list of variable assignments
    • createCtField

      public <T> CtField<T> createCtField(String name, CtTypeReference<T> type, String exp, ModifierKind... visibilities)
      Creates a field.
      Type Parameters:
      T - Generic type for the type of the field.
      Parameters:
      name - Name of the field.
      type - Type of the field.
      exp - Default expression of the field.
      visibilities - All visibilities of the field.
      Returns:
      a field
    • createCtBlock

      public <T extends CtStatement> CtBlock<?> createCtBlock(T element)
      Creates a block.
      Type Parameters:
      T - Subclasses of CtStatement.
      Parameters:
      element - Statement of the block.
      Returns:
      a block.
    • getOrCreateCtBlock

      public <T extends CtStatement> CtBlock<?> getOrCreateCtBlock(T element)
      Accepts instance of CtStatement or CtBlock. If element is CtStatement, then it creates wrapping CtBlock, which contains the element If element is CtBlock, then it directly returns that element If element is null, then it returns null. note: It must not create empty CtBlock - as expected in CtCatch, CtExecutable, CtLoop and CtTry setBody implementations
      Parameters:
      element -
      Returns:
      CtBlock instance
    • createCtThrow

      public CtThrow createCtThrow(String thrownExp)
      Creates a throw.
      Parameters:
      thrownExp - Expression of the throw.
      Returns:
      a throw.
    • createCtCatch

      public CtCatch createCtCatch(String nameCatch, Class<? extends Throwable> exception, CtBlock<?> ctBlock)
      Creates a catch element.
      Parameters:
      nameCatch - Name of the variable in the catch.
      exception - Type of the exception.
      ctBlock - Content of the catch.
      Returns:
      a catch.
    • createCtTypeReference

      public <T> CtTypeReference<T> createCtTypeReference(Class<?> originalClass)
      Creates a type reference.
      Type Parameters:
      T - Type of the reference.
      Parameters:
      originalClass - Original class of the reference.
      Returns:
      a type reference.
    • createCtPackageReference

      public CtPackageReference createCtPackageReference(Package originalPackage)
      Creates a package reference.
      Parameters:
      originalPackage - Original package of the reference.
      Returns:
      a package reference.
    • createAnnotation

      public <A extends Annotation> CtAnnotation<A> createAnnotation(CtTypeReference<A> annotationType)
      Creates an annotation.
      Parameters:
      annotationType - Type of the annotation.
      Returns:
      an annotation.
    • getReferences

      public <R extends CtReference,​ E extends CtNamedElement> List<R> getReferences(List<E> elements)
      Gets a list of references from a list of elements.
      Type Parameters:
      R - the expected reference type
      E - the element type
      Parameters:
      elements - the element list
      Returns:
      the corresponding list of references
    • modifiers

      public Set<ModifierKind> modifiers(ModifierKind... modifiers)
      Creates a modifier set.
      Parameters:
      modifiers - to put in set
      Returns:
      Set of given modifiers
    • createCodeSnippetExpression

      public <T> CtCodeSnippetExpression<T> createCodeSnippetExpression(String expression)
      Creates a Code Snippet expression.
      Type Parameters:
      T - The type of the expression represented by the CodeSnippet
      Parameters:
      expression - The string that contains the expression.
      Returns:
      a new CtCodeSnippetExpression.
    • createCodeSnippetStatement

      public CtCodeSnippetStatement createCodeSnippetStatement(String statement)
      Creates a Code Snippet statement.
      Parameters:
      statement - The String containing the statement.
      Returns:
      a new CtCodeSnippetStatement
    • createComment

      public CtComment createComment(String content, CtComment.CommentType type)
      Creates a comment
      Parameters:
      content - The content of the comment
      type - The comment type
      Returns:
      a new CtComment
    • createInlineComment

      public CtComment createInlineComment(String content)
      Creates an inline comment
      Parameters:
      content - The content of the comment
      Returns:
      a new CtComment
    • createJavaDocTag

      public CtJavaDocTag createJavaDocTag(String content, CtJavaDocTag.TagType type)
      Creates a javadoc tag
      Parameters:
      content - The content of the javadoc tag with a possible parameter
      type - The tag type
      Returns:
      a new CtJavaDocTag
    • createJavaDocTag

      public CtJavaDocTag createJavaDocTag(String content, CtJavaDocTag.TagType type, String realName)
      Creates a javadoc tag
      Parameters:
      content - The content of the javadoc tag with a possible parameter
      type - The tag type
      realName - The real name of the tag
      Returns:
      a new CtJavaDocTag