Package spoon.reflect.factory
Class CodeFactory
java.lang.Object
spoon.reflect.factory.SubFactory
spoon.reflect.factory.CodeFactory
This sub-factory contains utility methods to create code elements. To avoid
over-using reflection, consider using
Template
.-
Field Summary
Fields inherited from class spoon.reflect.factory.SubFactory
factory
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescription<A extends Annotation>
CtAnnotation<A>createAnnotation(CtTypeReference<A> annotationType)
Creates an annotation.<T> CtBinaryOperator<T>
createBinaryOperator(CtExpression<?> left, CtExpression<?> right, BinaryOperatorKind kind)
Creates a binary operator.<T> CtCatchVariable<T>
createCatchVariable(CtTypeReference<T> type, String name, ModifierKind... modifierKinds)
Creates a catch variable declaration.<T> CtCatchVariableReference<T>
createCatchVariableReference(CtCatchVariable<T> catchVariable)
Creates a catch variable reference that points to an existing catch variable (strong referencing).<T> CtFieldAccess<Class<T>>
createClassAccess(CtTypeReference<T> type)
Creates a class access expression of the formC.class
.<T> CtCodeSnippetExpression<T>
createCodeSnippetExpression(String expression)
Creates a Code Snippet expression.createCodeSnippetStatement(String statement)
Creates a Code Snippet statement.createComment(String content, CtComment.CommentType type)
Creates a comment<T> CtConstructorCall<T>
createConstructorCall(CtTypeReference<T> type, CtExpression<?>... parameters)
Creates a constructor call.<T extends CtStatement>
CtBlock<?>createCtBlock(T element)
Creates a block.createCtCatch(String nameCatch, Class<? extends Throwable> exception, CtBlock<?> ctBlock)
Creates a catch element.<T> CtField<T>
createCtField(String name, CtTypeReference<T> type, String exp, ModifierKind... visibilities)
Creates a field.createCtPackageReference(Package originalPackage)
Creates a package reference.createCtThrow(String thrownExp)
Creates a throw.<T> CtTypeReference<T>
createCtTypeReference(Class<?> originalClass)
Creates a type reference.createInlineComment(String content)
Creates an inline comment<T> CtInvocation<T>
createInvocation(CtExpression<?> target, CtExecutableReference<T> executable, List<CtExpression<?>> arguments)
Creates an invocation (can be a statement or an expression).<T> CtInvocation<T>
createInvocation(CtExpression<?> target, CtExecutableReference<T> executable, CtExpression<?>... arguments)
Creates an invocation (can be a statement or an expression).createJavaDocTag(String content, CtJavaDocTag.TagType type)
Creates a javadoc tagcreateJavaDocTag(String content, CtJavaDocTag.TagType type, String realName)
Creates a javadoc tag<T> CtLiteral<T>
createLiteral(T value)
Creates a literal with a given value.<T> CtNewArray<T[]>
createLiteralArray(T[] value)
Creates a one-dimension array that must only contain literals.<T> CtLocalVariable<T>
createLocalVariable(CtTypeReference<T> type, String name, CtExpression<T> defaultExpression)
Creates a local variable declaration.<T> CtLocalVariableReference<T>
createLocalVariableReference(CtLocalVariable<T> localVariable)
Creates a local variable reference that points to an existing local variable (strong referencing).<T> CtLocalVariableReference<T>
createLocalVariableReference(CtTypeReference<T> type, String name)
Creates a local variable reference with its name an type (weak referencing).<T> CtNewClass<T>
createNewClass(CtType<T> superClass, CtExpression<?>... parameters)
Creates a new anonymous class.<R> CtStatementList
createStatementList(CtBlock<R> block)
Creates a new statement list from an existing block.createTextBlock(String value)
Creates a TextBlock with the given string value.<T> CtThisAccess<T>
createThisAccess(CtTypeReference<T> type)
Creates an explicit access to athis
variable (of the formtype.this
).<T> CtThisAccess<T>
createThisAccess(CtTypeReference<T> type, boolean isImplicit)
Creates an access to athis
variable (of the formtype.this
).<T> CtTypeAccess<T>
createTypeAccess(CtTypeReference<T> accessedType)
Creates a accessed type.<T> CtTypeAccess<T>
createTypeAccess(CtTypeReference<T> accessedType, boolean isImplicit)
Creates a accessed type.<T> CtTypeAccess<T>
createTypeAccessWithoutCloningReference(CtTypeReference<T> accessedType)
Creates a accessed type, seecreateTypeAccess(CtTypeReference)
for details.<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).<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.<T> CtVariableAccess<T>
createVariableRead(CtVariableReference<T> variable, boolean isStatic)
Creates a variable access for read.List<CtExpression<?>>
createVariableReads(List<? extends CtVariable<?>> variables)
Creates a list of variable accesses for read.<T> CtVariableAccess<T>
createVariableWrite(CtVariableReference<T> variable, boolean isStatic)
Creates a variable access for write.<T extends CtStatement>
CtBlock<?>getOrCreateCtBlock(T element)
Accepts instance of CtStatement or CtBlock.<R extends CtReference, E extends CtNamedElement>
List<R>getReferences(List<E> elements)
Gets a list of references from a list of elements.modifiers(ModifierKind... modifiers)
Creates a modifier set.
-
Constructor Details
-
CodeFactory
Creates aCtCodeElement
sub-factory.
-
-
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 operandright
- the right operandkind
- the operator kind- Returns:
- a binary operator expression
-
createTypeAccess
Creates a accessed type.This method sets a clone of the given
accessedType
object to the accessedType field of the returnedCtTypeAccess
. If the givenaccessedType
is unique and cloning is not needed, usecreateTypeAccessWithoutCloningReference(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
Creates a accessed type.This method sets a clone of the given
accessedType
object to the accessedType field of the returnedCtTypeAccess
. If the givenaccessedType
is unique and cloning is not needed, usecreateTypeAccessWithoutCloningReference(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, seecreateTypeAccess(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
Creates a class access expression of the formC.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 constructorparameters
- the arguments of the constructor call- Returns:
- the constructor call
-
createNewClass
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 expressionexecutable
- the invoked executablearguments
- 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 executablearguments
- the argument list- Returns:
- the new invocation
-
createLiteral
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
Creates a TextBlock with the given string value.- Parameters:
value
- the string value of the literal- Returns:
- a new literal
-
createLiteralArray
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 typename
- the name of the variabledefaultExpression
- 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 typename
- the name of the variablemodifierKinds
- 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
Creates a new statement list from an existing block. -
createThisAccess
Creates an explicit access to athis
variable (of the formtype.this
).- Type Parameters:
T
- the actual type ofthis
- Parameters:
type
- the reference to the type that holds thethis
variable- Returns:
- a
type.this
expression
-
createThisAccess
Creates an access to athis
variable (of the formtype.this
).- Type Parameters:
T
- the actual type ofthis
- Parameters:
type
- the reference to the type that holds thethis
variableisImplicit
- 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
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 variableisStatic
- tells if the assigned variable is static or notexpression
- 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 assignedexpressions
- 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
Creates a block.- Type Parameters:
T
- Subclasses of CtStatement.- Parameters:
element
- Statement of the block.- Returns:
- a block.
-
getOrCreateCtBlock
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
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
Creates a type reference.- Type Parameters:
T
- Type of the reference.- Parameters:
originalClass
- Original class of the reference.- Returns:
- a type reference.
-
createCtPackageReference
Creates a package reference.- Parameters:
originalPackage
- Original package of the reference.- Returns:
- a package reference.
-
createAnnotation
Creates an annotation.- Parameters:
annotationType
- Type of the annotation.- Returns:
- an annotation.
-
getReferences
Gets a list of references from a list of elements.- Type Parameters:
R
- the expected reference typeE
- the element type- Parameters:
elements
- the element list- Returns:
- the corresponding list of references
-
modifiers
Creates a modifier set.- Parameters:
modifiers
- to put in set- Returns:
- Set of given modifiers
-
createCodeSnippetExpression
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
Creates a Code Snippet statement.- Parameters:
statement
- The String containing the statement.- Returns:
- a new CtCodeSnippetStatement
-
createComment
Creates a comment- Parameters:
content
- The content of the commenttype
- The comment type- Returns:
- a new CtComment
-
createInlineComment
Creates an inline comment- Parameters:
content
- The content of the comment- Returns:
- a new CtComment
-
createJavaDocTag
Creates a javadoc tag- Parameters:
content
- The content of the javadoc tag with a possible parametertype
- The tag type- Returns:
- a new CtJavaDocTag
-
createJavaDocTag
Creates a javadoc tag- Parameters:
content
- The content of the javadoc tag with a possible parametertype
- The tag typerealName
- The real name of the tag- Returns:
- a new CtJavaDocTag
-