Compute max locals, max stack and frames per method basis
Problem Statement
When using the ASM library for bytecode manipulation in Java, I encountered a limitation regarding the computation of maximum stack size, local variables, and frame information. Currently, ASM's ClassWriter
class allows specifying computation options (such as COMPUTE_MAXS
and COMPUTE_FRAMES
) only at the class level. This global setting applies to all methods within the class, which is not flexible for use cases where:
- Some methods have pre-calculated values for maximum stack sizes and local variables, requiring manual specification of these values.
- Other methods lack this information, necessitating ASM to automatically compute these values.
This limitation hinders efficient bytecode manipulation.
Attempted Solutions
I have explored extending ClassWriter
to modify its behavior on a per-method basis. However, this approach did not yield a viable solution due to the inability to override or extend the MethodWriter
class, which is declared as final and handles the method-level bytecode generation.
Additionally, utilizing the COMPUTE_FRAMES
option results in ASM disregarding manually specified values via visitMaxs
, thus recalculating everything automatically and ignoring pre-calculated values.
Proposed Enhancement
To address this issue, I propose an enhancement to the ASM library that allows selective application of computation options (COMPUTE_MAXS
and COMPUTE_FRAMES
) on a per-method basis. Specifically:
- Provide API support to manually set maximum stack sizes and local variables for methods with pre-calculated values.
- Allow ASM to automatically compute these values for methods lacking pre-calculated information, without affecting other methods in the class.
This feature would greatly enhance the flexibility and usability of ASM for complex bytecode manipulation tasks, accommodating a wider range of use cases.
Potential Impact
Implementing this feature could significantly improve developer efficiency and open up new possibilities for bytecode manipulation projects. It would also align with the needs of advanced users who require a blend of manual and automatic computation methods for bytecode generation.
I am open to contributing to the implementation of this feature or collaborating with others who are interested in enhancing ASM's capabilities in this area.
Thank you for considering this enhancement.