API suggestions

- liangchenblue at gmail.com
Wed Mar 22 03:38:03 UTC 2023


Hi,
After housekeeping the few tests after the latest integration of
additional constants in ConstantDescs, I have found two potentially
common patterns in bytecode manipulation and wish to suggest new APIs:

1. Injection of code in the beginning of methods
Currently, our CodeTransform only has an endHandler; however, this is
less useful, for a method can have multiple exit points. Usually to
track method calls, bytecode manipulation, such as those in jfr and
instrumentation, inject at the beginning of methods, which is
guaranteed to be on the code path.

Thus, I suggest a `startHandler(Consumer<CodeBuilder>)` for
CodeTransform for this purpose, instead of having to explicitly
override atStart(CodeBuilder) in non-lambda CodeTransform.

2. Adding new annotations
In ASM, annotation injection is done simply via extra annotation
visitor calls, which makes it compatible for either single or multiple
annotations. In contrast, in the Classfile API, if we want to add an
annotation, feeding a new RuntimeVisibleAnnotationAttribute will
destroy the previous RuntimeVisibleAnnotationAttribute, and handling
via a stateful ClassTransform is a bit too lengthy for users.

I don't know if we should have an API like Map::computeIfAbsent for
attributes or if we will simplify it via other means.

Chen Liang


More information about the classfile-api-dev mailing list