modifying a newly added method

Mark Roberts markro at cs.washington.edu
Thu Apr 24 22:07:16 UTC 2025


This approach looks interesting.  Just want to make sure I understand the
control flow.



The empty ‘accept’ is the first transform that does nothing.

‘atEnd’ is the second transform

‘andThen is the third transform which will see any changes made by the
second transform



Correct?



Thank you,

Mark



*From:* Chen Liang <chen.l.liang at oracle.com>
*Sent:* Tuesday, April 15, 2025 10:33 AM
*To:* Mark Roberts <markro at cs.washington.edu>; classfile-api-dev at openjdk.org
*Subject:* Re: modifying a newly added method



Hi Mark,

Unfortunately there is no easy way to do this, but it is doable.



One way can be that you start with a class transform with a dummy class
model, and move your original class builder code into the atEnd  of your
first transform. Then, you can call andThen with the 2nd transform, which
can get MethodModel views of methods built by your first transform.



Pseudocode:



Original:

cf.build(name, clb -> /* oldLambdaCode */)



Updated:

cf.build(clb -> clb.transform(dummyClassModel, new ClassTransform() {

  @Override public void accept(ClassBuilder b, ClassEement e) {} // throw
away

  @Override public void atEnd(ClassBuilder clb) { /* oldLambdaCode */ }

}.andThen(/* your actual transform that can see your old lambda's methods
as MethodModel*/))



That said, I had previously proposed to introduce something like
CodeBuilder::transforming to other models, which could have probably
covered your use case; however, other builders require extra arguments to
be constructed (class name, field/method name and types) so we did not add
these APIs back.

If you have an idea how such additions can be done, feel free to share your
ideas.



Regards, Chen
------------------------------

*From:* classfile-api-dev <classfile-api-dev-retn at openjdk.org> on behalf of
Mark Roberts <markro at cs.washington.edu>
*Sent:* Tuesday, April 15, 2025 12:15 PM
*To:* classfile-api-dev at openjdk.org <classfile-api-dev at openjdk.org>
*Subject:* modifying a newly added method



If I have added a new method to my classfile with
ClassBuilder.withMethod(...) I know it is not added to ClassModel.methods()
as that is an immutable list from the original, unmodified classfile.  Is
there any way to get a MethodModel for this new method in order to modify it
further?

Thank you,
Mark
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/classfile-api-dev/attachments/20250424/8bb3d2d6/attachment-0001.htm>


More information about the classfile-api-dev mailing list