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

public abstract class CtElementImpl extends Object implements CtElement
Contains the default implementation of most CtElement methods.
See Also:
Serialized Form
  • Field Details

    • LOGGER

      protected static final org.slf4j.Logger LOGGER
    • ERROR_MESSAGE_TO_STRING

      public static final String ERROR_MESSAGE_TO_STRING
      See Also:
      Constant Field Values
    • factory

      protected transient Factory factory
      this field `factory` must be transient in order to allow proper serialization the factory is restored in all AST nodes in SerializationModelStreamer
    • parent

      protected CtElement parent
  • Constructor Details

    • CtElementImpl

      public CtElementImpl()
  • Method Details

    • emptyList

      public static <T> List<T> emptyList()
    • emptySet

      public static <T> Set<T> emptySet()
    • unmodifiableList

      public static <T> List<T> unmodifiableList(List<T> list)
    • getShortRepresentation

      public String getShortRepresentation()
      Description copied from interface: CtElement
      Build a short representation of any element.
      Specified by:
      getShortRepresentation in interface CtElement
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • getAnnotation

      public <A extends Annotation> A getAnnotation(Class<A> annotationType)
      Description copied from interface: CtElement
      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. Use CtElement.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:
      getAnnotation in interface CtElement
      Type Parameters:
      A - the annotation's type
      Parameters:
      annotationType - the annotation's class
      Returns:
      if found, returns a proxy for this annotation
    • hasAnnotation

      public <A extends Annotation> boolean hasAnnotation(Class<A> annotationType)
      Specified by:
      hasAnnotation in interface CtElement
      Parameters:
      annotationType - the annotation type
      Returns:
      true if the element is annotated by the given annotation type.
    • getAnnotation

      public <A extends Annotation> CtAnnotation<A> getAnnotation(CtTypeReference<A> annotationType)
      Description copied from interface: CtElement
      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.
      Specified by:
      getAnnotation in interface CtElement
      Parameters:
      annotationType - the annotation type
      Returns:
      the annotation if this element is annotated by one annotation of the given type
    • getAnnotations

      public List<CtAnnotation<? extends Annotation>> getAnnotations()
      Description copied from interface: CtElement
      Returns the annotations that are present on this element. For sake of encapsulation, the returned list is unmodifiable.
      Specified by:
      getAnnotations in interface CtElement
    • getDocComment

      public String getDocComment()
      Description copied from interface: CtElement
      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 also CtElement.getComments().and CtJavaDoc
      Specified by:
      getDocComment in interface CtElement
    • getPosition

      public SourcePosition getPosition()
      Description copied from interface: CtElement
      Gets the position of this element in input source files
      Specified by:
      getPosition in interface CtElement
      Specified by:
      getPosition in interface SourcePositionHolder
      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()
      Overrides:
      hashCode in class Object
    • setAnnotations

      public <E extends CtElement> E setAnnotations(List<CtAnnotation<? extends Annotation>> annotations)
      Description copied from interface: CtElement
      Sets the annotations for this element.
      Specified by:
      setAnnotations in interface CtElement
    • delete

      public void delete()
      Description copied from interface: CtElement
      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.
      Specified by:
      delete in interface CtElement
    • addAnnotation

      public <E extends CtElement> E addAnnotation(CtAnnotation<? extends Annotation> annotation)
      Description copied from interface: CtElement
      Add an annotation for this element
      Specified by:
      addAnnotation in interface CtElement
      Returns:
      true if this element changed as a result of the call
    • removeAnnotation

      public boolean removeAnnotation(CtAnnotation<? extends Annotation> annotation)
      Description copied from interface: CtElement
      Remove an annotation for this element
      Specified by:
      removeAnnotation in interface CtElement
      Returns:
      true if this element changed as a result of the call
    • setDocComment

      public <E extends CtElement> E setDocComment(String docComment)
      Description copied from interface: CtElement
      Sets the text of the documentation ("javadoc") comment of this declaration. This API will set the content of the first javadoc CtComment or create a new javadoc CtComment if no javadoc CtComment is available on this object.
      Specified by:
      setDocComment in interface CtElement
    • setPosition

      public <E extends CtElement> E setPosition(SourcePosition position)
      Description copied from interface: CtElement
      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).
      Specified by:
      setPosition in interface CtElement
      Parameters:
      position - of this element in the input source files
    • setPositions

      public <E extends CtElement> E setPositions(SourcePosition position)
      Description copied from interface: CtElement
      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).
      Specified by:
      setPositions in interface CtElement
      Parameters:
      position - of this element and all children in the input source file
    • prettyprint

      public String prettyprint()
      Specified by:
      prettyprint in interface CtElement
      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.
    • toStringDebug

      public String toStringDebug()
      Specified by:
      toStringDebug in interface CtElement
      Returns:
      the most straightforward and explicit version of this element.
    • toString

      public String toString()
      Specified by:
      toString in interface CtElement
      Overrides:
      toString in class Object
      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: CtElement
      Gets the child elements annotated with the given annotation type's instances.
      Specified by:
      getAnnotatedChildren in interface CtElement
      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: CtElement
      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.
      Specified by:
      isImplicit in interface CtElement
    • setImplicit

      public <E extends CtElement> E setImplicit(boolean implicit)
      Description copied from interface: CtElement
      Sets this element to be implicit.
      Specified by:
      setImplicit in interface CtElement
    • getReferencedTypes

      public Set<CtTypeReference<?>> getReferencedTypes()
      Description copied from interface: CtElement
      Calculates and returns the set of all the types referenced by this element (and sub-elements in the AST).
      Specified by:
      getReferencedTypes in interface CtElement
    • getElements

      public <E extends CtElement> List<E> getElements(Filter<E> filter)
      Description copied from interface: CtElement
      Returns all the children elements recursively matching the filter. If the receiver (this) matches the filter, it is also returned
      Specified by:
      getElements in interface CtElement
    • map

      public <I> CtQuery map(CtConsumableFunction<I> queryStep)
      Specified by:
      map in interface CtQueryable
      See Also:
      CtQuery.map(CtConsumableFunction)
    • map

      public <I,​ R> CtQuery map(CtFunction<I,​R> function)
      Specified by:
      map in interface CtQueryable
      See Also:
      CtQuery.map(CtFunction)
    • filterChildren

      public <P extends CtElement> CtQuery filterChildren(Filter<P> predicate)
      Specified by:
      filterChildren in interface CtQueryable
      See Also:
      CtQuery.filterChildren(Filter)
    • getParent

      public CtElement getParent() throws ParentNotInitializedException
      Description copied from interface: CtElement
      Gets the parent of current reference.
      Specified by:
      getParent in interface CtElement
      Throws:
      ParentNotInitializedException - when the parent of this element is not initialized
    • setParent

      public <E extends CtElement> E setParent(CtElement parent)
      Description copied from interface: CtElement
      Manually sets the parent element of the current element.
      Specified by:
      setParent in interface CtElement
      Type Parameters:
      E - this element's type
      Parameters:
      parent - parent reference.
      Returns:
      this element
    • isParentInitialized

      public boolean isParentInitialized()
      Description copied from interface: CtElement
      Tells if this parent has been initialized.
      Specified by:
      isParentInitialized in interface CtElement
    • getParent

      public <P extends CtElement> P getParent(Class<P> parentType)
      Description copied from interface: CtElement
      Gets the first parent that matches the given type.
      Specified by:
      getParent in interface CtElement
      Returns:
      the nearest matching parent; null if no match is found or this element has no parent
    • getParent

      public <E extends CtElement> E getParent(Filter<E> filter)
      Description copied from interface: CtElement
      Gets the first parent that matches the filter.
      Specified by:
      getParent in interface CtElement
      Returns:
      the nearest matching parent; null if no match is found or this element has no parent
    • hasParent

      public boolean hasParent(CtElement candidate)
      Description copied from interface: CtElement
      Tells if the given element is a direct or indirect parent.
      Specified by:
      hasParent in interface CtElement
    • getRoleInParent

      public CtRole getRoleInParent()
      Specified by:
      getRoleInParent in interface CtElement
      Returns:
      the CtRole of the parent's attribute where this element is used. It returns the primary role. For example ((CtMethod) method).getRoleInParent() returns CtRole.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: CtElement
      Calculates and sets all the parents below this element. This function can be called to check and fix parents after manipulating the model.
      Specified by:
      updateAllParentsBelow in interface CtElement
    • getFactory

      public Factory getFactory()
      Description copied from interface: FactoryAccessor
      Gets the factory of this object.
      Specified by:
      getFactory in interface FactoryAccessor
    • setFactory

      public void setFactory(Factory factory)
      Description copied from interface: FactoryAccessor
      Sets the factory object.
      Specified by:
      setFactory in interface FactoryAccessor
    • replace

      public void replace(CtElement element)
      Description copied from interface: CtElement
      Replaces this element by another one.
      Specified by:
      replace in interface CtElement
    • replace

      public <E extends CtElement> void replace(Collection<E> elements)
      Description copied from interface: CtElement
      Replaces this element by several elements. If `elements` contains one single element, it is equivalent to CtElement.replace(CtElement). If `elements` is empty, it is equivalent to CtElement.delete().
      Specified by:
      replace in interface CtElement
    • setAllMetadata

      public <E extends CtElement> E setAllMetadata(Map<String,​Object> metadata)
      Description copied from interface: CtElement
      Saves a bunch of metadata inside an Element
      Specified by:
      setAllMetadata in interface CtElement
    • putMetadata

      public <E extends CtElement> E putMetadata(String key, Object val)
      Description copied from interface: CtElement
      Saves metadata inside an Element.
      Specified by:
      putMetadata in interface CtElement
    • getMetadata

      public Object getMetadata(String key)
      Description copied from interface: CtElement
      Retrieves metadata stored in an element. Returns null if it does not exist.
      Specified by:
      getMetadata in interface CtElement
    • getAllMetadata

      public Map<String,​Object> getAllMetadata()
      Description copied from interface: CtElement
      Retrieves all metadata stored in an element.
      Specified by:
      getAllMetadata in interface CtElement
    • getMetadataKeys

      public Set<String> getMetadataKeys()
      Description copied from interface: CtElement
      Returns the metadata keys stored in an element.
      Specified by:
      getMetadataKeys in interface CtElement
    • getComments

      public List<CtComment> getComments()
      Description copied from interface: CtElement
      The list of comments
      Specified by:
      getComments in interface CtElement
      Returns:
      the list of comment
    • addComment

      public <E extends CtElement> E addComment(CtComment comment)
      Description copied from interface: CtElement
      Add a comment to the current element element.addComment(element.getFactory().Code().createComment("comment", CtComment.CommentType.INLINE)
      Specified by:
      addComment in interface CtElement
      Parameters:
      comment - the comment
    • removeComment

      public <E extends CtElement> E removeComment(CtComment comment)
      Description copied from interface: CtElement
      Remove a comment
      Specified by:
      removeComment in interface CtElement
      Parameters:
      comment - the comment to remove
    • setComments

      public <E extends CtElement> E setComments(List<CtComment> comments)
      Description copied from interface: CtElement
      Set the comment list
      Specified by:
      setComments in interface CtElement
    • clone

      public CtElement clone()
      Description copied from interface: CtElement
      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 methods Refactoring.copyType(CtType) and Refactoring.copyMethod(CtMethod) instead which does additional work beyond cloning.
      Specified by:
      clone in interface CtElement
      Overrides:
      clone in class Object
    • getValueByRole

      public <T> T getValueByRole(CtRole role)
      Specified by:
      getValueByRole in interface CtElement
      Parameters:
      role - the role of the returned attribute with respect to this element. For instance, "klass.getValueByRole(CtRole.METHOD)" returns a list of methods. See RoleHandlerHelper 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

      public <E extends CtElement,​ T> E setValueByRole(CtRole role, T value)
      Description copied from interface: CtElement
      Sets a field according to a role.
      Specified by:
      setValueByRole in interface CtElement
      Parameters:
      role - the role of the field to be set
      value - to be assigned to this field.
    • getPath

      public CtPath getPath()
      Description copied from interface: CtElement
      Return the path from the model root to this CtElement, eg `.spoon.test.path.Foo.foo#body#statement[index=0]`
      Specified by:
      getPath in interface CtElement
    • descendantIterator

      public Iterator<CtElement> descendantIterator()
      Description copied from interface: CtElement
      Returns an iterator over this CtElement's descendants.
      Specified by:
      descendantIterator in interface CtElement
      Returns:
      An iterator over this CtElement's descendants.
    • asIterable

      public Iterable<CtElement> asIterable()
      Description copied from interface: CtElement
      Returns an Iterable instance of this CtElement, allowing for dfs traversal of its descendants.
      Specified by:
      asIterable in interface CtElement
      Returns:
      an Iterable object that allows iterating through this CtElement's descendants.
    • getOriginalSourceFragment

      public 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 interface CtElement
      Specified by:
      getOriginalSourceFragment in interface SourcePositionHolder
    • comment

      public void comment()
      Replace the statement with a CtComment having the statement as text
    • getDirectChildren

      public List<CtElement> getDirectChildren()
      Specified by:
      getDirectChildren in interface CtElement
      Returns:
      a list of CtElement containing the element's direct children.