<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<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 Mark,</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 performing a lot of modifications, you first can create a</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);">
private static void check(CodeBuilder builder, CodeElement element) {</div>
<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
    // check the elements, and if it matches a pattern, call another method</div>
<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
    // as long as you pass the CodeBuilder and make changes to it, your changes will be saved</div>
<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
}</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 then refer to this as a CodeTransform in a method reference.</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);">
Or, if you want to do a huge overhaul to a CodeModel, you can get a MethodTransform, and have a specific method like:</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);">
if (me instanceof CodeModel code) {</div>
<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
    methodBuilder.withCode(cob -> rebuildCode(cob, code));</div>
<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
}</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);">
private static void rebuildCode(CodeBuilder builder, CodeModel model) {}</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);">
This gives you a more holistic view of the code, and should be more friendly for massive transformations that almost rebuild the method code. (This one also allows you to pull instructions to a list with new ArrayList<>(model.elementList()), modify that list,
 and do list.forEach(builder) to send the results, which is used in some older patterns)</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);">
Regards,</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> Mark Roberts <markro@cs.washington.edu><br>
<b>Sent:</b> Sunday, November 17, 2024 6:30 PM<br>
<b>To:</b> Chen Liang <chen.l.liang@oracle.com><br>
<b>Subject:</b> [External] : RE: class file transformation questions</font>
<div> </div>
</div>
<style>
<!--
@font-face
        {font-family:"Cambria Math"}
@font-face
        {font-family:Calibri}
@font-face
        {font-family:"Segoe UI Emoji"}
@font-face
        {font-family:Georgia}
@font-face
        {font-family:Aptos}
p.x_MsoNormal, li.x_MsoNormal, div.x_MsoNormal
        {margin:0in;
        font-size:11.0pt;
        font-family:"Calibri",sans-serif}
a:link, span.x_MsoHyperlink
        {color:#0563C1;
        text-decoration:underline}
p.x_xmsonormal, li.x_xmsonormal, div.x_xmsonormal
        {margin:0in;
        font-size:11.0pt;
        font-family:"Calibri",sans-serif}
p.x_xmsolistparagraph, li.x_xmsolistparagraph, div.x_xmsolistparagraph
        {margin-top:0in;
        margin-right:0in;
        margin-bottom:0in;
        margin-left:.5in;
        font-size:11.0pt;
        font-family:"Calibri",sans-serif}
span.x_EmailStyle24
        {font-family:"Calibri",sans-serif;
        color:windowtext}
.x_MsoChpDefault
        {font-size:10.0pt}
@page WordSection1
        {margin:1.0in 1.0in 1.0in 1.0in}
div.x_WordSection1
        {}
ol
        {margin-bottom:0in}
ul
        {margin-bottom:0in}
-->
</style>
<div lang="EN-US" link="#0563C1" vlink="#954F72" style="word-wrap:break-word">
<div class="x_WordSection1">
<p class="x_MsoNormal">Thank you for your help, it is much appreciated.  I have what I guess you would call a coding style question.  If you wish to perform a lot of modifications to a class file I could envision a method with hundreds of lines of code.  For
 a smaller example, see the<span style="font-size:10.5pt; font-family:"Georgia",serif; color:#282828; background:white"> chaining multiple transformations example in
</span>java/lang/classfile/ClassFileTransform.html.  I find this very (extremely?) difficult to read.  Is there any way of writing this code in a more ‘traditional’ way with several, smaller method bodies?  Or is there no way to use java.lang.classfile without
 chaining together large numbers of builders written as lambda functions?</p>
<p class="x_MsoNormal"> </p>
<p class="x_MsoNormal">Thank you,</p>
<p class="x_MsoNormal">Mark</p>
<p class="x_MsoNormal"> </p>
<div>
<div style="border:none; border-top:solid #e1e1e1 1.0pt; padding:3.0pt 0in 0in 0in">
<p class="x_MsoNormal"><b>From:</b> Chen Liang <<a href="mailto:chen.l.liang@oracle.com">chen.l.liang@oracle.com</a>>
<br>
<b>Sent:</b> Friday, November 15, 2024 4:48 PM<br>
<b>To:</b> Mark Roberts <<a href="mailto:markro@cs.washington.edu">markro@cs.washington.edu</a>>;
<a href="mailto:classfile-api-dev@openjdk.org">classfile-api-dev@openjdk.org</a><br>
<b>Subject:</b> Re: class file transformation questions</p>
</div>
</div>
<p class="x_MsoNormal"> </p>
<div>
<p class="x_MsoNormal"><span style="font-size:12.0pt; font-family:Aptos; color:black">Hi Mark,</span></p>
</div>
<div>
<ol start="1" type="1">
<li class="x_MsoNormal" style="color:black"><span style="font-size:12.0pt; font-family:Aptos">For injecting code, you can override the atStart of the CodeTransform interface.  Unfortunately we don't have a utility method for you to pass a lambda
</span><span style="font-size:12.0pt; font-family:"Segoe UI Emoji",sans-serif">🙁</span><span style="font-size:12.0pt; font-family:Aptos"> but we can always add it later.  If you wish to find an injection point, you can get the full list of code elements, and
 find your insertion point and add the desired instruction objects.</span></li></ol>
