<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>
<ol start="1" data-editing-info="{"applyListStyleFromLevel":false,"orderedStyleType":1}" style="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 class="elementToProof">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 🙁 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.</div>
</li><li style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<div class="elementToProof">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.</div>
</li><li style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<div class="elementToProof">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.</div>
</li></ol>
<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
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.</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> classfile-api-dev <classfile-api-dev-retn@openjdk.org> on behalf of Mark Roberts <markro@cs.washington.edu><br>
<b>Sent:</b> Friday, November 15, 2024 1:42 PM<br>
<b>To:</b> classfile-api-dev@openjdk.org <classfile-api-dev@openjdk.org><br>
<b>Subject:</b> class file transformation questions</font>
<div> </div>
</div>
<style>
<!--
@font-face
{font-family:"Cambria Math"}
@font-face
{font-family:Calibri}
p.x_MsoNormal, li.x_MsoNormal, div.x_MsoNormal
{margin:0in;
font-size:11.0pt;
font-family:"Calibri",sans-serif}
p.x_MsoListParagraph, li.x_MsoListParagraph, div.x_MsoListParagraph
{margin-top:0in;
margin-right:0in;
margin-bottom:0in;
margin-left:.5in;
font-size:11.0pt;
font-family:"Calibri",sans-serif}
span.x_EmailStyle23
{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">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_MsoNormal"> </p>
<p class="x_MsoNormal">There are three basic transforms we need to perform (all on the same class file):</p>
<p class="x_MsoNormal"> </p>
<ol start="1" type="1" style="margin-top:0in">
<li class="x_MsoListParagraph" style="margin-left:0in">Injecting code into an existing method</li><li class="x_MsoListParagraph" style="margin-left:0in">Adding new methods</li><li class="x_MsoListParagraph" style="margin-left:0in">Duplicating an existing method with some changes</li></ol>
<p class="x_MsoNormal"> </p>
<p class="x_MsoNormal">Any suggestions as to how to accomplish these tasks would be much appreciated.</p>
<p class="x_MsoNormal"> </p>
<p class="x_MsoNormal">Thank you,</p>
<p class="x_MsoNormal">Mark Roberts</p>
<p class="x_MsoNormal"> </p>
<p class="x_MsoNormal"> </p>
<p class="x_MsoNormal"> </p>
<p class="x_MsoNormal"> </p>
</div>
</div>
</body>
</html>