Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2000, 2010 IBM Corporation and others. All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse Public License v1.0 which accompanies this distribution, and is available at http://www.eclipse.org/legal/epl-v10.html Contributors: IBM Corporation - initial API and implementation /
 
 package org.eclipse.jdt.core.dom;
 
For a given range, finds the covered node and the covering node.

Since:
3.5
 
 public final class NodeFinder {
This class defines the actual visitor that finds the node.
 
 	private static class NodeFinderVisitor extends ASTVisitor {
 		private int fStart;
 		private int fEnd;
 		private ASTNode fCoveringNode;
 		private ASTNode fCoveredNode;
 
 		NodeFinderVisitor(int offsetint length) {
 			super(true); // include Javadoc tags
 			this.offset;
 			this.offset + length;
 		}
 
 		public boolean preVisit2(ASTNode node) {
 			int nodeStartnode.getStartPosition();
 			int nodeEndnodeStart + node.getLength();
 			if (nodeEnd < this. || this. < nodeStart) {
 				return false;
 			}
 			if (nodeStart <= this. && this. <= nodeEnd) {
 				this.node;
 			}
 			if (this. <= nodeStart && nodeEnd <= this.) {
 				if (this. == node) { // nodeStart == fStart && nodeEnd == fEnd
 					this.node;
 					return true// look further for node with same length as parent
 				} else if (this. == null) { // no better found
 					this.node;
 				}
 				return false;
 			}
 			return true;
 		}
Returns the covered node. If more than one nodes are covered by the selection, the returned node is first covered node found in a top-down traversal of the AST

Returns:
ASTNode
 
 		public ASTNode getCoveredNode() {
 			return this.;
 		}

Returns the covering node. If more than one nodes are covering the selection, the returned node is last covering node found in a top-down traversal of the AST

Returns:
ASTNode
 
 		public ASTNode getCoveringNode() {
 			return this.;
 		}
 	}
Maps a selection to a given ASTNode, where the selection is defined using a start and a length. The result node is determined as follows:
  • first the visitor tries to find a node with the exact start and length
  • if no such node exists then the node that encloses the range defined by start and length is returned.
  • if the length is zero then also nodes are considered where the node's start or end position matches start.
  • otherwise null is returned.

Parameters:
root the root node from which the search starts
start the given start
length the given length
Returns:
the found node
 
 	public static ASTNode perform(ASTNode rootint startint length) {
		NodeFinder finder = new NodeFinder(rootstartlength);
		ASTNode resultfinder.getCoveredNode();
		if (result == null || result.getStartPosition() != start || result.getLength() != length) {
			return finder.getCoveringNode();
		}
		return result;
	}

Maps a selection to a given ASTNode, where the selection is defined using a source range. It calls perform(root, range.getOffset(), range.getLength()).

Returns:
the result node
See also:
perform(org.eclipse.jdt.core.dom.ASTNode,int,int)
	public static ASTNode perform(ASTNode rootISourceRange range) {
		return perform(rootrange.getOffset(), range.getLength());
	}

Maps a selection to a given ASTNode, where the selection is given by a start and a length. The result node is determined as follows:
  • first the visitor tries to find a node that is covered by start and length where either start and length exactly matches the node or where the text covered before and after the node only consists of white spaces or comments.
  • if no such node exists then the node that encloses the range defined by start and length is returned.
  • if the length is zero then also nodes are considered where the node's start or end position matches start.
  • otherwise null is returned.

Parameters:
root the root node from which the search starts
start the given start
length the given length
source the source of the compilation unit
Returns:
the result node
Throws:
org.eclipse.jdt.core.JavaModelException if an error occurs in the Java model
	public static ASTNode perform(ASTNode rootint startint lengthITypeRoot sourcethrows JavaModelException {
		NodeFinder finder = new NodeFinder(rootstartlength);
		ASTNode resultfinder.getCoveredNode();
		if (result == null)
			return null;
		int nodeStartresult.getStartPosition();
		if (start <= nodeStart && ((nodeStart + result.getLength()) <= (start + length))) {
			IBuffer buffersource.getBuffer();
			if (buffer != null) {
				IScanner scanner= ToolFactory.createScanner(falsefalsefalsefalse);
				try {
					scanner.setSource(buffer.getText(startlength).toCharArray());
					int tokenscanner.getNextToken();
					if (token != .) {
						int tStartscanner.getCurrentTokenStartPosition();
						if (tStart == result.getStartPosition() - start) {
							scanner.resetTo(tStart + result.getLength(), length - 1);
							tokenscanner.getNextToken();
								return result;
						}
					}
catch (InvalidInputException e) {
					// ignore
catch (IndexOutOfBoundsException e) {
					// https://bugs.eclipse.org/bugs/show_bug.cgi?id=305001
					return null;
				}
			}
		}
		return finder.getCoveringNode();
	}
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
	public NodeFinder(ASTNode rootint startint length) {
		NodeFinderVisitor nodeFinderVisitor = new NodeFinderVisitor(startlength);
		root.accept(nodeFinderVisitor);
		this. = nodeFinderVisitor.getCoveredNode();
		this. = nodeFinderVisitor.getCoveringNode();
	}
Returns the covered node. If more than one nodes are covered by the selection, the returned node is first covered node found in a top-down traversal of the AST.

Returns:
the covered node
	public ASTNode getCoveredNode() {
		return this.;
	}

Returns the covering node. If more than one nodes are covering the selection, the returned node is last covering node found in a top-down traversal of the AST.

Returns:
the covering node
		return this.;
	}
New to GrepCode? Check out our FAQ X