Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8335475

ClassBuilder incorrectly calculates max_locals in some cases

XMLWordPrintable

        There is a mini test case:
        jshell> import java.lang.classfile.*;
           ...> import java.lang.constant.ClassDesc;
           ...> import static java.lang.classfile.ClassFile.*;
           ...> import static java.lang.constant.ConstantDescs.MTD_void;

        jshell> ClassModel cm = ClassFile.of().parse(
           ...> ClassFile.of().build(
           ...> ClassDesc.of("test"),
           ...> clb-> clb.withMethodBody( "clsEmpty", MTD_void, ACC_PRIVATE | ACC_STATIC,
           ...> cb->cb.return_()).
           ...> withMethodBody( "instEmpty", MTD_void, ACC_PRIVATE,
           ...> cb->cb.return_()
           ...> )
           ...> ));
           ...> for (MethodModel mm : cm.methods()) {
           ...> System.out.println("%s:\tmax_locals = %d".formatted(mm.methodName().stringValue(), mm.code().get().maxLocals()));
           ...> }
        cm ==> ClassModel[thisClass=test, flags=1]
        clsEmpty: max_locals = 1
        instEmpty: max_locals = 1

        The expected values:
        clsEmpty: max_locals = 0
        instEmpty: max_locals = 1
        as a result of javac for the method:
         private static void clsEmpty() {
          }
        =>
          private static void clsEmpty();
            descriptor: ()V
            flags: (0x000a) ACC_PRIVATE, ACC_STATIC
            Code:
              stack=0, locals=0, args_size=0
                 0: return

              liach Chen Liang
              lkuskov Leonid Kuskov
              Votes:
              1 Vote for this issue
              Watchers:
              4 Start watching this issue

                Created:
                Updated:
                Resolved: