RFR: 8274755: Replace 'while' cycles with iterator with enhanced-for in jdk.jdi
Chris Plummer
cjplummer at openjdk.java.net
Tue Oct 5 20:11:07 UTC 2021
On Tue, 5 Oct 2021 19:48:06 GMT, Serguei Spitsyn <sspitsyn at openjdk.org> wrote:
>> There are few places in code where manual while loop is used with Iterator to iterate over Collection.
>> Instead of manual while cycles it's preferred to use enhanced-for cycle instead: it's less verbose, makes code easier to read and it's less error-prone.
>> It doesn't have any performance impact: java compiler generates similar code when compiling enhanced-for cycle.
>>
>> Similar cleanups:
>> * https://bugs.openjdk.java.net/browse/JDK-8258006
>> * https://bugs.openjdk.java.net/browse/JDK-8257912
>
> src/jdk.jdi/share/classes/com/sun/tools/jdi/EventRequestManagerImpl.java line 881:
>
>> 879: // copy the eventRequests to avoid ConcurrentModificationException
>> 880: for (EventRequest eventRequest : new ArrayList<>(eventRequests)) {
>> 881: ((EventRequestImpl) eventRequest).delete();
>
> I'm not sure the `eventRequest` needs to be wrapped into new `ArrayList`. I think, this should work without it.
It seems odd at first, but seems to have been done purposefully. Perhaps it has to do with the fact that delete() is being called, which does appear to also end up deleting the request from the list (see line 167). I don't think you want to be deleting from or adding to a collection that you are iterating over.
-------------
PR: https://git.openjdk.java.net/jdk/pull/5577
More information about the serviceability-dev
mailing list