Community
Participate
Working Groups
Given the two following classes: ----- package test; import java.util.Iterator; public class Tree<E> { private TreeNode<E> root; public void doSomething() { for (TreeNode<E> child : root.children()) { // root.children() should work?? } } public void doSomethingElse() { for (Iterator<TreeNode<E>> it = root.children().iterator(); it.hasNext();) { // this also should work } } } ----- package test; import java.util.Set; public class TreeNode<E> { private Set<TreeNode<E>> children; public Set<TreeNode<E>> children() { return children; } } ----- 1. The "for (TreeNode<E>..." line in Tree#doSomething() should be valid. Instead, it generates "Type mismatch: cannot convert from element type test.TreeNode<E> to test.TreeNode<E>". 2. The "for (Iterator..." line in Tree#doSomethingElse() should also be good, but it generates "Type mismatch: cannot convert from Iterator<TreeNode<E>> to Iterator<TreeNode<E>>" If either loop is replaced with: for (Iterator it = root.children().iterator(); it.hasNext();) { TreeNode<E> child = (TreeNode<E>)it.next(); // ... } then everything is fine (minus the warning generated by the cast). FURTHERMORE. (Yes, there's even more! :-) ) The errors in 1. and 2. do *not* display on the Problems view unless a Project->Clean is performed, and then they disappear the first time that the class is re-saved.
Same issue as in bug 77052 which has simpler testcase. Still added regression test: GenericTypeTest#test324 for this very scenario. *** This bug has been marked as a duplicate of 77052 ***