RFE: Intention action - Use new for loop syntax

How about an intention action to replace usages of Iterator in a for loop with the new 1.5 syntax (assuming that iterator.remove() is not called within the body of the for loop).

Nick

10 comments
Comment actions Permalink

+1

--
WBR,
Tom


0
Comment actions Permalink

How about an intention action to replace usages of Iterator in a for
loop with the new 1.5 syntax (assuming that iterator.remove() is not
called within the body of the for loop).


I think there should also be an intention to convert from new for loop to
old for loop. (Sometimes I have Java 5.0 for loop, and then I realize I have
to remove from the collection, so I need to convert to old for loop.)


0
Comment actions Permalink


There is already an inspection with quickfix for for loops which can be replaced with for-each loops. Look in the error highlighting panel under "Verbose or redundant code constructs/For loop replaceable by for-each (J2SDK5.0 only)". Per JetBrains request, the inspection can only be enabled for projects which have language level set to 5.0. I don't see any value in a pure intention for this, beyond what's provided by the inspection-and-quickfix-intention. As an inspection, it can either be used interactively, or in batch mode (to change all of the appropriate for-loops in your project into for-each loops).

(And by the way, there's way more edge cases that need to be checked other than whether iterator.remove() is called. In terms of shear code complexity, this one is probably the trickiest inspection in IG.)

--Dave Griffith

0
Comment actions Permalink


Reasonable (and a lot easier than coding the other direction). I'll put it on the list.

--Dave Griffith

0
Comment actions Permalink

(And by the way, there's way more edge cases that need to be
checked other than whether iterator.remove() is called. In terms of
shear code complexity, this one is probably the trickiest inspection
in IG.)


I'm curious, why can't you just check the body of the loop control flow and
make sure it.next() is called for for all possible control flows, and that
no other it methods are called?


0
Comment actions Permalink

To be precise, have to I make sure that it.next() is called immediately on loop entry, never called again, that no other calls are made on the iterator, that the iterator is never assigned to, assigned from, or passed to any other method. The checks for converting array iteration loops are similar.

--Dave Griffith

0
Comment actions Permalink

To be precise, have to I make sure that it.next() is called
immediately on loop entry, never called again, that no other calls are
made on the iterator, that the iterator is never assigned to, assigned
from, or passed to any other method. The checks for converting array
iteration loops are similar.

--Dave Griffith


It seems like this inspection would benefit from a dataflow API like I mentioned
in my post about exposing the Constant Conditions & Exceptions plugin.


0
Comment actions Permalink

How about collecting getting all references to 'it', checking that there is
only one, and it really is it.next() in the beginning of the for loop? (why
in the beginning, btw? It seems that it is safe to convert anyway)
To me it sounds rather simple and doesn't require any DFA...
Eugene.

"Dave Griffith" <dave.griffith@cnn.com> wrote in message
news:31840982.1109885586991.JavaMail.itn@is.intellij.net...

To be precise, have to I make sure that it.next() is called immediately on

loop entry, never called again, that no other calls are made on the
iterator, that the iterator is never assigned to, assigned from, or passed
to any other method. The checks for converting array iteration loops are
similar.
>

--Dave Griffith



0
Comment actions Permalink

Dave Griffith wrote:

The checks for converting array iteration loops are similar.


And one check seems to be missing:
http://www.jetbrains.net/jira/browse/IDEA-760. Just goes to show how
difficult it is to get everything absolutely correct in all corner cases...

0
Comment actions Permalink

Eeep. Stupid coding error on my part. I've just submitted a fix.

--Dave Griffith

0

Please sign in to leave a comment.