Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » Positional Attributes of the ASTNodes.
Positional Attributes of the ASTNodes. [message #249147] Wed, 31 October 2007 08:37
Rahul Mishra is currently offline Rahul MishraFriend
Messages: 19
Registered: July 2009
Junior Member
Hi Folks,

How do I get the positional attributes of the various AST nodes? Here,
the positional attributes would mean line number, Starting column Position
and Ending Column Position. I have used the ASTVisitor class for
traversing through the tree and overridden all the visit and endVisit
Methods for the various AST nodes. Infact, when I run this application in
debug mode and set the breakpoint at line 45 and try to expand type
declaration, method declaration, field declarations, various Statements
and Expressions, etc. in the "Variables View" there are no positional
attributes except the lineEndTable attribute at the top of the tree.

Am I doing something incorrectly? Please suggest.

The code snippet is provided here:-

26 public class ASTMapper {
27 public static void main(String[] args)
28 {
29 File f = new File("C:\\Parser\\sample.java");
30 FileReader filerd = new FileReader(f);
31 char[] buf = new char[(int)f.length()];
32 filerd.read(buf);
33
34 ASTParser parser = ASTParser.newParser(AST.JLS3);
35 parser.setKind(ASTParser.K_COMPILATION_UNIT);
36 parser.setSource(buf); // set source
37 parser.setResolveBindings(true); // we need bindings later on
38 parser.setStatementsRecovery(true);
39
40 CompilationUnit cu = (CompilationUnit) parser.createAST(null);
41 TreeCollector tr=new TreeCollector(cu);
42 tr.getNumberOfNodes();
43 CompilationUnit f2= tr.getfRoot();
44 ASTNode ast=f2.getRoot();
45 System.out.println("Tree Collection Completed");//....breakpoint
set
46 }
47 }


class TreeCollector {

public static class NodeCounter extends GenericVisitor
{
public int numberOfNodes= 0;

protected boolean visitNode(ASTNode node)
{
numberOfNodes++;
return true;
}
}


private final CompilationUnit fRoot;

public TreeCollector(CompilationUnit root)
{
fRoot= root;
}

public int getSize()
{
return fRoot.subtreeBytes();
}

public CompilationUnit getfRoot()
{
return fRoot;
}


public int getNumberOfNodes()
{
NodeCounter counter= new NodeCounter();
fRoot.accept(counter);
return counter.numberOfNodes;
}
}

The GenericVisitorClass extends ASTVisitor and has the overridden methods
visit and endVisit for all the ASTnodes. visit is overridden in the
following manner :

class GenericVisitor extends ASTVisitor {

public GenericVisitor() {
super();
}

public GenericVisitor(boolean visitJavadocTags) {
super(visitJavadocTags);
}

//---- Hooks for subclasses
-------------------------------------------------

protected boolean visitNode(ASTNode node) {
return true;
}

protected void endVisitNode(ASTNode node) {
// do nothing
}

public boolean visit(AnonymousClassDeclaration node) {
return visitNode(node);
}
public boolean visit(ArrayAccess node) {
return visitNode(node);
}
public boolean visit(ArrayCreation node) {
return visitNode(node);
}
public boolean visit(ArrayInitializer node) {
return visitNode(node);
........
...Till the last node type i.e. Wilcard

public boolean visit(WildcardType node) {
return visitNode(node);
}
... this is the overloaded endVisit
public boolean endVisit(WildcardType node) {
return endVisitNode(node);
}
All the endVisit are overloaded by doing nothing.
Previous Topic:Eclipse/Ant properties bug?
Next Topic:Java Builder: what is considered a "resource"?
Goto Forum:
  


Current Time: Sat Jul 13 18:55:50 GMT 2024

Powered by FUDForum. Page generated in 0.03731 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top