Details
-
Bug
-
Resolution: Fixed
-
P4
-
6, 8
-
b98
-
generic
-
generic
-
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.
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
- relates to
-
JDK-6855236 Compiler Tree API TreePath class generates NullPointerException from Iterator
- Closed
-
JDK-6843077 JSR 308: Annotations on types
- Closed