modifying a newly added method

Chen Liang chen.l.liang at oracle.com
Tue Apr 15 17:32:33 UTC 2025


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/20250415/c1f3e8f3/attachment.htm>


More information about the classfile-api-dev mailing list