Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6473148

TreePath.iterator() should document the iteration order

    Details

    • Subcomponent:
    • Resolved In Build:
      b98
    • CPU:
      generic
    • OS:
      generic
    • Verification:
      Not verified

      Description

      As of b99, TreePath.iterator() is implemented as follows, and as you see it's guaranteed to cause NPE because curr is not initialized to this. Also, it fails to iterate the leaf node correctly:

          public Iterator<Tree> iterator() {
              return new Iterator<Tree>() {
                  public boolean hasNext() {
                      return curr.parent != null;
                  }
                  
                  public Tree next() {
                      curr = curr.parent;
                      return curr.leaf;
                  }
                  
                  public void remove() {
                      throw new UnsupportedOperationException();
                  }
                  
                  private TreePath curr;
              };
          }

      The correct implementation is:

          public Iterator<Tree> iterator() {
              return new Iterator<Tree>() {
                  public boolean hasNext() {
                      return curr != null;
                  }
                  
                  public Tree next() {
                      TreePath t = curr;
                      curr = curr.parent;
                      return t.leaf;
                  }
                  
                  public void remove() {
                      throw new UnsupportedOperationException();
                  }
                  
                  private TreePath curr = TreePath.this;
              };
          }

      The iterator method should also document in which order Trees are iterated. I find it more useful to be able to iterate from root to leaf, but no matter which way it goes, it should be documented.

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                vromero Vicente Arturo Romero Zaldivar
                Reporter:
                kkawagucsunw Kohsuke Kawaguchi (Inactive)
              • Votes:
                0 Vote for this issue
                Watchers:
                1 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:
                  Imported:
                  Indexed: