<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<style type="text/css" style="display:none;"> P {margin-top:0;margin-bottom:0;} </style>
</head>
<body dir="ltr">
<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Hi Rafael, thanks for the quick followup!</div>
<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Indeed, the line number label handling enhancement would be helpful. (and that actually applies to all user attribute encoding of Labels, as currently user attributes cannot convert labels to bcis too)</div>
<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
For the following stacktrace, I don't think JDK's constants API is wrong: you should use (Lookup, String, Class) -> void type for the bootstrap method's method handle type. I checked bytebuddy's JavaConstant.Dynamic and JavaConstantDynamicTest and still have
 no clue how that happens, or how it passes elsewhere. It would be great if you can provide a simple generated class file or its javap output, especially if this class can run on java command line so we can check the VM behavior.</div>
<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
And congratulations on the progress! The "dead code" warning shouldn't happen usually, as ClassFile API has PATCH_DEAD_CODE by default for DeadCodeOption. These errors should go away if you restore the option.</div>
<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Thanks again for trying out!</div>
<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Chen Liang</div>
<div id="appendonsend"></div>
<hr style="display:inline-block;width:98%" tabindex="-1">
<div id="divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" style="font-size:11pt" color="#000000"><b>From:</b> Rafael Winterhalter <rafael.wth@gmail.com><br>
<b>Sent:</b> Tuesday, August 13, 2024 5:08 PM<br>
<b>To:</b> Chen Liang <chen.l.liang@oracle.com><br>
<b>Cc:</b> classfile-api-dev <classfile-api-dev@openjdk.org><br>
<b>Subject:</b> [External] : Re: ASM to OpenJDK ClassReader/ClassWriter bridge: experiences and questions</font>
<div> </div>
</div>
<div>
<div dir="ltr">
<div>Thanks, I will try the manual stack map frame translation! As for line numbers: I also wanted to create the attribute manually. Unfortunately, LineNumberInfo only accepts a bci instead of a Label. If that can be adjusted (I think this is sensible as this
 is the only location where bci seems to be exposed), I could solve the problem that way.</div>
<div><br>
</div>
<div>I also found a possible bug with the following stacktrace:</div>
<div><br>
</div>
<div>java.lang.IllegalArgumentException: Expected type of (T*)V for constructor, found MethodTypeDesc[(MethodHandles$Lookup,String,Class)DynamicConstantBootstrap]<br>
at java.base/java.lang.constant.DirectMethodHandleDescImpl.validateConstructor(DirectMethodHandleDescImpl.java:107)<br>
at java.base/java.lang.constant.DirectMethodHandleDescImpl.<init>(DirectMethodHandleDescImpl.java:75)<br>
at java.base/java.lang.constant.MethodHandleDesc.of(MethodHandleDesc.java:89)<br>
at java.base/jdk.internal.classfile.impl.AbstractPoolEntry$MethodHandleEntryImpl.asSymbol(AbstractPoolEntry.java:919)<br>
at java.base/java.lang.classfile.constantpool.ConstantDynamicEntry.asSymbol(ConstantDynamicEntry.java:65)<br>
at java.base/jdk.internal.classfile.impl.StackMapGenerator.processLdc(StackMapGenerator.java:705)</div>
<div><br>
</div>
<div>This happens when using a constructor as the bootstrap for a dynamic constant. The check does not seem to consider that constructors are valid bootstraps similar to static methods.</div>
<div><br>
</div>
<div>Other than that, I ran Byte Buddy's entire test suite with about 10.000 tests using that bridge now and only about 200 tests seem to fail. Most commonly this is due to "Unable to generate stack map frame for dead code at bytecode offset 36 of method foobar()",
 but this I might be able to solve with your suggestion. The reminder are mostly about edge cases that I will investigate further.</div>
<div><br>
</div>
<div>Well done! If these two issues can be resolved, I plan to offer an adapter into Byte Buddy once this is public API.</div>
<div>Rafael<br>
</div>
</div>
<br>
<div class="x_gmail_quote">
<div dir="ltr" class="x_gmail_attr">Am Di., 13. Aug. 2024 um 23:29 Uhr schrieb Chen Liang <<a href="mailto:chen.l.liang@oracle.com">chen.l.liang@oracle.com</a>>:<br>
</div>
<blockquote class="x_gmail_quote" style="margin:0px 0px 0px 0.8ex; border-left:1px solid rgb(204,204,204); padding-left:1ex">
<div class="x_msg1123692594695973572">
<div dir="ltr">
<div style="direction:ltr; font-family:Aptos,Aptos_EmbeddedFont,Aptos_MSFontService,Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
Hi Rafael, thanks for your adoption!</div>
<ol start="1" style="direction:ltr; margin-top:0px; margin-bottom:0px; list-style-type:decimal">
<li style="font-family:Aptos,Aptos_EmbeddedFont,Aptos_MSFontService,Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<div style="direction:ltr">In fact, ClassFile API has quite a few non-standard (JVMS) attributes: CharacterRangeTable, CompilationID, ModuleHashes, ModuleResolution, ModuleTarget, SourceDebugExtension, SourceID, and seems there's little information available
 about these attributes. I think you can treat them as if they are unknown attributes in your translations to ASM.</div>
