class file transformation questions

Chen Liang chen.l.liang at oracle.com
Sat Nov 16 00:47:50 UTC 2024


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.
  2.
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.
  3.
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> on behalf of Mark Roberts <markro at cs.washington.edu>
Sent: Friday, November 15, 2024 1:42 PM
To: classfile-api-dev at openjdk.org <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/20241116/954966c5/attachment-0001.htm>


More information about the classfile-api-dev mailing list