Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 260626 - [clean up] "Convert for loops to enhanced" clean up leaves iterator references
Summary: [clean up] "Convert for loops to enhanced" clean up leaves iterator references
Status: RESOLVED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 3.4   Edit
Hardware: All All
: P3 normal (vote)
Target Milestone: 3.6 M7   Edit
Assignee: Markus Keller CLA
QA Contact:
URL:
Whiteboard:
Keywords:
: 307150 (view as bug list)
Depends on:
Blocks:
 
Reported: 2009-01-10 16:09 EST by Hollis Waite CLA
Modified: 2010-03-29 06:57 EDT (History)
4 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Hollis Waite CLA 2009-01-10 16:09:01 EST
Build ID: I20080617-2000

Steps To Reproduce:
import java.util.Collections;
import java.util.Iterator;

public class Test {
  void foo() {
    for (
      Iterator iter = Collections.emptySet().iterator();
      iter.hasNext();
    ) {
      iter.next().toString();
      String.valueOf(iter.hasNext() ? "x" : "y");
    }
  }
}

becomes

import java.util.Collections;

public class Test {
  void foo() {
    for (Object element : Collections.emptySet()) {
      element.toString();
      String.valueOf(iter.hasNext() ? "x" : "y");
    }
  }
}

More information:
The cleanup seems to update code when there is only a single reference to iterator.  Things go bad when subsequent refs are in ternary conditions.
Comment 1 Markus Keller CLA 2010-03-29 06:45:55 EDT
*** Bug 307150 has been marked as a duplicate of this bug. ***
Comment 2 Markus Keller CLA 2010-03-29 06:57:33 EDT
Fixed in ConvertIterableLoopOperation. The problem occurred when iter.*() is called inside another method invocation expression.