Package org.eclipse.jdt.core.dom.rewrite

# Class ListRewrite

java.lang.Object
org.eclipse.jdt.core.dom.rewrite.ListRewrite

public final class ListRewrite extends Object
For describing manipulations to a child list property of an AST node.

This class is not intended to be subclassed.

Since:
  3.0
See Also:
  - `ASTRewrite.getListRewrite(ASTNode, ChildListPropertyDescriptor)`

## Method Summary

Modifier and Type
Method
Description
`final ASTNode`
`createCopyTarget(ASTNode first, ASTNode last)`
Creates and returns a placeholder node for a true copy of a range of nodes of the current list.
`final ASTNode`
`createMoveTarget(ASTNode first, ASTNode last)`
Creates and returns a placeholder node for a move of a range of nodes of the current list.
`final ASTNode`
`createMoveTarget(ASTNode first, ASTNode last, ASTNode replacingNode, org.eclipse.text.edits.TextEditGroup editGroup)`
Creates and returns a placeholder node for a move of a range of nodes of the current list.
`ASTRewrite`
`getASTRewrite()`
Returns the ASTRewrite instance from which this ListRewriter has been created from.
`StructuralPropertyDescriptor`
`getLocationInParent()`
Returns the property of the parent node for which this list rewriter was created.
`List`
`getOriginalList()`
Returns the original nodes in the list property managed by this rewriter.
`ASTNode`
`getParent()`
Returns the parent of the list for which this list rewriter was created.
`List`
`getRewrittenList()`
Returns the nodes in the revised list property managed by this rewriter.
`void`
`insertAfter(ASTNode node, ASTNode previousElement, org.eclipse.text.edits.TextEditGroup editGroup)`
Inserts the given node into the list after the given element.
`void`
`insertAt(ASTNode node, int index, org.eclipse.text.edits.TextEditGroup editGroup)`
Inserts the given node into the list at the given index.
`void`
`insertBefore(ASTNode node, ASTNode nextElement, org.eclipse.text.edits.TextEditGroup editGroup)`
Inserts the given node into the list before the given element.
`void`
`insertFirst(ASTNode node, org.eclipse.text.edits.TextEditGroup editGroup)`
Inserts the given node into the list at the start of the list.
`void`
`insertLast(ASTNode node, org.eclipse.text.edits.TextEditGroup editGroup)`
Inserts the given node into the list at the end of the list.
`void`
`remove(ASTNode node, org.eclipse.text.edits.TextEditGroup editGroup)`
Removes the given node from its parent's list property in the rewriter.
`void`
`replace(ASTNode node, ASTNode replacement, org.eclipse.text.edits.TextEditGroup editGroup)`
Replaces the given node from its parent's list property in the rewriter.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Method Details

### getParent

public ASTNode getParent()
Returns the parent of the list for which this list rewriter was created.

Returns:
  the node that contains the list for which this list rewriter was created
Since:
  3.1
See Also:
  - `getLocationInParent()`

### getLocationInParent

public StructuralPropertyDescriptor getLocationInParent()
Returns the property of the parent node for which this list rewriter was created.

Returns:
  the property of the parent node for which this list rewriter was created
Since:
  3.1
See Also:
  - `getParent()`

### remove

public void remove(ASTNode node, org.eclipse.text.edits.TextEditGroup editGroup)
Removes the given node from its parent's list property in the rewriter. The node must be contained in the list. The AST itself is not actually modified in any way; rather, the rewriter just records a note that this node has been removed from this list.

Parameters:
  `node` - the node being removed. The node can either be an original node in this list or (since 3.4) a new node already inserted or used as replacement in this AST rewriter.
  `editGroup` - the edit group in which to collect the corresponding text edits, or `null` if ungrouped
Throws:
  `IllegalArgumentException` - if the node is null, or if the node is not part of this rewriter's AST, or if the described modification is invalid (not a member of this node's original list)

### getASTRewrite

public ASTRewrite getASTRewrite()
Returns the ASTRewrite instance from which this ListRewriter has been created from.

Returns:
  the parent AST Rewriter instance.
Since:
  3.1

### replace

