JDK-6473148 : TreePath.iterator() should document the iteration order
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 6,8
  • Priority: P4
  • Status: Closed
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2006-09-20
  • Updated: 2013-07-11
  • Resolved: 2013-06-28
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 8
8 b98Fixed
Related Reports
Relates :  
Relates :  
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.

Comments
EVALUATION The NPE was fixed as part of 6843077. However, the issue about documenting the iteration order is well taken.
19-01-2010