java - Automatic conversion of `for (int i=0;i<list.size();i++)` to enhanced for / for-each loops? Find enhance-able for loops? -
i refactored small part of large project grown on years have methods accept collection
list
required. this, had rewrite code à la
for (int i=0; < somelist.size(); i++) { sometype element = somelist.get(i); somemethod(element); // ... more code not using // or just: somemethod(/*other args */ somelist.get(i)); }
to
for (sometype element: somelist) { somemethod(element); // ... more code not using }
since there many more occurrences of pattern, wonder whether there way automatically convert old-style loop enhanced one, or @ least report loops can converted? (pmd:avoidarrayloops
- using sonar code analysis - similar arrays, few false positives)
i imagine regular expression might help, not consider myself well-versed enough handle part of establishing loop variable used in get
method of list size checked in termination expression.
if using eclipse:
- menu
window > preferences
,java > code style > clean up
. - edit profile
- tab
code style
control statements > convert loops enhanced
save (probably creating new profile) , close preferences
create java project sources
- context menu of source folder,
source > clean up...
Comments
Post a Comment