[External] : RE: class file transformation questions

Chen Liang chen.l.liang at oracle.com
Mon Nov 18 00:58:50 UTC 2024


Hi Mark,
For performing a lot of modifications, you first can create a

private static void check(CodeBuilder builder, CodeElement element) {
    // check the elements, and if it matches a pattern, call another method
    // as long as you pass the CodeBuilder and make changes to it, your changes will be saved
}

And then refer to this as a CodeTransform in a method reference.

Or, if you want to do a huge overhaul to a CodeModel, you can get a MethodTransform, and have a specific method like:

if (me instanceof CodeModel code) {
    methodBuilder.withCode(cob -> rebuildCode(cob, code));
}

private static void rebuildCode(CodeBuilder builder, CodeModel model) {}

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)

Regards,
Chen Liang
________________________________
From: Mark Roberts <markro at cs.washington.edu>
Sent: Sunday, November 17, 2024 6:30 PM
To: Chen Liang <chen.l.liang at oracle.com>
Subject: [External] : RE: class file transformation questions


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 chaining multiple transformations example in 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?



Thank you,

Mark



From: Chen Liang <chen.l.liang at oracle.com<mailto:chen.l.liang at oracle.com>>
Sent: Friday, November 15, 2024 4:48 PM
To: Mark Roberts <markro at cs.washington.edu<mailto:markro at cs.washington.edu>>; classfile-api-dev at openjdk.org<mailto:classfile-api-dev at openjdk.org>
Subject: Re: class file transformation questions



Hi Mark,

  1.  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.

  1.  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.

  1.  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.

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.

Regards,

Chen Liang

________________________________

From: classfile-api-dev <classfile-api-dev-retn at openjdk.org<mailto:classfile-api-dev-retn at openjdk.org>> on behalf of Mark Roberts <markro at cs.washington.edu<mailto:markro at cs.washington.edu>>
Sent: Friday, November 15, 2024 1:42 PM
To: classfile-api-dev at openjdk.org<mailto:classfile-api-dev at openjdk.org> <classfile-api-dev at openjdk.org<mailto:classfile-api-dev at openjdk.org>>
Subject: class file transformation questions



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.



There are three basic transforms we need to perform (all on the same class file):



  1.  Injecting code into an existing method
  2.  Adding new methods
  3.  Duplicating an existing method with some changes



Any suggestions as to how to accomplish these tasks would be much appreciated.



Thank you,

Mark Roberts








-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/classfile-api-dev/attachments/20241118/5ca87d01/attachment-0001.htm>


More information about the classfile-api-dev mailing list