<div dir="ltr"><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">OK, that makes sense. I can use the option. I still think the generics usage isn't correct, but if all usages of the ad-hoc attributes are internal then it's not a problem for users and if implementers are happy with it then it's fine.</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, Aug 29, 2023 at 8:27 AM Adam Sotona <<a href="mailto:adam.sotona@oracle.com">adam.sotona@oracle.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div class="msg-26172473517253399">
<div lang="en-CZ" style="overflow-wrap: break-word;">
<div class="m_-5446455886717759534WordSection1">
<p class="MsoNormal"><span lang="EN-US" style="font-size:11pt">Unfortunately jdk.internal.classfile.impl.StackMapGenerator is not exposed in the Classfile API and it is not designed to be called by user.<u></u><u></u></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11pt">The right way to drive stack maps generation is through Classfile.StackMapsOption.<u></u><u></u></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11pt">Where the default StackMapsOption.STACK_MAPS_WHEN_REQUIRED option will handle majority of use cases with maximum performance:<u></u><u></u></span></p>
<ul style="margin-top:0cm" type="disc">
<li class="m_-5446455886717759534MsoListParagraph" style="margin-left:0cm"><span lang="EN-US" style="font-size:11pt">transformations of valid classes keeping the original stack maps when the code is unchanged<u></u><u></u></span></li><li class="m_-5446455886717759534MsoListParagraph" style="margin-left:0cm"><span lang="EN-US" style="font-size:11pt">generation of stack maps for new methods<u></u><u></u></span></li><li class="m_-5446455886717759534MsoListParagraph" style="margin-left:0cm"><span lang="EN-US" style="font-size:11pt">however it does not fix invalid class files at the input of the transformation<u></u><u></u></span></li></ul>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11pt">StackMapsOption.GENERATE_STACK_MAPS option will force SM generation, for the cases like:<u></u><u></u></span></p>
<ul style="margin-top:0cm" type="disc">
<li class="m_-5446455886717759534MsoListParagraph" style="margin-left:0cm"><span lang="EN-US" style="font-size:11pt">stack maps should be generated even when not mandatory by JVMS<u></u><u></u></span></li><li class="m_-5446455886717759534MsoListParagraph" style="margin-left:0cm"><span lang="EN-US" style="font-size:11pt">source classes of the transformation are missing stack maps and Classfile API is used to fix it<u></u><u></u></span></li></ul>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11pt"><u></u> <u></u></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11pt">There are several specific aspects of the stack maps generation:<u></u><u></u></span></p>
<ol style="margin-top:0cm" start="1" type="1">
<li class="m_-5446455886717759534MsoListParagraph" style="margin-left:0cm"><span lang="EN-US" style="font-size:11pt">Non-presence of </span><span style="font-size:11pt;font-family:Arial,sans-serif">StackMapAttribute</span><span style="font-size:11pt;font-family:Arial,sans-serif">
<span lang="EN-US">is a valid state for many methods, so this bare information is not enough to trigger the generator.</span></span><span lang="EN-US" style="font-size:11pt"><u></u><u></u></span></li><li class="m_-5446455886717759534MsoListParagraph" style="margin-left:0cm"><span lang="EN-US" style="font-size:11pt;font-family:Arial,sans-serif">StackMapGenerator is internally invoked at the last stage of the code generation, where the bytecode array
is fully constructed. </span><span lang="EN-US" style="font-size:11pt"><u></u><u></u></span></li></ol>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11pt"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11pt"><u></u> <u></u></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11pt">What specific use case are you trying to solve?<u></u><u></u></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11pt"><u></u> <u></u></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11pt">Thanks,<u></u><u></u></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11pt">Adam<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11pt"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11pt"><u></u> <u></u></span></p>
<div id="m_-5446455886717759534mail-editor-reference-message-container">
<div>
<div style="border-right:none;border-bottom:none;border-left:none;border-top:1pt solid rgb(181,196,223);padding:3pt 0cm 0cm">
<p class="MsoNormal" style="margin-bottom:12pt"><b><span style="font-size:12pt;color:black">From:
</span></b><span style="font-size:12pt;color:black">classfile-api-dev <<a href="mailto:classfile-api-dev-retn@openjdk.org" target="_blank">classfile-api-dev-retn@openjdk.org</a>> on behalf of David Lloyd <<a href="mailto:david.lloyd@redhat.com" target="_blank">david.lloyd@redhat.com</a>><br>
<b>Date: </b>Monday, 28 August 2023 18:51<br>
<b>To: </b><a href="mailto:classfile-api-dev@openjdk.org" target="_blank">classfile-api-dev@openjdk.org</a> <<a href="mailto:classfile-api-dev@openjdk.org" target="_blank">classfile-api-dev@openjdk.org</a>><br>
<b>Subject: </b>Minor issue with stack map generator and generics<u></u><u></u></span></p>
</div>
<div>
<div>
<p class="MsoNormal"><span style="font-size:11pt;font-family:Arial,sans-serif">I've been doing more experimenting with this API and ran across a minor issue. The way the stack map generator API is presently structured, it seems difficult to "get-or-generate".<u></u><u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:11pt;font-family:Arial,sans-serif"><u></u> <u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:11pt;font-family:Arial,sans-serif">For example, I thought to find the stack map for a method, or otherwise generate it if it did not exist, so my code looked something like this (structured for readability):<u></u><u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:11pt;font-family:Arial,sans-serif"><u></u> <u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:11pt;font-family:Arial,sans-serif"> MethodModel mm = ....;<u></u><u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:11pt;font-family:Arial,sans-serif"> Optional<StackMapAttribute> optAttr = mm.findAttribute(Attributes.STACK_MAP_TABLE);<u></u><u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:11pt;font-family:Arial,sans-serif"> StackMapAttribute sma = optAttr.orElseGet(() -> new StackMapGenerator(...).stackMapTableAttribute());<u></u><u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:11pt;font-family:Arial,sans-serif"><u></u> <u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:11pt;font-family:Arial,sans-serif">But this fails because `StackMapGenerator.stackMapTableAttribute()` returns an `Attribute<? extends StackMapAttribute>` instead of just `StackMapAttribute`. This method returns
an anonymous subclass of `AdHocAttribute`; could it instead be changed to an inner class which also implements `StackMapAttribute`? I think this might be more correct as well because the type argument of `Attribute` seems like it was intended to be a self-type,
and if so, these direct anonymous subclasses (and there are a few of them) seem to violate that intention.<u></u><u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:11pt"><u></u> <u></u></span></p>
</div>
<p class="MsoNormal"><span class="m_-5446455886717759534gmailsignatureprefix"><span style="font-size:11pt">--
</span></span><span style="font-size:11pt"><u></u><u></u></span></p>
<div>
<div>
<p class="MsoNormal"><span style="font-size:11pt">- DML • he/him<u></u><u></u></span></p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div></blockquote></div><br clear="all"><div><br></div><span class="gmail_signature_prefix">-- </span><br><div dir="ltr" class="gmail_signature"><div dir="ltr">- DML • he/him<br></div></div>