Bug 89432 - [quick assist] Transform enhanced for-loop to iterator
Summary: [quick assist] Transform enhanced for-loop to iterator
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 3.1   Edit
Hardware: PC Windows 2000
: P3 enhancement with 2 votes (vote)
Target Milestone: 3.8 M2   Edit
Assignee: Markus Keller CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2005-03-29 18:22 EST by Trevor Robinson CLA
Modified: 2011-09-14 13:15 EDT (History)
5 users (show)

See Also:


Attachments
Implementation and tests (37.68 KB, patch)
2011-09-09 15:08 EDT, Markus Keller CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Trevor Robinson CLA 2005-03-29 18:22:18 EST
I've often written iteration code using the enhanced for-loop initially, then
later discovered I needed access to the Iterator to remove an object. It would
be nice if Eclipse could do this simple (but verbose) transformation for me. For
example:

for (final Member member : list)
{
    //...
}

becomes:

for (final Iterator<Member> memberIter = list.iterator(); memberIter.hasNext(); )
{
    final Member member = memberIter.next();
    // ...
    // if (foo) memberIter.remove();
}

Similarly, the transformation could support arrays:

for (final Member member : array)
{
    // ...
}

becomes:

for (int memberIndex = 0; memberIndex < array.length; ++memberIndex)
{
    final Member member = array[memberIndex];
    // ...
    // if (foo) member[memberIndex] = null;
}

Another variation would be creating a ListIterator for lists, to allow adding
elements, or doing reverse iteration.
Comment 1 Olivier Thomann CLA 2005-03-29 22:00:01 EST
Move to JDT/UI
Comment 2 Dirk Baeumer CLA 2005-03-30 03:35:48 EST
Suggest to postpone > 3.1
Comment 3 Martin Aeschlimann CLA 2005-05-23 13:53:09 EDT
after 3.1
Comment 4 Martin Aeschlimann CLA 2005-10-11 04:40:53 EDT
reopen for 3.2
Comment 5 Dan Phifer CLA 2006-12-24 03:17:01 EST
This is also useful if you decide to move back to 1.4 compatibility. Ideally, this decision would be made early on, but if for some reason you have to revert, converting all of the enhanced loops by hand is error prone and tedious.  
Comment 6 Claudio Nieder CLA 2006-12-24 09:00:40 EST
> Ideally, this decision would be made early on,

Sometimes you can't decide, e.g. anybody wishing to use code I have written in Java 1.5 and made public, but needs to use it on Java 1.4 or needs to access the iterator or ... would be very happy to have such a transformation in eclipse.  
Comment 7 Martin Aeschlimann CLA 2007-11-07 06:15:10 EST
Setting to helpwanted as we have currently no resources for this feature
Comment 8 Nikolay Metchev CLA 2007-11-07 06:15:55 EST
Can I just point out a quite common use case for Java 5 code and above:
If you have an enhanced for-loop and wish to start using the index for something.
I would actually prefer it if the quickfix had 2 options: convert loop to iterator and convert loop to index access (which should only be available if you are looping through arrays or lists).
Comment 9 Markus Keller CLA 2011-09-09 15:08:05 EDT
Created attachment 203086 [details]
Implementation and tests
Comment 10 Markus Keller CLA 2011-09-09 15:09:07 EDT
Fixed in HEAD.
Comment 11 Deepak Azad CLA 2011-09-13 04:40:32 EDT
"Convert to Iterator for loop" sounds a bit awkward. How about "Convert to Iterator based for loop" or "Use Iterator in for loop" ?

"Convert to indexed for loop" can also be changed accordingly to "Use index variable in for loop".

Rest looks good.
Comment 12 Raksha Vasisht CLA 2011-09-13 04:58:14 EDT
Verified for 3.8 M2 with  I20110912-2126.
Comment 13 Paul Benedict CLA 2011-09-13 23:55:02 EDT
My 2 cents... I don't think "indexed" or "Iterator" sound very natural as an adjective. I would recommend:
* Convert to for loop having index
* Convert to for loop having Iterator
Comment 14 Markus Keller CLA 2011-09-14 09:05:13 EDT
We've already modified the wording a bit with bug 357490 and now use:

- Convert to indexed 'for' loop
- Convert to Iterator-based 'for' loop

The term "indexed" is also used in the JLS and e.g. in http://en.wikipedia.org/wiki/Index_%28computer_science%29 .


> * Convert to for loop having index
> * Convert to for loop having Iterator

I find "having" a bit clumsy. Maybe we could use "with" or "using" instead.

Nevertheless, the advantage of the current wording is that the differentiating word comes earlier, so the user has to read less to find the right quick assist e.g. here:

    public static void main(String[] args) {
        for (String string : Arrays.asList(args)) {
            System.out.println(string);
        }
    }
Comment 15 Trevor Robinson CLA 2011-09-14 13:15:41 EDT
(In reply to comment #14)
> We've already modified the wording a bit with bug 357490 and now use:
> 
> - Convert to indexed 'for' loop
> - Convert to Iterator-based 'for' loop

+1 on this wording; the others do seem clumsier.

And thanks for implementing this! It's impressive to see that a 6.5 year old feature request still gets noticed.