<div dir="ltr"><div>Hei Adam,</div><div><br></div><div>I agree that most use cases should be covered by today's API. I already use the former in my bridge implementation to use ASM with the Class File API. When it comes to LineNumberTableAttribute, I do not understand how it can be useful to resolve the remaining 1%, though. LineNumberInfo is built around a "startPc". Without an opportunity to resolve a BCI for a generated method, I do not understand how I can create a custom line number table attribute using the Class File API. The CodeBuilder interface does not expose the created CodeAttribute, which would be needed to resolve a label position to the needed BCI that the LineNumberInfo class requires. At a minimum, I would need CodeBuilder to resolve the current byte code index, but in the end this would only be used to build a custom label mechanism. I argue that this should already be handled by the API. The same goes for reading. I can of course count the size of each encountered instruction and keep a custom mapping of attributed BCIs to invoke a custom callback whenever a relevant offset is discovered, but this is also what the Label API is used for, and I think the API should handle this mapping.<br></div><div><br></div><div>I find the same limitation with LocalVariableTypeInfo, LocalVariableInfo and CharacterRangeInfo. I also believe this was considered for type annotations where TypeAnnotation.TargetInfo.OffsetTarget resolves a label, and not a BCI. I expected consistent handling for all attributes other than type annotations, including custom attributes.<br></div><div><br></div><div>Best regards, Rafael<br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Am Mo., 11. Nov. 2024 um 09:17 Uhr schrieb Adam Sotona <<a href="mailto:adam.sotona@oracle.com">adam.sotona@oracle.com</a>>:<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-3959551913766766305">
<div lang="EN-US" style="overflow-wrap: break-word;">
<div class="m_-3849973303882908695WordSection1">
<p class="MsoNormal"><span style="font-size:11pt">Hi Rafael,<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">LineNumber, LocalVariable and LocalVariableType (as CodeElements) are integral parts of the Class-File API and they should serve well to 99% of user use cases.<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">In addition, LineNumberTableAttribute, LocalVariableTableAttribute, LocalVariableTypeTableAttribute and StackMapTableAttribute are available in “raw” forms to support strongly requested specific corner cases.<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">This is clear request to support custom code-level attributes with references to bytecode offsets.<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11pt">I agree with Chen that JDK-8341274 should be sufficient to resolve it.<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">Thank you,<u></u><u></u></span></p>
<p class="MsoNormal"><span 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>
<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_-3849973303882908695mail-editor-reference-message-container">
<div>
<div>
<div style="border-width:1pt medium medium;border-style:solid none none;border-color:rgb(181,196,223) currentcolor currentcolor;padding:3pt 0in 0in">
<p class="MsoNormal" style="margin-bottom:12pt"><b><span style="color:black">From:
</span></b><span style="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 Chen Liang <<a href="mailto:chen.l.liang@oracle.com" target="_blank">chen.l.liang@oracle.com</a>><br>
<b>Date: </b>Monday, 11 November 2024 at 0:28<br>
<b>To: </b>Rafael Winterhalter <<a href="mailto:rafael.wth@gmail.com" target="_blank">rafael.wth@gmail.com</a>>, classfile-api-dev <<a href="mailto:classfile-api-dev@openjdk.org" target="_blank">classfile-api-dev@openjdk.org</a>><br>
<b>Subject: </b>Re: Mapping byte code offsets in custom code attributes<u></u><u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="color:black">Hi Rafael,<u></u><u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="color:black">You are correct. I had previously created an issue
<a href="https://bugs.openjdk.org/browse/JDK-8341274" target="_blank">https://bugs.openjdk.org/browse/JDK-8341274</a> to track this demand. Since this scenario has no way to bypass, maybe we can raise this issue to P3 and work on this feature for JDK 24. Also I wonder how
frequently are custom attributes storing BCIs?<u></u><u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="color:black"><u></u> <u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="color:black">For LineNumberInfo: I believe the Label-based version, LineNumber, is already streamed in class file reading, and can be sent to the builder also via CodeBuilder::lineNumber(int). Your particular use case can
probably be covered by the RFE too, once we can convert labels to BCIs in non-Code attributes.<u></u><u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="color:black"><u></u> <u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="color:black">Regards,<u></u><u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="color:black">Chen Liang<u></u><u></u></span></p>
</div>
<div class="MsoNormal" align="center" style="text-align:center">
<hr size="0" width="100%" align="center">
</div>
<div id="m_-3849973303882908695divRplyFwdMsg">
<p class="MsoNormal"><b><span style="font-size:11pt;font-family:"Calibri",sans-serif;color:black">From:</span></b><span style="font-size:11pt;font-family:"Calibri",sans-serif;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 Rafael Winterhalter <<a href="mailto:rafael.wth@gmail.com" target="_blank">rafael.wth@gmail.com</a>><br>
<b>Sent:</b> Saturday, November 9, 2024 2:32 AM<br>
<b>To:</b> classfile-api-dev <<a href="mailto:classfile-api-dev@openjdk.org" target="_blank">classfile-api-dev@openjdk.org</a>><br>
<b>Subject:</b> Mapping byte code offsets in custom code attributes</span> <u></u><u></u></p>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
<div>
<div>
<div>
<p class="MsoNormal">Hello,<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">I mentioned before that I think there is still a missing bit when it comes to custom code attributes, and mapping them using an AttributeMapper. Many code attributes point to a location, or area, within a method. This typically implies
specifying one or two byte code offsets within an attribute.<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">To implement this, I would need a possibility to translate offsets to labels within an attribute mapper, and vice versa. For example, if I created byte code from a different source, where a method consists of multiple files, I would want
to mark what part of the method comes from what file.<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">I do not currently know of a way to do this in the Class File API. To resolve this, I would have hoped that the attribute reader method would be able to issue labels that are later issued when the code instructions are supplied. Similarly,
I would have hoped that the attribute writer method would be capable of translating labels to byte code offsets.<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">In the same way, I would have hoped that all attributes such as LineNumberInfo of the LineNumberTableAttribute would expose bytecode offsets as labels and not as integers, as the latter does not really give any useful information by itself.
This way, I could also write a custom LineNumberTableAttribute after a method is already created by issuing relevant labels, but by only writing the attribute when the entire method was visited. Sometimes this is necessary when generating code, when the information
about the actual line numbers is only retrieved at a later stage that requires knowledge of the entire method.<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">As I understand it, the ClassFile API is supposed to be released in Java 24. Are there still plans to integrate such a change? After that it would require incompatible changes, therefore I had hoped this would make it into the final version.<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">Thanks! Rafael<u></u><u></u></p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div></blockquote></div>