Package spoon.reflect.declaration
Interface CtElement
-
- All Superinterfaces:
Cloneable
,CtQueryable
,CtVisitable
,FactoryAccessor
,Serializable
,SourcePositionHolder
- All Known Subinterfaces:
CompilationUnit
,CtAbstractInvocation<T>
,CtAbstractSwitch<S>
,CtAnnotation<A>
,CtAnnotationFieldAccess<T>
,CtAnnotationMethod<T>
,CtAnnotationType<T>
,CtAnonymousExecutable
,CtArrayAccess<T,E>
,CtArrayRead<T>
,CtArrayTypeReference<T>
,CtArrayWrite<T>
,CtAssert<T>
,CtAssignment<T,A>
,CtBinaryOperator<T>
,CtBlock<R>
,CtBodyHolder
,CtBreak
,CtCase<S>
,CtCatch
,CtCatchVariable<T>
,CtCatchVariableReference<T>
,CtCFlowBreak
,CtClass<T>
,CtCodeElement
,CtCodeSnippetExpression<T>
,CtCodeSnippetStatement
,CtComment
,CtCompilationUnit
,CtConditional<T>
,CtConstructor<T>
,CtConstructorCall<T>
,CtContinue
,CtDo
,CtEnum<T>
,CtEnumValue<T>
,CtExecutable<R>
,CtExecutableReference<T>
,CtExecutableReferenceExpression<T,E>
,CtExpression<T>
,CtField<T>
,CtFieldAccess<T>
,CtFieldRead<T>
,CtFieldReference<T>
,CtFieldWrite<T>
,CtFor
,CtForEach
,CtFormalTypeDeclarer
,CtIf
,CtImport
,CtInterface<T>
,CtIntersectionTypeReference<T>
,CtInvocation<T>
,CtJavaDoc
,CtJavaDocTag
,CtLabelledFlowBreak
,CtLambda<T>
,CtLiteral<T>
,CtLocalVariable<T>
,CtLocalVariableReference<T>
,CtLoop
,CtMethod<T>
,CtModifiable
,CtModule
,CtModuleDirective
,CtModuleReference
,CtModuleRequirement
,CtMultiTypedElement
,CtNamedElement
,CtNewArray<T>
,CtNewClass<T>
,CtOperatorAssignment<T,A>
,CtPackage
,CtPackageDeclaration
,CtPackageExport
,CtPackageReference
,CtParameter<T>
,CtParameterReference<T>
,CtPattern
,CtProvidedService
,CtRecord
,CtRecordComponent
,CtReference
,CtResource<T>
,CtReturn<R>
,CtStatement
,CtStatementList
,CtSuperAccess<T>
,CtSwitch<S>
,CtSwitchExpression<T,S>
,CtSynchronized
,CtTargetedExpression<T,E>
,CtTextBlock
,CtThisAccess<T>
,CtThrow
,CtTry
,CtTryWithResource
,CtType<T>
,CtTypeAccess<A>
,CtTypedElement<T>
,CtTypeMember
,CtTypeMemberWildcardImportReference
,CtTypeParameter
,CtTypeParameterReference
,CtTypePattern
,CtTypeReference<T>
,CtUnaryOperator<T>
,CtUnboundVariableReference<T>
,CtUsedService
,CtVariable<T>
,CtVariableAccess<T>
,CtVariableRead<T>
,CtVariableReference<T>
,CtVariableWrite<T>
,CtWhile
,CtWildcardReference
,CtYieldStatement
- All Known Implementing Classes:
CompilationUnitImpl
,CtAnnotationFieldAccessImpl
,CtAnnotationImpl
,CtAnnotationMethodImpl
,CtAnnotationTypeImpl
,CtAnonymousExecutableImpl
,CtArrayAccessImpl
,CtArrayReadImpl
,CtArrayTypeReferenceImpl
,CtArrayWriteImpl
,CtAssertImpl
,CtAssignmentImpl
,CtBinaryOperatorImpl
,CtBlockImpl
,CtBreakImpl
,CtCaseImpl
,CtCatchImpl
,CtCatchVariableImpl
,CtCatchVariableReferenceImpl
,CtClassImpl
,CtCodeElementImpl
,CtCodeSnippetExpressionImpl
,CtCodeSnippetStatementImpl
,CtCommentImpl
,CtCompilationUnitImpl
,CtConditionalImpl
,CtConstructorCallImpl
,CtConstructorImpl
,CtContinueImpl
,CtDoImpl
,CtElementImpl
,CtEnumImpl
,CtEnumValueImpl
,CtExecutableImpl
,CtExecutableReferenceExpressionImpl
,CtExecutableReferenceImpl
,CtExpressionImpl
,CtFieldAccessImpl
,CtFieldImpl
,CtFieldReadImpl
,CtFieldReferenceImpl
,CtFieldWriteImpl
,CtForEachImpl
,CtForImpl
,CtIfImpl
,CtImportImpl
,CtInterfaceImpl
,CtIntersectionTypeReferenceImpl
,CtInvocationImpl
,CtJavaDocImpl
,CtJavaDocTagImpl
,CtLambdaImpl
,CtLiteralImpl
,CtLocalVariableImpl
,CtLocalVariableReferenceImpl
,CtLoopImpl
,CtMethodImpl
,CtModelImpl.CtRootPackage
,CtModuleImpl
,CtModuleReferenceImpl
,CtModuleRequirementImpl
,CtNamedElementImpl
,CtNewArrayImpl
,CtNewClassImpl
,CtOperatorAssignmentImpl
,CtPackageDeclarationImpl
,CtPackageExportImpl
,CtPackageImpl
,CtPackageReferenceImpl
,CtParameterImpl
,CtParameterReferenceImpl
,CtProvidedServiceImpl
,CtRecordComponentImpl
,CtRecordImpl
,CtReferenceImpl
,CtReturnImpl
,CtStatementImpl
,CtStatementListImpl
,CtSuperAccessImpl
,CtSwitchExpressionImpl
,CtSwitchImpl
,CtSynchronizedImpl
,CtTargetedExpressionImpl
,CtTextBlockImpl
,CtThisAccessImpl
,CtThrowImpl
,CtTryImpl
,CtTryWithResourceImpl
,CtTypeAccessImpl
,CtTypeImpl
,CtTypeMemberWildcardImportReferenceImpl
,CtTypeParameterImpl
,CtTypeParameterReferenceImpl
,CtTypePatternImpl
,CtTypeReferenceImpl
,CtUnaryOperatorImpl
,CtUnboundVariableReferenceImpl
,CtUnresolvedImport
,CtUsedServiceImpl
,CtVariableAccessImpl
,CtVariableReadImpl
,CtVariableReferenceImpl
,CtVariableWriteImpl
,CtWhileImpl
,CtWildcardReferenceImpl
,CtYieldStatementImpl
,InvisibleArrayConstructorImpl
,ModuleFactory.CtUnnamedModule
,NoSourcePosition.NullCompilationUnit
public interface CtElement extends FactoryAccessor, CtVisitable, Cloneable, CtQueryable, Serializable, SourcePositionHolder
This interface is the root interface for the metamodel elements (any program element).
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description <E extends CtElement>
EaddAnnotation(CtAnnotation<? extends Annotation> annotation)
Add an annotation for this element<E extends CtElement>
EaddComment(CtComment comment)
Add a comment to the current elementelement.addComment(element.getFactory().Code().createComment("comment", CtComment.CommentType.INLINE)
Iterable<CtElement>
asIterable()
Returns an Iterable instance of this CtElement, allowing for dfs traversal of its descendants.CtElement
clone()
Clone the element which calls this method in a new object.void
delete()
Deletes the element.Iterator<CtElement>
descendantIterator()
Returns an iterator over this CtElement's descendants.Map<String,Object>
getAllMetadata()
Retrieves all metadata stored in an element.<E extends CtElement>
List<E>getAnnotatedChildren(Class<? extends Annotation> annotationType)
Gets the child elements annotated with the given annotation type's instances.<A extends Annotation>
AgetAnnotation(Class<A> annotationType)
Searches for an annotation of the given class that annotates the current element.<A extends Annotation>
CtAnnotation<A>getAnnotation(CtTypeReference<A> annotationType)
Gets the annotation element for a given annotation type.List<CtAnnotation<? extends Annotation>>
getAnnotations()
Returns the annotations that are present on this element.List<CtComment>
getComments()
The list of commentsList<CtElement>
getDirectChildren()
String
getDocComment()
Returns the text of the documentation ("javadoc") comment of this element.<E extends CtElement>
List<E>getElements(Filter<E> filter)
Returns all the children elements recursively matching the filter.Object
getMetadata(String key)
Retrieves metadata stored in an element.Set<String>
getMetadataKeys()
Returns the metadata keys stored in an element.ElementSourceFragment
getOriginalSourceFragment()
Returns the original source code (maybe different from toString() if a transformation has been applied).CtElement
getParent()
Gets the parent of current reference.<P extends CtElement>
PgetParent(Class<P> parentType)
Gets the first parent that matches the given type.<E extends CtElement>
EgetParent(Filter<E> filter)
Gets the first parent that matches the filter.CtPath
getPath()
Return the path from the model root to this CtElement, eg `.spoon.test.path.Foo.foo#body#statement[index=0]`SourcePosition
getPosition()
Gets the position of this element in input source filesSet<CtTypeReference<?>>
getReferencedTypes()
Calculates and returns the set of all the types referenced by this element (and sub-elements in the AST).CtRole
getRoleInParent()
String
getShortRepresentation()
Build a short representation of any element.<T> T
getValueByRole(CtRole role)
<A extends Annotation>
booleanhasAnnotation(Class<A> annotationType)
boolean
hasParent(CtElement candidate)
Tells if the given element is a direct or indirect parent.boolean
isImplicit()
Returns true if this element is not present in the code (automatically added by the Java compiler or inferred when the model is built).boolean
isParentInitialized()
Tells if this parent has been initialized.String
prettyprint()
<E extends CtElement>
EputMetadata(String key, Object val)
Saves metadata inside an Element.boolean
removeAnnotation(CtAnnotation<? extends Annotation> annotation)
Remove an annotation for this element<E extends CtElement>
EremoveComment(CtComment comment)
Remove a comment<E extends CtElement>
voidreplace(Collection<E> elements)
Replaces this element by several elements.void
replace(CtElement element)
Replaces this element by another one.<E extends CtElement>
EsetAllMetadata(Map<String,Object> metadata)
Saves a bunch of metadata inside an Element<E extends CtElement>
EsetAnnotations(List<CtAnnotation<? extends Annotation>> annotation)
Sets the annotations for this element.<E extends CtElement>
EsetComments(List<CtComment> comments)
Set the comment list<E extends CtElement>
EsetDocComment(String docComment)
Sets the text of the documentation ("javadoc") comment of this declaration.<E extends CtElement>
EsetImplicit(boolean b)
Sets this element to be implicit.<E extends CtElement>
EsetParent(CtElement parent)
Manually sets the parent element of the current element.<E extends CtElement>
EsetPosition(SourcePosition position)
Sets the position in the Java source file.<E extends CtElement>
EsetPositions(SourcePosition position)
Sets the position of this element and all its children element.<E extends CtElement,T>
EsetValueByRole(CtRole role, T value)
Sets a field according to a role.String
toString()
String
toStringDebug()
void
updateAllParentsBelow()
Calculates and sets all the parents below this element.-
Methods inherited from interface spoon.reflect.visitor.chain.CtQueryable
filterChildren, map, map
-
Methods inherited from interface spoon.reflect.visitor.CtVisitable
accept
-
Methods inherited from interface spoon.processing.FactoryAccessor
getFactory, setFactory
-
-
-
-
Method Detail
-
getAnnotation
<A extends Annotation> A getAnnotation(Class<A> annotationType)
Searches for an annotation of the given class that annotates the current element. When used with a shadow element, this method might return an empty list even on an annotated element because annotations without a RUNTIME retention policy are lost after compilation. WARNING: this method uses a class loader proxy, which is costly. UsegetAnnotation(CtTypeReference)
preferably.NOTE: before using an annotation proxy, you have to make sure that all the types referenced by the annotation have been compiled and are in the classpath so that accessed values can be converted into the actual types.
- Type Parameters:
A
- the annotation's type- Parameters:
annotationType
- the annotation's class- Returns:
- if found, returns a proxy for this annotation
-
getAnnotation
<A extends Annotation> CtAnnotation<A> getAnnotation(CtTypeReference<A> annotationType)
Gets the annotation element for a given annotation type. When used with a shadow element, this method might return an empty list even on an annotated element because annotations without a RUNTIME retention policy are lost after compilation.- Parameters:
annotationType
- the annotation type- Returns:
- the annotation if this element is annotated by one annotation of the given type
-
hasAnnotation
<A extends Annotation> boolean hasAnnotation(Class<A> annotationType)
- Parameters:
annotationType
- the annotation type- Returns:
- true if the element is annotated by the given annotation type.
-
getAnnotations
List<CtAnnotation<? extends Annotation>> getAnnotations()
Returns the annotations that are present on this element. For sake of encapsulation, the returned list is unmodifiable.
-
getDocComment
String getDocComment()
Returns the text of the documentation ("javadoc") comment of this element. It contains the text of Javadoc together with the tags. If one only wants only the text without the tag, one can call `getComments().get(0).getContent()` If one wants to analyze the tags, one can call `getComments().get(0).asJavaDoc().getTags()` See alsogetComments()
.andCtJavaDoc
-
getShortRepresentation
String getShortRepresentation()
Build a short representation of any element.
-
getPosition
SourcePosition getPosition()
Gets the position of this element in input source files- Specified by:
getPosition
in interfaceSourcePositionHolder
- Returns:
- Source file and line number of this element.
It never returns null. Use
SourcePosition.isValidPosition()
to detect whether return instance contains start/end indexes.
-
replace
void replace(CtElement element)
Replaces this element by another one.
-
replace
<E extends CtElement> void replace(Collection<E> elements)
Replaces this element by several elements. If `elements` contains one single element, it is equivalent toreplace(CtElement)
. If `elements` is empty, it is equivalent todelete()
.
-
addAnnotation
<E extends CtElement> E addAnnotation(CtAnnotation<? extends Annotation> annotation)
Add an annotation for this element- Parameters:
annotation
-- Returns:
- true if this element changed as a result of the call
-
removeAnnotation
boolean removeAnnotation(CtAnnotation<? extends Annotation> annotation)
Remove an annotation for this element- Parameters:
annotation
-- Returns:
- true if this element changed as a result of the call
-
setPosition
<E extends CtElement> E setPosition(SourcePosition position)
Sets the position in the Java source file. Note that this information is used to feed the line numbers in the generated bytecode if any (which is useful for debugging).- Parameters:
position
- of this element in the input source files
-
getAnnotatedChildren
<E extends CtElement> List<E> getAnnotatedChildren(Class<? extends Annotation> annotationType)
Gets the child elements annotated with the given annotation type's instances.- Type Parameters:
E
- the element's type- Parameters:
annotationType
- the annotation type- Returns:
- all the child elements annotated with an instance of the given annotation type
-
isImplicit
boolean isImplicit()
Returns true if this element is not present in the code (automatically added by the Java compiler or inferred when the model is built). Consequently, implicit elements are not pretty-printed and have no position.
-
setImplicit
<E extends CtElement> E setImplicit(boolean b)
Sets this element to be implicit.
-
getReferencedTypes
Set<CtTypeReference<?>> getReferencedTypes()
Calculates and returns the set of all the types referenced by this element (and sub-elements in the AST).
-
getElements
<E extends CtElement> List<E> getElements(Filter<E> filter)
Returns all the children elements recursively matching the filter. If the receiver (this) matches the filter, it is also returned
-
setPositions
<E extends CtElement> E setPositions(SourcePosition position)
Sets the position of this element and all its children element. Note that this information is used to feed the line numbers in the generated bytecode if any (which is useful for debugging).- Parameters:
position
- of this element and all children in the input source file
-
setAnnotations
<E extends CtElement> E setAnnotations(List<CtAnnotation<? extends Annotation>> annotation)
Sets the annotations for this element.
-
getParent
CtElement getParent() throws ParentNotInitializedException
Gets the parent of current reference.- Throws:
ParentNotInitializedException
- when the parent of this element is not initialized
-
getParent
<P extends CtElement> P getParent(Class<P> parentType)
Gets the first parent that matches the given type.- Returns:
- the nearest matching parent; null if no match is found or this element has no parent
-
getParent
<E extends CtElement> E getParent(Filter<E> filter)
Gets the first parent that matches the filter.- Returns:
- the nearest matching parent; null if no match is found or this element has no parent
-
setParent
<E extends CtElement> E setParent(CtElement parent)
Manually sets the parent element of the current element.- Type Parameters:
E
- this element's type- Parameters:
parent
- parent reference.- Returns:
- this element
-
isParentInitialized
boolean isParentInitialized()
Tells if this parent has been initialized.
-
hasParent
boolean hasParent(CtElement candidate)
Tells if the given element is a direct or indirect parent.
-
updateAllParentsBelow
void updateAllParentsBelow()
Calculates and sets all the parents below this element. This function can be called to check and fix parents after manipulating the model.
-
getRoleInParent
CtRole getRoleInParent()
- Returns:
- the
CtRole
of the parent's attribute where this element is used. It returns the primary role. For example ((CtMethod) method).getRoleInParent() returnsCtRole.TYPE_MEMBER
.
Returns null if parent doesn't contain this element as direct children or if this element has no parent.
-
delete
void delete()
Deletes the element. For instance, delete a statement from its containing block. Warning: it may result in an incorrect AST, use at your own risk.
-
setAllMetadata
<E extends CtElement> E setAllMetadata(Map<String,Object> metadata)
Saves a bunch of metadata inside an Element
-
putMetadata
<E extends CtElement> E putMetadata(String key, Object val)
Saves metadata inside an Element.
-
getMetadata
Object getMetadata(String key)
Retrieves metadata stored in an element. Returns null if it does not exist.
-
addComment
<E extends CtElement> E addComment(CtComment comment)
Add a comment to the current elementelement.addComment(element.getFactory().Code().createComment("comment", CtComment.CommentType.INLINE)
- Parameters:
comment
- the comment
-
removeComment
<E extends CtElement> E removeComment(CtComment comment)
Remove a comment- Parameters:
comment
- the comment to remove
-
clone
CtElement clone()
Clone the element which calls this method in a new object. Note that that references are kept as is, and thus, so if you clone whole classes or methods, some parts of the cloned element (eg executable references) may still point to the initial element. In this case, consider using methodsRefactoring.copyType(CtType)
andRefactoring.copyMethod(CtMethod)
instead which does additional work beyond cloning.
-
getValueByRole
<T> T getValueByRole(CtRole role)
- Parameters:
role
- the role of the returned attribute with respect to this element. For instance, "klass.getValueByRole(CtRole.METHOD)" returns a list of methods. SeeRoleHandlerHelper
for more advanced methods.- Returns:
- a a single value (eg a CtElement), List, Set or Map depending on this `element` and `role`. Returned collections are read-only.
-
setValueByRole
<E extends CtElement,T> E setValueByRole(CtRole role, T value)
Sets a field according to a role.- Parameters:
role
- the role of the field to be setvalue
- to be assigned to this field.
-
getPath
CtPath getPath()
Return the path from the model root to this CtElement, eg `.spoon.test.path.Foo.foo#body#statement[index=0]`
-
descendantIterator
Iterator<CtElement> descendantIterator()
Returns an iterator over this CtElement's descendants.- Returns:
- An iterator over this CtElement's descendants.
-
asIterable
Iterable<CtElement> asIterable()
Returns an Iterable instance of this CtElement, allowing for dfs traversal of its descendants.- Returns:
- an Iterable object that allows iterating through this CtElement's descendants.
-
getDirectChildren
List<CtElement> getDirectChildren()
- Returns:
- a list of CtElement containing the element's direct children.
-
toString
String toString()
- Overrides:
toString
in classObject
- Returns:
- the source code of this element according to the setting of
Environment.getPrettyPrintingMode()
.
-
toStringDebug
String toStringDebug()
- Returns:
- the most straightforward and explicit version of this element.
-
prettyprint
String prettyprint()
- Returns:
- the source code of this element with the pretty-printing rules of Spoon
Warning: this is not side-effect free, this triggers some
ImportAnalyzer
which would change the model: add/remove imports, change the value `implicit` of some model elements, etc.
-
getOriginalSourceFragment
ElementSourceFragment getOriginalSourceFragment()
Description copied from interface:SourcePositionHolder
Returns the original source code (maybe different from toString() if a transformation has been applied). Warning: this is a advanced method which cannot be considered as part of the stable API- Specified by:
getOriginalSourceFragment
in interfaceSourcePositionHolder
-
-