public void replace(ASTNode node, ASTNode replacement, org.eclipse.text.edits.TextEditGroup editGroup)
Replaces the given node from its parent's list property in the rewriter. The node must be contained in the list. The replacement node must either be brand new (not part of the original AST) or a placeholder node (for example, one created by `ASTRewrite.createCopyTarget(ASTNode)`, `ASTRewrite.createMoveTarget(ASTNode)`, or `ASTRewrite.createStringPlaceholder(String, int)`). The AST itself is not actually modified in any way; rather, the rewriter just records a note that this node has been replaced in this list.

Parameters:
  `node` - the node being removed. The node can either be an original node in this list or (since 3.4) a new node already inserted or used as replacement in this AST rewriter.
  `replacement` - the replacement node, or `null` if no replacement
  `editGroup` - the edit group in which to collect the corresponding text edits, or `null` if ungrouped
Throws:
  `IllegalArgumentException` - if the node is null, or if the node is not part of this rewriter's AST, or if the replacement node is not a new node (or placeholder), or if the described modification is otherwise invalid (not a member of this node's original list)

### insertAfter

public void insertAfter(ASTNode node, ASTNode previousElement, org.eclipse.text.edits.TextEditGroup editGroup)
Inserts the given node into the list after the given element. The existing node `previousElement` must be in the list, either as an original or as a new node that has been inserted. The inserted node must either be brand new (not part of the original AST) or a placeholder node (for example, one created by `ASTRewrite.createCopyTarget(ASTNode)`, `ASTRewrite.createMoveTarget(ASTNode)`, or `ASTRewrite.createStringPlaceholder(String, int)`). The AST itself is not actually modified in any way; rather, the rewriter just records a note that this node has been inserted into the list.

Parameters:
  `node` - the node to insert
  `previousElement` - the element after which the given node is to be inserted
  `editGroup` - the edit group in which to collect the corresponding text edits, or `null` if ungrouped
Throws:
  `IllegalArgumentException` - if the node or element is null, or if the node is not part of this rewriter's AST, or if the inserted node is not a new node (or placeholder), or if `previousElement` is not a member of the list (original or new), or if the described modification is otherwise invalid

### insertBefore

public void insertBefore(ASTNode node, ASTNode nextElement, org.eclipse.text.edits.TextEditGroup editGroup)
Inserts the given node into the list before the given element. The existing node `nextElement` must be in the list, either as an original or as a new node that has been inserted. The inserted node must either be brand new (not part of the original AST) or a placeholder node (for example, one created by `ASTRewrite.createCopyTarget(ASTNode)`, `ASTRewrite.createMoveTarget(ASTNode)`, or `ASTRewrite.createStringPlaceholder(String, int)`). The AST itself is not actually modified in any way; rather, the rewriter just records a note that this node has been inserted into the list.

Parameters:
  `node` - the node to insert
  `nextElement` - the element before which the given node is to be inserted
  `editGroup` - the edit group in which to collect the corresponding text edits, or `null` if ungrouped
Throws:
  `IllegalArgumentException` - if the node or next element is null, or if the node is not part of this rewriter's AST, or if the inserted node is not a new node (or placeholder), or if `nextElement` is not a member of the list (original or new), or if the described modification is otherwise invalid

### insertFirst

public void insertFirst(ASTNode node, org.eclipse.text.edits.TextEditGroup editGroup)
Inserts the given node into the list at the start of the list. Equivalent to `insertAt(node, 0, editGroup)`.

Parameters:
  `node` - the node to insert
  `editGroup` - the edit group in which to collect the corresponding text edits, or `null` if ungrouped
Throws:
  `IllegalArgumentException` - if the node is null, or if the node is not part of this rewriter's AST, or if the inserted node is not a new node (or placeholder), or if the described modification is otherwise invalid (not a member of this node's original list)
See Also:
  - `insertAt(ASTNode, int, TextEditGroup)`

### insertLast

public void insertLast(ASTNode node, org.eclipse.text.edits.TextEditGroup editGroup)
Inserts the given node into the list at the end of the list. Equivalent to `insertAt(node, -1, editGroup)`.

Parameters:
  `node` - the node to insert
  `editGroup` - the edit group in which to collect the corresponding text edits, or `null` if ungrouped
Throws:
  `IllegalArgumentException` - if the node is null, or if the node is not part of this rewriter's AST, or if the inserted node is not a new node (or placeholder), or if the described modification is otherwise invalid (not a member of this node's original list)
