java.lang.classfile model enumeration order

Chen Liang chen.l.liang at oracle.com
Fri Nov 22 18:50:17 UTC 2024


I think the interest in the order is more among the same kind of elements instead of among different kinds of elements.

ASM currently allows you to mix-and-match fields and methods in class visiting; it just has weird requirements such as visitSource -> visitModule -> visitNestHost -> visit other attributes.  Such requirements are extremely error-prone and make little sense, and do not exist in the ClassFile API.

However, there might be value for the order for the same kind of element in class file streaming.  For example, if you stream f1, m1, m2, f2, you might want f1 before f2 and m1 before m2.  This order is currently a complementary gift from the implementation, but nothing guarantees that stay the way in the long run. The order of fields and methods are not important in class file interpretation, after all.

Regards,
Chen Liang
________________________________
From: Brian Goetz <brian.goetz at oracle.com>
Sent: Friday, November 22, 2024 12:09 PM
To: Mark Roberts <markro at cs.washington.edu>; Adam Sotona <adam.sotona at oracle.com>; Chen Liang <chen.l.liang at oracle.com>; classfile-api-dev at openjdk.org <classfile-api-dev at openjdk.org>
Subject: Re: java.lang.classfile model enumeration order

Correct.  ASM does guarantee an order of events, but our experience is that such constraints do not help the user very much, are error-prone to rely on, and can limit future flexibility for the implementation.


On 11/22/2024 1:07 PM, Mark Roberts wrote:

So to summarize: it’s true today, but don’t rely on it.



Mark



From: Brian Goetz <brian.goetz at oracle.com<mailto:brian.goetz at oracle.com>>
Sent: Friday, November 22, 2024 8:02 AM
To: Adam Sotona <adam.sotona at oracle.com<mailto:adam.sotona at oracle.com>>; Mark Roberts <markro at cs.washington.edu<mailto:markro at cs.washington.edu>>; Chen Liang <chen.l.liang at oracle.com<mailto:chen.l.liang at oracle.com>>; classfile-api-dev at openjdk.org<mailto:classfile-api-dev at openjdk.org>
Subject: Re: java.lang.classfile model enumeration order



This is a property of the implementation, but not something that the spec guarantees, though.  Also, in the middle of a transformation pipeline, there is no longer a "classfile order" to fall back on.  So it is best to not assume anything about ordering, unless the ordering is part of the semantics (like the bytecodes in a method body.)




On 11/22/2024 5:42 AM, Adam Sotona wrote:

Hi Mark,

Fields and methods are included in the ClassElement stream or iterator in the same order, and this order corresponds to their order in the class file.

This principle is common for the whole API, except for some PseudoInstructions identifying their start and stop positions  in the stream by Labels. These PseudoInstructions are always streamed before respective LabelTargets.



Adam



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>
Date: Friday, 22 November 2024 at 1:12
To: Chen Liang <chen.l.liang at oracle.com><mailto:chen.l.liang at oracle.com>, 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: java.lang.classfile model enumeration order

Are there any rules/guarantees that the order of items in ClassModel::methods() will match the order in ‘for (ClassElement ce : cm)’  for the ce elements that are MethodModel?  And also for similar items down the class hierarchy?



Thank you, Mark





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


More information about the classfile-api-dev mailing list