RFR: 8315678: Classfile API ConstantPool::entryCount and ConstantPool::entryByIndex methods are confusing [v2]

Adam Sotona asotona at openjdk.org
Wed Sep 6 14:17:41 UTC 2023


On Wed, 6 Sep 2023 08:10:12 GMT, ExE Boss <duke at openjdk.org> wrote:

>> Adam Sotona has updated the pull request incrementally with one additional commit since the last revision:
>> 
>>   fixed tests
>
> src/java.base/share/classes/jdk/internal/classfile/constantpool/ConstantPool.java line 76:
> 
>> 74:             }
>> 75:         };
>> 76:     }
> 
> This iterator isn’t entirely correct, because if the constant pool is modified between the call to `hasNext()` and `next()`, then it may throw `NoSuchElementException`.
> 
> Additionally, `hasNext()` can go from returning `false` to returning `true`.

The size of the pool can never go down so the iterator should behave correctly even when underlying pool is modified.

Primary purpose of the iterator is to replace not very intuitive (and frequently coded wrong - even in our own tests) for loop iteration:

for(int index = 1; index < pool.size(); index += pool.entryByIndex(index).width())

-------------

PR Review Comment: https://git.openjdk.org/jdk/pull/15567#discussion_r1317355090


More information about the core-libs-dev mailing list