Package spoon.support.sniper.internal
Class ElementSourceFragment
- java.lang.Object
-
- spoon.support.sniper.internal.ElementSourceFragment
-
- All Implemented Interfaces:
SourceFragment
public class ElementSourceFragment extends Object implements SourceFragment
Represents a part of source code of anCtElement
It is connected into a tree ofElementSourceFragment
s. UseSourcePositionHolder.getOriginalSourceFragment()
to get it.
-
-
Field Summary
Fields Modifier and Type Field Description static ElementSourceFragment
NO_SOURCE_FRAGMENT
represents anElementSourceFragment
, which doesn't exist
-
Constructor Summary
Constructors Constructor Description ElementSourceFragment(SourcePositionHolder element, RoleHandler roleHandlerInParent)
Creates a source fragment ofSourcePositionHolder
-
Method Summary
-
-
-
Field Detail
-
NO_SOURCE_FRAGMENT
public static final ElementSourceFragment NO_SOURCE_FRAGMENT
represents anElementSourceFragment
, which doesn't exist
-
-
Constructor Detail
-
ElementSourceFragment
public ElementSourceFragment(SourcePositionHolder element, RoleHandler roleHandlerInParent)
Creates a source fragment ofSourcePositionHolder
- Parameters:
element
- targetSourcePositionHolder
roleHandlerInParent
- TheRoleHandler
, which defines role of target `element` in it's parent
-
-
Method Detail
-
getStart
public int getStart()
- Returns:
- offset of first character which belongs to this fragment
-
getEnd
public int getEnd()
- Returns:
- offset of character after this fragment
-
getSourcePosition
public SourcePosition getSourcePosition()
- Returns:
SourcePosition
of this fragment
-
getSourceCode
public String getSourceCode()
- Specified by:
getSourceCode
in interfaceSourceFragment
- Returns:
- origin source code of this fragment
-
getSourceCode
public String getSourceCode(int start, int end)
- Parameters:
start
- start offset relative to compilation unitend
- end offset (after last character) relative to compilation unit- Returns:
- source code of this Fragment between start/end offsets
-
createSourceFragmentsFrom
public static ElementSourceFragment createSourceFragmentsFrom(CtElement element)
Builds a tree of source fragments for `element` and all its children, based on the source positions of each element- Parameters:
element
- the root element of the tree
-
add
public ElementSourceFragment add(ElementSourceFragment other)
adds `other`ElementSourceFragment
into tree ofElementSourceFragment
s represented by this root element- Parameters:
other
- to be addedElementSourceFragment
- Returns:
- new root of the tree of the
ElementSourceFragment
s. It can be be this or `other`
-
getNextSibling
public ElementSourceFragment getNextSibling()
- Returns:
ElementSourceFragment
which belongs to the same parent and is next in the sources
-
getFirstChild
public ElementSourceFragment getFirstChild()
- Returns:
ElementSourceFragment
, which is first child of this fragment
-
getSourceFragmentOf
public ElementSourceFragment getSourceFragmentOf(SourcePositionHolder element, int start, int end)
Searches the tree of fragments for theElementSourceFragment
with expected `element`, which contains `start` and `end` source interval. It searches in siblings and children of thisElementSourceFragment
recursively.- Parameters:
element
- theSourcePositionHolder
of fragment it is looking for or null for any elementstart
- the start offset of searched fragmentend
- the offset of next character after the end of searched fragment- Returns:
ElementSourceFragment
which represents the root of the CtElement whose sources are in interval [start, end]
-
getElement
public SourcePositionHolder getElement()
- Returns:
CtElement
whose source code is contained in this fragment. May be null
-
getChildrenFragments
public List<SourceFragment> getChildrenFragments()
Note: the List of children is flat. The child fragments of collections (parameters, type members, ...) are next to each other.- Returns:
- list of child fragments of this
ElementSourceFragment
.
-
getGroupedChildrenFragments
public List<SourceFragment> getGroupedChildrenFragments()
Detects all child fragments of thisElementSourceFragment
. Note: the List of children contains oneCollectionSourceFragment
for each collection of fragments (parameters, type members, ...). Note: theCollectionSourceFragment
may contain a mix of fragments of different roles, when they overlap. For example this code contains mix of annotations and modifierspublic @Test static @Ignored void method()
- Returns:
- list of child fragments of this
ElementSourceFragment
where fragments, which belongs to the same collection are grouped intoCollectionSourceFragment
-
getRoleInParent
public CtRole getRoleInParent()
- Returns:
- role of the element of this fragment in scope of it's parent
-
getContainerKindInParent
public ContainerKind getContainerKindInParent()
- Returns:
- the
ContainerKind
of the attribute which holds the element of this fragment in it's parent
-
-