See Also:
  - `insertAt(ASTNode, int, TextEditGroup)`

### insertAt

public void insertAt(ASTNode node, int index, org.eclipse.text.edits.TextEditGroup editGroup)
Inserts the given node into the list at the given index. The index corresponds to a combined list of original and new nodes; removed or replaced nodes are still in the combined list. The inserted node must either be brand new (not part of the original AST) or a placeholder node (for example, one created by `ASTRewrite.createCopyTarget(ASTNode)`, `ASTRewrite.createMoveTarget(ASTNode)`, or `ASTRewrite.createStringPlaceholder(String, int)`). The AST itself is not actually modified in any way; rather, the rewriter just records a note that this node has been inserted into the list.

Parameters:
  `node` - the node to insert
  `index` - insertion index in the combined list of original and inserted nodes; `-1` indicates insertion as the last element
  `editGroup` - the edit group in which to collect the corresponding text edits, or `null` if ungrouped
Throws:
  `IllegalArgumentException` - if the node is null, or if the node is not part of this rewriter's AST, or if the inserted node is not a new node (or placeholder), or if the described modification is otherwise invalid (not a member of this node's original list)
  `IndexOutOfBoundsException` - if the index is negative and not -1, or if it is larger than the size of the combined list

### createCopyTarget

public final ASTNode createCopyTarget(ASTNode first, ASTNode last)
Creates and returns a placeholder node for a true copy of a range of nodes of the current list. The placeholder node can either be inserted as new or used to replace an existing node. When the document is rewritten, a copy of the source code for the given node range is inserted into the output document at the position corresponding to the placeholder (indentation is adjusted).

Parameters:
  `first` - the node that starts the range
  `last` - the node that ends the range
Returns:
  the new placeholder node
Throws:
  `IllegalArgumentException` - An exception is thrown if the first or last node are `null`, if a node is not a child of the current list or if the first node is not before the last node. An `IllegalArgumentException` is also thrown if the copied range is overlapping with an other moved or copied range.

### createMoveTarget

public final ASTNode createMoveTarget(ASTNode first, ASTNode last)
Creates and returns a placeholder node for a move of a range of nodes of the current list. The placeholder node can either be inserted as new or used to replace an existing node. When the document is rewritten, a copy of the source code for the given node range is inserted into the output document at the position corresponding to the placeholder (indentation is adjusted).

Parameters:
  `first` - the node that starts the range
  `last` - the node that ends the range
Returns:
  the new placeholder node
Throws:
  `IllegalArgumentException` - An exception is thrown if the first or last node are `null`, if a node is not a child of the current list or if the first node is not before the last node. An `IllegalArgumentException` is also thrown if the moved range is overlapping with an other moved or copied range.
Since:
  3.1

### createMoveTarget

public final ASTNode createMoveTarget(ASTNode first, ASTNode last, ASTNode replacingNode, org.eclipse.text.edits.TextEditGroup editGroup)
Creates and returns a placeholder node for a move of a range of nodes of the current list. The moved nodes can optionally be replaced by a specified node. The placeholder node can either be inserted as new or used to replace an existing node. When the document is rewritten, a copy of the source code for the given node range is inserted into the output document at the position corresponding to the placeholder (indentation is adjusted).

Parameters:
  `first` - the node that starts the range
  `last` - the node that ends the range
  `replacingNode` - a node that is set at the location of the moved nodes or `null` to remove all nodes
  `editGroup` - the edit group in which to collect the corresponding text edits fro a replace, or `null` if ungrouped
Returns:
  the new placeholder node
Throws:
  `IllegalArgumentException` - An exception is thrown if the first or last node are `null`, if a node is not a child of the current list or if the first node is not before the last node. An `IllegalArgumentException` is also thrown if the moved range is overlapping with an other moved or copied range.
Since:
  3.1

### getOriginalList

public List getOriginalList()
Returns the original nodes in the list property managed by this rewriter. The returned list is unmodifiable.

Returns:
  a list of all original nodes in the list

### getRewrittenList

public List getRewrittenList()
Returns the nodes in the revised list property managed by this rewriter. The returned list is unmodifiable.

Returns:
  a list of all nodes in the list taking into account all the described changes