</li><li style="font-family:Aptos,Aptos_EmbeddedFont,Aptos_MSFontService,Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<div style="direction:ltr">Line numbers are naturally streamed just like labels. If an ASM user is streaming line numbers late, an alternative way is to buffer the other elements into an ASM tree first, then inject the line number tokens on the second round.
 (If LineNumberTableAttribute supports using Labels, then we can probably just write that attribute at last, too)</div>
</li><li style="font-family:Aptos,Aptos_EmbeddedFont,Aptos_MSFontService,Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<div style="direction:ltr">Unknown attributes are troubling the ClassFile API too. We have a stability() in AttributeMapper to indicate if we should retain or drop attributes. You can always copy any attribute over as-is by calling XxxBuilder::with(attribute).
 You should always handle the unknown attributes with a default branch in a switch if you wish to be forward compatible, or an exhaustive switch if you wish to fail fast on newer known attributes.</div>
</li><li style="font-family:Aptos,Aptos_EmbeddedFont,Aptos_MSFontService,Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<div style="direction:ltr">For stack map frames, you can use StackMapsOption.DROP_STACK_MAPS and pass a StackMapsTableAttribute in CodeBuilder.with; this is already how ProxyGenerator does it. However we don't allow users to specify max stacks and locals, which
 can be hurting performance sensitive use cases.</div>
</li></ol>
<div style="direction:ltr; font-family:Aptos,Aptos_EmbeddedFont,Aptos_MSFontService,Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<br>
</div>
<div style="direction:ltr; font-family:Aptos,Aptos_EmbeddedFont,Aptos_MSFontService,Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
Regards,</div>
<div style="direction:ltr; font-family:Aptos,Aptos_EmbeddedFont,Aptos_MSFontService,Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
Chen Liang</div>
<div id="x_m_1123692594695973572x_appendonsend"></div>
<hr style="direction:ltr; display:inline-block; width:98%">
<div id="x_m_1123692594695973572x_divRplyFwdMsg" dir="ltr"><span style="font-family:Calibri,sans-serif; font-size:11pt; color:rgb(0,0,0)"><b>From:</b> 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> Tuesday, August 13, 2024 4:51 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> ASM to OpenJDK ClassReader/ClassWriter bridge: experiences and questions</span>
<div> </div>
</div>
<div style="direction:ltr">Hello!</div>
<div style="direction:ltr"><br>
</div>
<div style="direction:ltr">It's been a while since I last tried, but I now managed to implement both a JDK-based ClassReader and ClassWriter for ASM. This seems to work really well for a range of tests that I did as it allows me to avoid major rewrites of ASM-based
 code while still having forward-compatibility as long as there are no new language constructs as the JDK bundles parser and serializer.</div>
<div style="direction:ltr"><br>
</div>
<div style="direction:ltr">The code can be found here: <a href="https://urldefense.com/v3/__https://github.com/raphw/asm-jdk-bridge__;!!ACWV5N9M2RV99hQ!KAQFTclWgq_IwySyN468Wdy3QUCwqLln_sFAi0SSvWoBHHyKMExsxZbmfhPm0axz2oGf-EbHxWkP391GDU6uYQ$" id="x_m_1123692594695973572OWA2740785e-b87c-54ec-3710-3f68e8ade305" target="_blank">
https://github.com/raphw/asm-jdk-bridge</a></div>
<div style="direction:ltr"><br>
</div>
<div style="direction:ltr">A few notes and questions I have:</div>
<div style="direction:ltr"><br>
</div>
<div style="direction:ltr">1. The OpenJDK API supports CharacterRangeTable which is a non-standard attribute that is not described in the specification. By introducing it to the official API, isn't the attribute in a way formalized? Is there a plan to standardize
 the attribute?</div>
<div style="direction:ltr"><br>
</div>
<div style="direction:ltr">2. One thing that cannot be mapped directly from OpenJDK to ASM are line numbers. In ASM, they can be added later using a Label. In OpenJDK they have to be visited at "the right time". Using labels is otherwise common for other attributes,
 both in ASM and OpenJDK, but line numbers are the exception. Is there a reason for this?</div>
<div style="direction:ltr"><br>
</div>
<div style="direction:ltr">3. It would be nice if there was a way to flag what attributes should be treated as UnknownAttributes. Right now, I retain UnknownAttributes as they are. But if a future release of the OpenJDK promotes an UnknownAttribute to a known
 one, I might miss it when copying them over as raw arrays.</div>
<div style="direction:ltr"><br>
</div>
<div style="direction:ltr">4. I take it so that there are no plans to add support for manually defining StackMapFrame and method sizes? Sometimes, the OpenJDK erases information, for example if a local variable is never assigned a value, it will simply be treated
 as "N" value. This is not a big issue, but it can make slight transformations that can have very subtle implications when for example writing Java agents, so I would still hope for an option for this to be used by advanced users.</div>
<div style="direction:ltr"><br>
</div>
<div style="direction:ltr">Thanks! Rafael</div>
</div>
</div>
</blockquote>
</div>
</div>
</body>
</html>