</div>
<div>
<ol start="2" type="1">
<li class="x_MsoNormal" style="color:black"><span style="font-size:12.0pt; font-family:Aptos">For adding new methods, you can also override the atStart/atEnd method of your ClassTransform, or do andThen(ClassTransform.endHandler(clb -> clb.withMethod(...)))
 to add methods.</span></li></ol>
</div>
<div>
<ol start="3" type="1">
<li class="x_MsoNormal" style="color:black"><span style="font-size:12.0pt; font-family:Aptos">To duplicate an existing method object, you can first call withMethod to create a new method, and once you have the method builder, it has a transform(otherMethod,
 transform) that can pipe otherMethod's contents through the transform, and finally everything goes to the method builder.</span></li></ol>
</div>
<div>
<p class="x_MsoNormal"><span style="font-size:12.0pt; font-family:Aptos; color:black">Feel free to ask more or suggest.  Unfortunately I am still trying to improve documentations for ClassFile API, so at this stage the documents may still be insufficient, and
 many of the useful methods are not covered by the JEP.</span></p>
</div>
<div>
<p class="x_MsoNormal"><span style="font-size:12.0pt; font-family:Aptos; color:black">Regards,</span></p>
</div>
<div>
<p class="x_MsoNormal"><span style="font-size:12.0pt; font-family:Aptos; color:black">Chen Liang</span></p>
</div>
<div class="x_MsoNormal" align="center" style="text-align:center">
<hr size="2" width="98%" align="center">
</div>
<div id="x_divRplyFwdMsg">
<p class="x_MsoNormal"><b><span style="color:black">From:</span></b><span style="color:black"> classfile-api-dev <<a href="mailto:classfile-api-dev-retn@openjdk.org">classfile-api-dev-retn@openjdk.org</a>> on behalf of Mark Roberts <<a href="mailto:markro@cs.washington.edu">markro@cs.washington.edu</a>><br>
<b>Sent:</b> Friday, November 15, 2024 1:42 PM<br>
<b>To:</b> <a href="mailto:classfile-api-dev@openjdk.org">classfile-api-dev@openjdk.org</a> <<a href="mailto:classfile-api-dev@openjdk.org">classfile-api-dev@openjdk.org</a>><br>
<b>Subject:</b> class file transformation questions</span> </p>
<div>
<p class="x_MsoNormal"> </p>
</div>
</div>
<div>
<div>
<p class="x_xmsonormal">Several of our tools use the java.lang.instrument package to instrument class files when they are loaded.  We currently use BCEL to do the instrumentation, but would like to move to the new java.lang.classfile package.  I have gotten
 some basic transforms working in this environment and I see how to modify existing instructions.   However, we need to perform some larger modifications to the classes we instrument.</p>
<p class="x_xmsonormal"> </p>
<p class="x_xmsonormal">There are three basic transforms we need to perform (all on the same class file):</p>
<p class="x_xmsonormal"> </p>
<ol start="1" type="1" style="margin-top:0in">
<li class="x_xmsolistparagraph" style="margin-left:0in">Injecting code into an existing method</li><li class="x_xmsolistparagraph" style="margin-left:0in">Adding new methods</li><li class="x_xmsolistparagraph" style="margin-left:0in">Duplicating an existing method with some changes</li></ol>
<p class="x_xmsonormal"> </p>
<p class="x_xmsonormal">Any suggestions as to how to accomplish these tasks would be much appreciated.</p>
<p class="x_xmsonormal"> </p>
<p class="x_xmsonormal">Thank you,</p>
<p class="x_xmsonormal">Mark Roberts</p>
<p class="x_xmsonormal"> </p>
<p class="x_xmsonormal"> </p>
<p class="x_xmsonormal"> </p>
<p class="x_xmsonormal"> </p>
</div>
</div>
</div>
</div>
</body>
</html>