Class CtElementImpl
java.lang.Object
spoon.support.reflect.declaration.CtElementImpl
- All Implemented Interfaces:
Serializable,Cloneable,FactoryAccessor,SourcePositionHolder,CtElement,CtQueryable,CtVisitable
- Direct Known Subclasses:
CtCodeElementImpl,CtCompilationUnitImpl,CtImportImpl,CtJavaDocTagImpl,CtModuleRequirementImpl,CtNamedElementImpl,CtPackageDeclarationImpl,CtPackageExportImpl,CtProvidedServiceImpl,CtReferenceImpl,CtTypeMemberWildcardImportReferenceImpl,CtUnresolvedImport,CtUsedServiceImpl
Contains the default implementation of most CtElement methods.
- See Also:
- Serialized Form
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic Stringprotected Factorythis field `factory` must be transient in order to allow proper serialization the factory is restored in all AST nodes inSerializationModelStreamerprotected static org.slf4j.Loggerprotected CtElement -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription<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)Returns an Iterable instance of this CtElement, allowing for dfs traversal of its descendants.clone()Clone the element which calls this method in a new object.voidcomment()Replace the statement with a CtComment having the statement as textvoiddelete()Deletes the element.Returns an iterator over this CtElement's descendants.static <T> List<T>static <T> Set<T>emptySet()booleanfilterChildren(Filter<P> predicate)Retrieves all metadata stored in an element.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>>Returns the annotations that are present on this element.The list of commentsReturns the text of the documentation ("javadoc") comment of this element.getElements(Filter<E> filter)Returns all the children elements recursively matching the filter.Gets the factory of this object.getMetadata(String key)Retrieves metadata stored in an element.Returns the metadata keys stored in an element.Returns the original source code (maybe different from toString() if a transformation has been applied).Gets the parent of current reference.<P extends CtElement>
PGets the first parent that matches the given type.<E extends CtElement>
EGets the first parent that matches the filter.getPath()Return the path from the model root to this CtElement, eg `.spoon.test.path.Foo.foo#body#statement[index=0]`Gets the position of this element in input source filesSet<CtTypeReference<?>>Calculates and returns the set of all the types referenced by this element (and sub-elements in the AST).Build a short representation of any element.<T> TgetValueByRole(CtRole role)<A extends Annotation>
booleanhasAnnotation(Class<A> annotationType)inthashCode()booleanTells if the given element is a direct or indirect parent.booleanReturns true if this element is not present in the code (automatically added by the Java compiler or inferred when the model is built).booleanTells if this parent has been initialized.<I> CtQuerymap(CtConsumableFunction<I> queryStep)<I, R> CtQuerymap(CtFunction<I,R> function)<E extends CtElement>
EputMetadata(String key, Object val)Saves metadata inside an Element.booleanremoveAnnotation(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.voidReplaces 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>> annotations)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.voidsetFactory(Factory factory)Sets the factory object.<E extends CtElement>
EsetImplicit(boolean implicit)Sets this element to be implicit.<E extends CtElement>
EManually 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.toString()static <T> List<T>unmodifiableList(List<T> list)voidCalculates and sets all the parents below this element.Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface spoon.reflect.visitor.CtVisitable
accept
-
Field Details
-
LOGGER
protected static final org.slf4j.Logger LOGGER -
ERROR_MESSAGE_TO_STRING
- See Also:
- Constant Field Values
-
factory
this field `factory` must be transient in order to allow proper serialization the factory is restored in all AST nodes inSerializationModelStreamer -
parent
-
-
Constructor Details
-
CtElementImpl
public CtElementImpl()
-
-
Method Details
-
emptyList
-
emptySet
-
unmodifiableList
-
getShortRepresentation
Description copied from interface:CtElementBuild a short representation of any element.- Specified by:
getShortRepresentationin interfaceCtElement
-
equals
-
getAnnotation
Description copied from interface:CtElementSearches 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. UseCtElement.getAnnotation(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.
- Specified by:
getAnnotationin interfaceCtElement- Type Parameters:
A- the annotation's type- Parameters:
annotationType- the annotation's class- Returns:
- if found, returns a proxy for this annotation
-
hasAnnotation
- Specified by:
hasAnnotationin interfaceCtElement- Parameters:
annotationType- the annotation type- Returns:
- true if the element is annotated by the given annotation type.
-
getAnnotation
Description copied from interface:CtElementGets 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.- Specified by:
getAnnotationin interfaceCtElement- Parameters:
annotationType- the annotation type- Returns:
- the annotation if this element is annotated by one annotation of the given type
-
getAnnotations
Description copied from interface:CtElementReturns the annotations that are present on this element. For sake of encapsulation, the returned list is unmodifiable.- Specified by:
getAnnotationsin interfaceCtElement
-
getDocComment
Description copied from interface:CtElementReturns 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 alsoCtElement.getComments().andCtJavaDoc- Specified by:
getDocCommentin interfaceCtElement
-
getPosition
Description copied from interface:CtElementGets the position of this element in input source files- Specified by:
getPositionin interfaceCtElement- Specified by:
getPositionin 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.
-
hashCode
public int hashCode() -
setAnnotations
public <E extends CtElement> E setAnnotations(List<CtAnnotation<? extends Annotation>> annotations)Description copied from interface:CtElementSets the annotations for this element.- Specified by:
setAnnotationsin interfaceCtElement
-
delete
public void delete()Description copied from interface:CtElementDeletes the element. For instance, delete a statement from its containing block. Warning: it may result in an incorrect AST, use at your own risk. -
addAnnotation
Description copied from interface:CtElementAdd an annotation for this element- Specified by:
addAnnotationin interfaceCtElement- Returns:
- true if this element changed as a result of the call
-
removeAnnotation
Description copied from interface:CtElementRemove an annotation for this element- Specified by:
removeAnnotationin interfaceCtElement- Returns:
- true if this element changed as a result of the call
-
setDocComment
Description copied from interface:CtElementSets the text of the documentation ("javadoc") comment of this declaration. This API will set the content of the first javadocCtCommentor create a new javadocCtCommentif no javadocCtCommentis available on this object.- Specified by:
setDocCommentin interfaceCtElement
-
setPosition
Description copied from interface:CtElementSets 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).- Specified by:
setPositionin interfaceCtElement- Parameters:
position- of this element in the input source files
-
setPositions
Description copied from interface:CtElementSets 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).- Specified by:
setPositionsin interfaceCtElement- Parameters:
position- of this element and all children in the input source file
-
prettyprint
- Specified by:
prettyprintin interfaceCtElement- Returns:
- the source code of this element with the pretty-printing rules of Spoon
Warning: this is not side-effect free, this triggers some
ImportAnalyzerwhich would change the model: add/remove imports, change the value `implicit` of some model elements, etc.
-
toStringDebug
- Specified by:
toStringDebugin interfaceCtElement- Returns:
- the most straightforward and explicit version of this element.
-
toString
- Specified by:
toStringin interfaceCtElement- Overrides:
toStringin classObject- Returns:
- the source code of this element according to the setting of
Environment.getPrettyPrintingMode().
-
getAnnotatedChildren
public <E extends CtElement> List<E> getAnnotatedChildren(Class<? extends Annotation> annotationType)Description copied from interface:CtElementGets the child elements annotated with the given annotation type's instances.- Specified by:
getAnnotatedChildrenin interfaceCtElement- 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
public boolean isImplicit()Description copied from interface:CtElementReturns 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.- Specified by:
isImplicitin interfaceCtElement
-
setImplicit
Description copied from interface:CtElementSets this element to be implicit.- Specified by:
setImplicitin interfaceCtElement
-
getReferencedTypes
Description copied from interface:CtElementCalculates and returns the set of all the types referenced by this element (and sub-elements in the AST).- Specified by:
getReferencedTypesin interfaceCtElement
-
getElements
Description copied from interface:CtElementReturns all the children elements recursively matching the filter. If the receiver (this) matches the filter, it is also returned- Specified by:
getElementsin interfaceCtElement
-
map
- Specified by:
mapin interfaceCtQueryable- See Also:
CtQuery.map(CtConsumableFunction)
-
map
- Specified by:
mapin interfaceCtQueryable- See Also:
CtQuery.map(CtFunction)
-
filterChildren
- Specified by:
filterChildrenin interfaceCtQueryable- See Also:
CtQuery.filterChildren(Filter)
-
getParent
Description copied from interface:CtElementGets the parent of current reference.- Specified by:
getParentin interfaceCtElement- Throws:
ParentNotInitializedException- when the parent of this element is not initialized
-
setParent
Description copied from interface:CtElementManually sets the parent element of the current element. -
isParentInitialized
public boolean isParentInitialized()Description copied from interface:CtElementTells if this parent has been initialized.- Specified by:
isParentInitializedin interfaceCtElement
-
getParent
Description copied from interface:CtElementGets the first parent that matches the given type. -
getParent
Description copied from interface:CtElementGets the first parent that matches the filter. -
hasParent
Description copied from interface:CtElementTells if the given element is a direct or indirect parent. -
getRoleInParent
- Specified by:
getRoleInParentin interfaceCtElement- Returns:
- the
CtRoleof 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.
-
updateAllParentsBelow
public void updateAllParentsBelow()Description copied from interface:CtElementCalculates and sets all the parents below this element. This function can be called to check and fix parents after manipulating the model.- Specified by:
updateAllParentsBelowin interfaceCtElement
-
getFactory
Description copied from interface:FactoryAccessorGets the factory of this object.- Specified by:
getFactoryin interfaceFactoryAccessor
-
setFactory
Description copied from interface:FactoryAccessorSets the factory object.- Specified by:
setFactoryin interfaceFactoryAccessor
-
replace
Description copied from interface:CtElementReplaces this element by another one. -
replace
Description copied from interface:CtElementReplaces this element by several elements. If `elements` contains one single element, it is equivalent toCtElement.replace(CtElement). If `elements` is empty, it is equivalent toCtElement.delete(). -
setAllMetadata
Description copied from interface:CtElementSaves a bunch of metadata inside an Element- Specified by:
setAllMetadatain interfaceCtElement
-
putMetadata
Description copied from interface:CtElementSaves metadata inside an Element.- Specified by:
putMetadatain interfaceCtElement
-
getMetadata
Description copied from interface:CtElementRetrieves metadata stored in an element. Returns null if it does not exist.- Specified by:
getMetadatain interfaceCtElement
-
getAllMetadata
Description copied from interface:CtElementRetrieves all metadata stored in an element.- Specified by:
getAllMetadatain interfaceCtElement
-
getMetadataKeys
Description copied from interface:CtElementReturns the metadata keys stored in an element.- Specified by:
getMetadataKeysin interfaceCtElement
-
getComments
Description copied from interface:CtElementThe list of comments- Specified by:
getCommentsin interfaceCtElement- Returns:
- the list of comment
-
addComment
Description copied from interface:CtElementAdd a comment to the current elementelement.addComment(element.getFactory().Code().createComment("comment", CtComment.CommentType.INLINE)- Specified by:
addCommentin interfaceCtElement- Parameters:
comment- the comment
-
removeComment
Description copied from interface:CtElementRemove a comment- Specified by:
removeCommentin interfaceCtElement- Parameters:
comment- the comment to remove
-
setComments
Description copied from interface:CtElementSet the comment list- Specified by:
setCommentsin interfaceCtElement
-
clone
Description copied from interface:CtElementClone 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
- Specified by:
getValueByRolein interfaceCtElement- Parameters:
role- the role of the returned attribute with respect to this element. For instance, "klass.getValueByRole(CtRole.METHOD)" returns a list of methods. SeeRoleHandlerHelperfor 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
Description copied from interface:CtElementSets a field according to a role.- Specified by:
setValueByRolein interfaceCtElement- Parameters:
role- the role of the field to be setvalue- to be assigned to this field.
-
getPath
Description copied from interface:CtElementReturn the path from the model root to this CtElement, eg `.spoon.test.path.Foo.foo#body#statement[index=0]` -
descendantIterator
Description copied from interface:CtElementReturns an iterator over this CtElement's descendants.- Specified by:
descendantIteratorin interfaceCtElement- Returns:
- An iterator over this CtElement's descendants.
-
asIterable
Description copied from interface:CtElementReturns an Iterable instance of this CtElement, allowing for dfs traversal of its descendants.- Specified by:
asIterablein interfaceCtElement- Returns:
- an Iterable object that allows iterating through this CtElement's descendants.
-
getOriginalSourceFragment
Description copied from interface:SourcePositionHolderReturns 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:
getOriginalSourceFragmentin interfaceCtElement- Specified by:
getOriginalSourceFragmentin interfaceSourcePositionHolder
-
comment
public void comment()Replace the statement with a CtComment having the statement as text -
getDirectChildren
- Specified by:
getDirectChildrenin interfaceCtElement- Returns:
- a list of CtElement containing the element's direct children.
-