Hi all!
Why are getChildCount() and getChildAt() defined in class CompilationUnit? I would have thought they'd be declared in the interface ProgramElement since (to my understanding) it is a ProgramElement that represents a node in the AST.
Background: I'm implementing a SourceVisitor and want to collect all the AST-traversing in one method, which takes one argument of type ProgramElement and which calls itself recursively passing on each child node.
Thanks for answer!
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
getChildCount() and getChildAt() are actually defined in NonTerminalProgramElement. CompilationUnit is just one of the (many) implementors. "Identifier", "Public", "Private" and so on are terminals and, as such, do not have those methods.
One idea for your implementation: Use recoder.convenience.TreeWalker. You can combine it with a SourceVisitor like in the following:
TreeWalker tw = new TreeWalker(pe);
// alternative: ForestWalker, which takes a list of compilation units
SourceVisitor sv = new MySourceVisitor();
while (tw.next()) {
tw.getProgramElement().accept(sv);
}
Best regards,
Tobias
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi all!
Why are getChildCount() and getChildAt() defined in class CompilationUnit? I would have thought they'd be declared in the interface ProgramElement since (to my understanding) it is a ProgramElement that represents a node in the AST.
Background: I'm implementing a SourceVisitor and want to collect all the AST-traversing in one method, which takes one argument of type ProgramElement and which calls itself recursively passing on each child node.
Thanks for answer!
Hi,
getChildCount() and getChildAt() are actually defined in NonTerminalProgramElement. CompilationUnit is just one of the (many) implementors. "Identifier", "Public", "Private" and so on are terminals and, as such, do not have those methods.
One idea for your implementation: Use recoder.convenience.TreeWalker. You can combine it with a SourceVisitor like in the following:
TreeWalker tw = new TreeWalker(pe);
// alternative: ForestWalker, which takes a list of compilation units
SourceVisitor sv = new MySourceVisitor();
while (tw.next()) {
tw.getProgramElement().accept(sv);
}
Best regards,
Tobias