Menu

getChildCount() defined in CompilationUnit?

Developers
widheg
2009-08-13
2012-10-08
  • widheg

    widheg - 2009-08-13

    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!

     
    • Tobias Gutzmann

      Tobias Gutzmann - 2009-08-13

      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

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.