<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body dir="auto">
These seem reasonable. My preference is for throwing over optional.  Size is probably the least problematic name.  <br>
<br>
<div dir="ltr">Sent from my iPad</div>
<div dir="ltr"><br>
<blockquote type="cite">On Aug 11, 2023, at 2:10 AM, Adam Sotona <adam.sotona@oracle.com> wrote:<br>
<br>
</blockquote>
</div>
<blockquote type="cite">
<div dir="ltr">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
 <style>@font-face { font-family: Wingdings; }
@font-face { font-family: "Cambria Math"; }
@font-face { font-family: Calibri; }
p.MsoNormal, li.MsoNormal, div.MsoNormal { margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif; }
a:link, span.MsoHyperlink { color: rgb(5, 99, 193); text-decoration: underline; }
code { font-family: "Courier New", serif; }
pre { margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New", serif; }
p.MsoListParagraph, li.MsoListParagraph, div.MsoListParagraph { margin: 0cm 0cm 0cm 36pt; font-size: 11pt; font-family: Calibri, sans-serif; }
span.HTMLPreformattedChar { font-family: "Courier New", serif; }
p.norm, li.norm, div.norm { margin-right: 0cm; margin-left: 0cm; font-size: 11pt; font-family: Calibri, sans-serif; }
span.literal { }
span.term { }
span.apple-converted-space { }
.MsoChpDefault { font-size: 10pt; font-family: Calibri, sans-serif; }
@page WordSection1 { size: 612pt 792pt; margin: 72pt; }
div.WordSection1 { page: WordSection1; }
ol { margin-bottom: 0cm; }
ul { margin-bottom: 0cm; }</style>
<div class="WordSection1">
<p class="MsoNormal"><span lang="EN-US">Hi,<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">I’ve noticed confusion in understanding (and inconsistency in implementations) of two ConstantPool methods:<o:p></o:p></span></p>
<p class="MsoNormal" style="background:white"><span style="font-size:10.0pt;font-family:"Courier New",serif;color:black;mso-ligatures:none;mso-fareast-language:EN-GB">   
</span><span style="font-size:10.0pt;font-family:"Courier New",serif;color:#969696;mso-ligatures:none;mso-fareast-language:EN-GB">/**</span><span style="font-size:10.0pt;font-family:"Courier New",serif;color:black;mso-ligatures:none;mso-fareast-language:EN-GB"><o:p></o:p></span></p>
<p class="MsoNormal" style="background:white"><span style="font-size:10.0pt;font-family:"Courier New",serif;color:#969696;mso-ligatures:none;mso-fareast-language:EN-GB">     *
<b>{@return</b></span><span style="font-size:10.0pt;font-family:"Courier New",serif;color:black;mso-ligatures:none;mso-fareast-language:EN-GB">
</span><b><span style="font-size:10.0pt;font-family:"Courier New",serif;color:#969696;mso-ligatures:none;mso-fareast-language:EN-GB">the</span></b><span style="font-size:10.0pt;font-family:"Courier New",serif;color:black;mso-ligatures:none;mso-fareast-language:EN-GB">
</span><b><span style="font-size:10.0pt;font-family:"Courier New",serif;color:#969696;mso-ligatures:none;mso-fareast-language:EN-GB">entry</span></b><span style="font-size:10.0pt;font-family:"Courier New",serif;color:black;mso-ligatures:none;mso-fareast-language:EN-GB">
</span><b><span style="font-size:10.0pt;font-family:"Courier New",serif;color:#969696;mso-ligatures:none;mso-fareast-language:EN-GB">at</span></b><span style="font-size:10.0pt;font-family:"Courier New",serif;color:black;mso-ligatures:none;mso-fareast-language:EN-GB">
</span><b><span style="font-size:10.0pt;font-family:"Courier New",serif;color:#969696;mso-ligatures:none;mso-fareast-language:EN-GB">the</span></b><span style="font-size:10.0pt;font-family:"Courier New",serif;color:black;mso-ligatures:none;mso-fareast-language:EN-GB">
</span><b><span style="font-size:10.0pt;font-family:"Courier New",serif;color:#969696;mso-ligatures:none;mso-fareast-language:EN-GB">specified</span></b><span style="font-size:10.0pt;font-family:"Courier New",serif;color:black;mso-ligatures:none;mso-fareast-language:EN-GB">
</span><b><span style="font-size:10.0pt;font-family:"Courier New",serif;color:#969696;mso-ligatures:none;mso-fareast-language:EN-GB">index</span></b><span style="font-size:10.0pt;font-family:"Courier New",serif;color:#969696;mso-ligatures:none;mso-fareast-language:EN-GB">}</span><span style="font-size:10.0pt;font-family:"Courier New",serif;color:black;mso-ligatures:none;mso-fareast-language:EN-GB"><o:p></o:p></span></p>
<p class="MsoNormal" style="background:white"><span style="font-size:10.0pt;font-family:"Courier New",serif;color:#969696;mso-ligatures:none;mso-fareast-language:EN-GB">     *</span><span style="font-size:10.0pt;font-family:"Courier New",serif;color:black;mso-ligatures:none;mso-fareast-language:EN-GB"><o:p></o:p></span></p>
<p class="MsoNormal" style="background:white"><span style="font-size:10.0pt;font-family:"Courier New",serif;color:#969696;mso-ligatures:none;mso-fareast-language:EN-GB">     *
<b>@param</b></span><span style="font-size:10.0pt;font-family:"Courier New",serif;color:black;mso-ligatures:none;mso-fareast-language:EN-GB"> index
</span><span style="font-size:10.0pt;font-family:"Courier New",serif;color:#969696;mso-ligatures:none;mso-fareast-language:EN-GB">the</span><span style="font-size:10.0pt;font-family:"Courier New",serif;color:black;mso-ligatures:none;mso-fareast-language:EN-GB">
</span><span style="font-size:10.0pt;font-family:"Courier New",serif;color:#969696;mso-ligatures:none;mso-fareast-language:EN-GB">index</span><span style="font-size:10.0pt;font-family:"Courier New",serif;color:black;mso-ligatures:none;mso-fareast-language:EN-GB">
</span><span style="font-size:10.0pt;font-family:"Courier New",serif;color:#969696;mso-ligatures:none;mso-fareast-language:EN-GB">within</span><span style="font-size:10.0pt;font-family:"Courier New",serif;color:black;mso-ligatures:none;mso-fareast-language:EN-GB">
</span><span style="font-size:10.0pt;font-family:"Courier New",serif;color:#969696;mso-ligatures:none;mso-fareast-language:EN-GB">the</span><span style="font-size:10.0pt;font-family:"Courier New",serif;color:black;mso-ligatures:none;mso-fareast-language:EN-GB">
</span><span style="font-size:10.0pt;font-family:"Courier New",serif;color:#969696;mso-ligatures:none;mso-fareast-language:EN-GB">pool</span><span style="font-size:10.0pt;font-family:"Courier New",serif;color:black;mso-ligatures:none;mso-fareast-language:EN-GB">
</span><span style="font-size:10.0pt;font-family:"Courier New",serif;color:#969696;mso-ligatures:none;mso-fareast-language:EN-GB">of</span><span style="font-size:10.0pt;font-family:"Courier New",serif;color:black;mso-ligatures:none;mso-fareast-language:EN-GB">
</span><span style="font-size:10.0pt;font-family:"Courier New",serif;color:#969696;mso-ligatures:none;mso-fareast-language:EN-GB">the</span><span style="font-size:10.0pt;font-family:"Courier New",serif;color:black;mso-ligatures:none;mso-fareast-language:EN-GB">
</span><span style="font-size:10.0pt;font-family:"Courier New",serif;color:#969696;mso-ligatures:none;mso-fareast-language:EN-GB">desired</span><span style="font-size:10.0pt;font-family:"Courier New",serif;color:black;mso-ligatures:none;mso-fareast-language:EN-GB">
</span><span style="font-size:10.0pt;font-family:"Courier New",serif;color:#969696;mso-ligatures:none;mso-fareast-language:EN-GB">entry</span><span style="font-size:10.0pt;font-family:"Courier New",serif;color:black;mso-ligatures:none;mso-fareast-language:EN-GB"><o:p></o:p></span></p>
<p class="MsoNormal" style="background:white"><span style="font-size:10.0pt;font-family:"Courier New",serif;color:black;mso-ligatures:none;mso-fareast-language:EN-GB">    
</span><span style="font-size:10.0pt;font-family:"Courier New",serif;color:#969696;mso-ligatures:none;mso-fareast-language:EN-GB">*/</span><span style="font-size:10.0pt;font-family:"Courier New",serif;color:black;mso-ligatures:none;mso-fareast-language:EN-GB"><o:p></o:p></span></p>
<p class="MsoNormal" style="background:white"><span style="font-size:10.0pt;font-family:"Courier New",serif;color:black;mso-ligatures:none;mso-fareast-language:EN-GB">    PoolEntry
<b>entryByIndex</b>(</span><span style="font-size:10.0pt;font-family:"Courier New",serif;color:#336BDD;mso-ligatures:none;mso-fareast-language:EN-GB">int</span><span style="font-size:10.0pt;font-family:"Courier New",serif;color:black;mso-ligatures:none;mso-fareast-language:EN-GB">
 index);<o:p></o:p></span></p>
<p class="MsoNormal" style="background:white"><span style="font-size:10.0pt;font-family:"Courier New",serif;color:black;mso-ligatures:none;mso-fareast-language:EN-GB"><o:p> </o:p></span></p>
<p class="MsoNormal" style="background:white"><span style="font-size:10.0pt;font-family:"Courier New",serif;color:black;mso-ligatures:none;mso-fareast-language:EN-GB">   
</span><span style="font-size:10.0pt;font-family:"Courier New",serif;color:#969696;mso-ligatures:none;mso-fareast-language:EN-GB">/**</span><span style="font-size:10.0pt;font-family:"Courier New",serif;color:black;mso-ligatures:none;mso-fareast-language:EN-GB"><o:p></o:p></span></p>
<p class="MsoNormal" style="background:white"><span style="font-size:10.0pt;font-family:"Courier New",serif;color:#969696;mso-ligatures:none;mso-fareast-language:EN-GB">     * {<b>@return</b></span><span style="font-size:10.0pt;font-family:"Courier New",serif;color:black;mso-ligatures:none;mso-fareast-language:EN-GB">
</span><span style="font-size:10.0pt;font-family:"Courier New",serif;color:#969696;mso-ligatures:none;mso-fareast-language:EN-GB">the</span><span style="font-size:10.0pt;font-family:"Courier New",serif;color:black;mso-ligatures:none;mso-fareast-language:EN-GB">
</span><span style="font-size:10.0pt;font-family:"Courier New",serif;color:#969696;mso-ligatures:none;mso-fareast-language:EN-GB">number</span><span style="font-size:10.0pt;font-family:"Courier New",serif;color:black;mso-ligatures:none;mso-fareast-language:EN-GB">
</span><span style="font-size:10.0pt;font-family:"Courier New",serif;color:#969696;mso-ligatures:none;mso-fareast-language:EN-GB">of</span><span style="font-size:10.0pt;font-family:"Courier New",serif;color:black;mso-ligatures:none;mso-fareast-language:EN-GB">
</span><span style="font-size:10.0pt;font-family:"Courier New",serif;color:#969696;mso-ligatures:none;mso-fareast-language:EN-GB">entries</span><span style="font-size:10.0pt;font-family:"Courier New",serif;color:black;mso-ligatures:none;mso-fareast-language:EN-GB">
</span><span style="font-size:10.0pt;font-family:"Courier New",serif;color:#969696;mso-ligatures:none;mso-fareast-language:EN-GB">in</span><span style="font-size:10.0pt;font-family:"Courier New",serif;color:black;mso-ligatures:none;mso-fareast-language:EN-GB">
</span><span style="font-size:10.0pt;font-family:"Courier New",serif;color:#969696;mso-ligatures:none;mso-fareast-language:EN-GB">the</span><span style="font-size:10.0pt;font-family:"Courier New",serif;color:black;mso-ligatures:none;mso-fareast-language:EN-GB">
</span><span style="font-size:10.0pt;font-family:"Courier New",serif;color:#969696;mso-ligatures:none;mso-fareast-language:EN-GB">constant</span><span style="font-size:10.0pt;font-family:"Courier New",serif;color:black;mso-ligatures:none;mso-fareast-language:EN-GB">
</span><span style="font-size:10.0pt;font-family:"Courier New",serif;color:#969696;mso-ligatures:none;mso-fareast-language:EN-GB">pool}</span><span style="font-size:10.0pt;font-family:"Courier New",serif;color:black;mso-ligatures:none;mso-fareast-language:EN-GB"><o:p></o:p></span></p>
<p class="MsoNormal" style="background:white"><span style="font-size:10.0pt;font-family:"Courier New",serif;color:black;mso-ligatures:none;mso-fareast-language:EN-GB">    
</span><span style="font-size:10.0pt;font-family:"Courier New",serif;color:#969696;mso-ligatures:none;mso-fareast-language:EN-GB">*/</span><span style="font-size:10.0pt;font-family:"Courier New",serif;color:black;mso-ligatures:none;mso-fareast-language:EN-GB"><o:p></o:p></span></p>
<p class="MsoNormal" style="background:white"><span style="font-size:10.0pt;font-family:"Courier New",serif;color:black;mso-ligatures:none;mso-fareast-language:EN-GB">   
</span><span style="font-size:10.0pt;font-family:"Courier New",serif;color:#336BDD;mso-ligatures:none;mso-fareast-language:EN-GB">int</span><span style="font-size:10.0pt;font-family:"Courier New",serif;color:black;mso-ligatures:none;mso-fareast-language:EN-GB">
<b>entryCount</b>();<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">Intuitive understanding of the above methods is that user can iterate over entries incrementing index by one up-to the entryCount and get entry for each index from the range.<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">However the reality is that the methods reflects JVMS 4.1:<o:p></o:p></span></p>
<p class="MsoNormal"><span class="term"><span style="font-size:9.0pt;font-family:"Arial",sans-serif;color:black"><o:p> </o:p></span></span></p>
<p class="MsoNormal"><span class="term"><span style="font-size:9.0pt;font-family:"Arial",sans-serif;color:black">constant_pool_count</span></span></p>
<p class="norm" style="margin-left:36.0pt"><a name="jvms-4.1-200-C"></a><span style="font-size:9.0pt;font-family:"Arial",sans-serif;color:black">The value of the<span class="apple-converted-space"> </span></span><code><span style="font-size:10.0pt;color:black">constant_pool_count</span></code><span class="apple-converted-space"><span style="font-size:9.0pt;font-family:"Arial",sans-serif;color:black"> </span></span><span style="font-size:9.0pt;font-family:"Arial",sans-serif;color:black">item
 is equal to the number of entries in the<span class="apple-converted-space"> </span></span><code><span style="font-size:10.0pt;color:black">constant_pool</span></code><span class="apple-converted-space"><span style="font-size:9.0pt;font-family:"Arial",sans-serif;color:black"> </span></span><span style="font-size:9.0pt;font-family:"Arial",sans-serif;color:black">table
 plus one. A<span class="apple-converted-space"> </span></span><code><span style="font-size:10.0pt;color:black">constant_pool</span></code><span class="apple-converted-space"><span style="font-size:9.0pt;font-family:"Arial",sans-serif;color:black"> </span></span><span style="font-size:9.0pt;font-family:"Arial",sans-serif;color:black">index
 is considered valid if it is <b>greater than zero</b> and less than<span class="apple-converted-space"> </span></span><code><span style="font-size:10.0pt;color:black">constant_pool_count</span></code><span style="font-size:9.0pt;font-family:"Arial",sans-serif;color:black">,
<b>with the exception for constants of type<span class="apple-converted-space"> </span></b></span><code><b><span style="font-size:10.0pt;color:black">long</span></b></code><span class="apple-converted-space"><b><span style="font-size:9.0pt;font-family:"Arial",sans-serif;color:black"> </span></b></span><b><span style="font-size:9.0pt;font-family:"Arial",sans-serif;color:black">and<span class="apple-converted-space"> </span></span></b><code><b><span style="font-size:10.0pt;color:black">double</span></b></code><span class="apple-converted-space"><span style="font-size:9.0pt;font-family:"Arial",sans-serif;color:black"> </span></span><span style="font-size:9.0pt;font-family:"Arial",sans-serif;color:black">noted
 in<span class="apple-converted-space"> </span><a href="https://docs.oracle.com/javase/specs/jvms/se17/html/jvms-4.html#jvms-4.4.5" title="4.4.5. The CONSTANT_Long_info and CONSTANT_Double_info Structures">§4.4.5</a>.<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">Following user code cause more or less confusion:<o:p></o:p></span></p>
<pre style="background:white"><span class="literal"><span style="color:#336BDD"><o:p> </o:p></span></span></pre>
<pre style="background:white"><span class="literal"><span style="color:#336BDD">for</span></span><span style="color:black"> (</span><span class="literal"><span style="color:#336BDD">int</span></span><span style="color:black"> i = 0; i < cp.entryCount(); i++) cp.entryByIndex(i);<o:p></o:p></span></pre>
<ul style="margin-top:0cm" type="disc">
<li class="MsoListParagraph" style="margin-left:0cm;mso-list:l0 level1 lfo3"><span lang="EN-US">Fails immediately with ConstantPoolException at index 0<o:p></o:p></span></li></ul>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<pre style="background:white"><span class="literal"><span style="color:#336BDD">for</span></span><span style="color:black"> (</span><span class="literal"><span style="color:#336BDD">int</span></span><span style="color:black"> i = </span><span lang="EN-US" style="color:black">1</span><span style="color:black">; i < cp.entryCount(); i++) cp.entryByIndex(i);<o:p></o:p></span></pre>
<ul style="margin-top:0cm" type="disc">
<li class="MsoListParagraph" style="margin-left:0cm;mso-list:l0 level1 lfo3"><span lang="EN-US">May fail for constant pools containing long or double entries (double-slot entries), however it may not fail if the tag at the invalid offset imitates a valid entry
 (this is a bug) or it may return null when SplitConstantPool implementation is involved (inconsistency in implementations).<o:p></o:p></span></li></ul>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">So the only valid (however not very intuitive) iteration over all entries should look like this:<o:p></o:p></span></p>
<pre style="background:white"><span class="literal"><span style="color:#336BDD"><o:p> </o:p></span></span></pre>
<pre style="background:white"><span class="literal"><span style="color:#336BDD">for</span></span><span style="color:black"> (</span><span class="literal"><span style="color:#336BDD">int</span></span><span style="color:black"> i = </span><span lang="EN-US" style="color:black">1</span><span style="color:black">; i < cp.entryCount(); i += cp.entryByIndex(i).width())<o:p></o:p></span></pre>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">I propose following changes to ConstantPool:<o:p></o:p></span></p>
<ul style="margin-top:0cm" type="disc">
<li class="MsoListParagraph" style="margin-left:0cm;mso-list:l0 level1 lfo3"><span lang="EN-US">Fix all implementations of PoolEntry ConstantPool::entryByIndex(int) to always throw ConstantPoolException when the index is invalid<br>
or change the method signature to Optional<PoolEntry> ConstantPool::entryByIndex(int) and explain it in the Javadoc<o:p></o:p></span></li><li class="MsoListParagraph" style="margin-left:0cm;mso-list:l0 level1 lfo3"><span lang="EN-US">Rename ConstantPool::entryCount to slotsCount or size or width and explain it in the Javadoc<o:p></o:p></span></li><li class="MsoListParagraph" style="margin-left:0cm;mso-list:l0 level1 lfo3"><span lang="EN-US">Make ConstantPool extends Iterable<PoolEntry> so user does not need to understand CP internals to iterate over its entries<o:p></o:p></span></li></ul>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">Thanks,<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">Adam<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
</div>
</div>
</blockquote>
</body>
</html>