org.aspectj.org.eclipse.jdt.core.dom
Class NodeFinder

java.lang.Object
  extended by org.aspectj.org.eclipse.jdt.core.dom.NodeFinder

public final class NodeFinder
extends java.lang.Object

For a given selection range, finds the covered node and the covering node.

Since:
3.5

Constructor Summary
NodeFinder(ASTNode root, int start, int length)
          Instantiate a new node finder using the given root node, the given start and the given length.
 
Method Summary
 ASTNode getCoveredNode()
          If the AST contains nodes whose range is equal to the selection, returns the innermost of those nodes.
 ASTNode getCoveringNode()
          Returns the innermost node that fully contains the selection.
static ASTNode perform(ASTNode root, int start, int length)
          Maps a selection to an ASTNode, where the selection is defined using a start and a length.
static ASTNode perform(ASTNode root, int start, int length, ITypeRoot source)
          Maps a selection to an ASTNode, where the selection is given by a start and a length.
static ASTNode perform(ASTNode root, ISourceRange range)
          Maps a selection to an ASTNode, where the selection is defined using a source range.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

NodeFinder

public NodeFinder(ASTNode root,
                  int start,
                  int length)
Instantiate a new node finder using the given root node, the given start and the given length.

Parameters:
root - the given root node
start - the given start
length - the given length
Method Detail

perform

public static ASTNode perform(ASTNode root,
                              int start,
                              int length)
Maps a selection to an ASTNode, where the selection is defined using a start and a length. The result node is determined as follows:

Parameters:
root - the root node from which the search starts
start - the start of the selection
length - the length of the selection
Returns:
the innermost node that exactly matches the selection, or the first node that contains the selection

perform

public static ASTNode perform(ASTNode root,
                              ISourceRange range)
Maps a selection to an ASTNode, where the selection is defined using a source range. Calls perform(root, range.getOffset(), range.getLength()).

Parameters:
root - the root node from which the search starts
range - the selection range
Returns:
the innermost node that exactly matches the selection, or the first node that contains the selection
See Also:
perform(ASTNode, int, int)

perform

public static ASTNode perform(ASTNode root,
                              int start,
                              int length,
                              ITypeRoot source)
                       throws JavaModelException
Maps a selection to an ASTNode, where the selection is given by a start and a length. The result node is determined as follows:

Parameters:
root - the root node from which the search starts
start - the start of the selection
length - the length of the selection
source - the source of the compilation unit
Returns:
the result node
Throws:
JavaModelException - if an error occurs in the Java model

getCoveredNode

public ASTNode getCoveredNode()
If the AST contains nodes whose range is equal to the selection, returns the innermost of those nodes. Otherwise, returns the first node in a preorder traversal of the AST, where the complete node range is covered by the selection.

Example: For a SimpleType whose name is a SimpleName and a selection that equals both nodes' range, the covered node is the SimpleName. But if the selection is expanded to include a whitespace before or after the SimpleType, then the covered node is the SimpleType.

Returns:
the covered node, or null if the selection is empty or too short to cover an entire node

getCoveringNode

public ASTNode getCoveringNode()
Returns the innermost node that fully contains the selection. A node also contains the zero-length selection on either end.

If more than one node covers the selection, the returned node is the last covering node found in a preorder traversal of the AST. This implies that for a zero-length selection between two adjacent sibling nodes, the node on the right is returned.

Returns:
the covering node