JVM virtual machine --- (11) method table file structure of the Class collection

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq1021979964/article/details/97129710

Table collection method

Description Class file description of the methods for the field almost the same, has the same construction and the fields of the table of the method table sequentially include access flag (access_flags), Name Index (name_index), descriptor index (description_index), attribute table set (attribute) a few.

 

1. The method of table structure (method_info) Composition

Access flag (access_flags), name index (name_index), description Index (descriptor_index), a collection of attribute table

2. Case

public class TestMethod {

    public int add(int a,int b) {
        return a+b;
    }

}

After the fields of the table is a set of counter method, and then on to the zone method. We just above defines a method add, but there are two display methods, because it is a no-argument constructor, you first talk about the constructor

Method counter: 0x0002 (corresponding to decimal 2), there are two methods.

3. The constructor method Case

  1. Method access flag: 0x0001, corresponding to the public.
  2. Method Name: 0x0004 (corresponding to decimal 4), the fourth point to the constant pool for the init.
  3. Descriptor index: 0x0005 (corresponding to decimal 5), point 5 of constant pool, is () V, represents a method without parameters, no return value.
  4. Attribute counters: 0x0001 (corresponding to decimal 1), it indicates that the method table has a property sheet. Followed closely behind.
  5. Attribute table name index: 0x0006 (corresponding to decimal 6), pointing to the sixth constant pool for Code, a Code indicates the type of the attribute table.
  6. Attribute length: 0x0000 001D (corresponding to decimal 29), 29 indicates a subsequent byte is information indicating the Code attribute table.
  7. The maximum depth of the operand stack: 0x0001 (corresponding to decimal 1), represents the maximum depth of the stack frame is an operand stack.
  8. 局部变量表的最大容量:0x0001(对应十进制的1),JVM调用该方法时,根据这个值设置栈帧中的局部变量表的大小。
  9. 机器指令数目:0x0000 0005,表示后续的5个字节是机器指令集。
  10. 机器指令集:0x002A、0x00B7、0x0000、0x0001、0x00B1。
  11. 显示异常表集合:0x0000,表示方法中没有需要处理的异常信息。
  12. Code属性表的属性表集合:0x0001,表示还有其他1个属性表集合。
  13. Code属性表集合名称:0x0007(对应十进制为7),指向常量池的LineNumberTable。

LineNumberTable属性表的就不说了,直接过掉了。

4.自定义方法案例

  1. 方法访问标志:0x0001,对应public。
  2. 方法名称:0x0008(对应十进制为8),指向常量池的第8,为add。
  3. 描述符索引:0x0009(对应十进制为9),指向常量池的第9,为(II)I,表示方法参数有2个都是Int类型,返回值类型是Int。
  4. 属性计数器:0x0001(对应十进制为1),表示该方法表中有1个属性表。紧跟在后。
  5. 属性表名称索引:0x0006(对应十进制为6),指向常量池的第6,为Code,表示是Code类型的属性表。
  6. 属性长度:0x0000 001C(对应十进制的28),表示后续28个字节是表示这个Code属性表的信息。
  7. 操作数栈的最大深度:0x0002(对应十进制的2),表示栈帧中操作数栈的最大深度是2。
  8. 局部变量表的最大容量:0x0003(对应十进制的3),JVM调用该方法时,根据这个值设置栈帧中的局部变量表的大小。
  9. 机器指令数目:0x0000 0004,表示后续的4个字节是机器指令集。
  10. 机器指令集:0x001B、0x001C、0x0060、0x00AC。
  11. 显示异常表集合:0x0000,表示方法中没有需要处理的异常信息。
  12. Code属性表的属性表集合:0x0001,表示还有其他1个属性表集合。
  13. Code属性表集合名称:0x0007(对应十进制为7),指向常量池的LineNumberTable。

LineNumberTable属性表的就不说了,直接过掉了。

Guess you like

Origin blog.csdn.net/qq1021979964/article/details/97129710