JDK-8337238 : Demote maxStack and maxLocals from CodeModel to CodeAttribute
  • Type: CSR
  • Component: core-libs
  • Sub-Component: java.lang.classfile
  • Priority: P4
  • Status: Closed
  • Resolution: Approved
  • Fix Versions: 24
  • Submitted: 2024-07-25
  • Updated: 2024-07-29
  • Resolved: 2024-07-29
Related Reports
CSR :  
Description
Summary
-------

Move the `maxLocals` and `maxStack` accessors from `CodeModel` to `CodeAttribute`.

Problem
-------

`Code` attribute has encoded max locals and stack information, but this is not helpful for in-transform code models that implement `CodeModel`; computing these information is costly, and no transformation depends on these APIs.

Solution
--------

Move these 2 APIs to `CodeAttribute`.

If transformations wish to access the max stack and locals information, they can create custom transformations in a chain to scan the instructions to detect the max locals, or use `CodeStackTracker` to detect the max stacks.

Finally, if these APIs are found necessary to `CodeModel` later, they can be added back, which would be a source and binary compatible addition.

Specification
-------------

    diff --git a/src/java.base/share/classes/java/lang/classfile/CodeModel.java b/src/java.base/share/classes/java/lang/classfile/CodeModel.java
    index 45488561c1a..ba816f20805 100644
    --- a/src/java.base/share/classes/java/lang/classfile/CodeModel.java
    +++ b/src/java.base/share/classes/java/lang/classfile/CodeModel.java
    @@ -43,17 +43,7 @@
     @PreviewFeature(feature = PreviewFeature.Feature.CLASSFILE_API)
     public sealed interface CodeModel
             extends CompoundElement<CodeElement>, AttributedElement, MethodElement
    -        permits CodeAttribute, BufferedCodeBuilder.Model, CodeImpl {
    -
    -    /**
    -     * {@return the maximum size of the local variable table}
    -     */
    -    int maxLocals();
    -
    -    /**
    -     * {@return the maximum size of the operand stack}
    -     */
    -    int maxStack();
    +        permits CodeAttribute, BufferedCodeBuilder.Model {
     
         /**
          * {@return the enclosing method, if known}
    diff --git a/src/java.base/share/classes/java/lang/classfile/attribute/CodeAttribute.java b/src/java.base/share/classes/java/lang/classfile/attribute/CodeAttribute.java
    index 02cbcee810f..7a4f5886580 100644
    --- a/src/java.base/share/classes/java/lang/classfile/attribute/CodeAttribute.java
    +++ b/src/java.base/share/classes/java/lang/classfile/attribute/CodeAttribute.java
    @@ -47,6 +47,16 @@
     public sealed interface CodeAttribute extends Attribute<CodeAttribute>, CodeModel
             permits BoundAttribute.BoundCodeAttribute {
     
    +    /**
    +     * {@return the maximum size of the local variable table}
    +     */
    +    int maxLocals();
    +
    +    /**
    +     * {@return the maximum size of the operand stack}
    +     */
    +    int maxStack();
    +
         /**
          * {@return The length of the code array in bytes}
          */
    
Comments
Moving to Approved.
29-07-2024