[JVM]理解Class文件(3)

1. 引言

在上两篇理解Class文件(1):手动解析常量池[JVM]理解Class文件(2)中,已经对class文件中的大部分内容做了解析,接下来再看下class文件中剩下的最后两个字段:methods和attributes

  • ClassFile结构

ClassFile {
    u4 magic; 
    u2 minor_version; 
    u2 major_version; 
    u2 constant_pool_count; 
    cp_info constant_pool[constant_pool_count-1];
    u2 access_flags; 
    u2 this_class; 
    u2 super_class; 
    u2 interfaces_count; 
    u2 interfaces[interfaces_count]; 
    u2 fields_count; 
    field_info fields[fields_count]; 
    u2 methods_count; 
    method_info methods[methods_count]; 
    u2 attributes_count; 
    attribute_info attributes[attributes_count]; 
}
  • 示例TestClass文件

将生成的class文件用UltraEdit打开,可以清楚地看到Java编译后生成的字节码,我们要解析的内容也就是这些字节码。

00000000h: CA FE BA BE 00 00 00 33 00 12 0A 00 03 00 0E 07 ; 
00000010h: 00 0F 07 00 10 01 00 04 54 45 53 54 01 00 12 4C ; 
00000020h: 6A 61 76 61 2F 6C 61 6E 67 2F 53 74 72 69 6E 67 ; 
00000030h: 3B 01 00 0D 43 6F 6E 73 74 61 6E 74 56 61 6C 75 ; 
00000040h: 65 08 00 11 01 00 06 3C 69 6E 69 74 3E 01 00 03 ; 
00000050h: 28 29 56 01 00 04 43 6F 64 65 01 00 0F 4C 69 6E ; 
00000060h: 65 4E 75 6D 62 65 72 54 61 62 6C 65 01 00 0A 53 ;
00000070h: 6F 75 72 63 65 46 69 6C 65 01 00 0E 54 65 73 74 ;
00000080h: 43 6C 61 73 73 2E 6A 61 76 61 0C 00 08 00 09 01 ; 
00000090h: 00 09 54 65 73 74 43 6C 61 73 73 01 00 10 6A 61 ; 
000000a0h: 76 61 2F 6C 61 6E 67 2F 4F 62 6A 65 63 74 01 00 ;  
000000b0h: 0B 74 65 73 74 20 73 74 72 69 6E 67 00 21 00 02 ;  
000000c0h: 00 03 00 00 00 01 00 1A 00 04 00 05 00 01 00 06 ;  
000000d0h: 00 00 00 02 00 07 00 01 00 01 00 08 00 09 00 01 ;  
000000e0h: 00 0A 00 00 00 1D 00 01 00 01 00 00 00 05 2A B7 ; 
000000f0h: 00 01 B1 00 00 00 01 00 0B 00 00 00 06 00 01 00 ; 
00000100h: 00 00 02 00 01 00 0C 00 00 00 02 00 0D          ; 
  • TestClass常量池

TestClass常量池

2. methods_count、methods

  • ** 2.1 method_info**

method_info { 
    u2 access_flags; 
    u2 name_index; 
    u2 descriptor_index; 
    u2 attributes_count; 
    attribute_info attributes[attributes_count]; 
}

虽然methods字段结构体看起来很简单,但是由于attribute_info中又嵌套了其他属性,导致这部分的解析过程会比想象中要复杂。先附上TestClass的methods解析示例图,然后再来看各个字段代表的含义。


methods字段解析
  • ** 2.2 access_flags**

access_flags定义当前方法的访问权限和基本属性的掩码标志,它的取值范围和相应含义如下图:


方法 access_flags标记列表

截止到attribute_name_index这个字段,前面部分都比较简单,对应字段含义如下:

Option Values Description
methods_count 0x00 01 方法个数为1
access_flags 0x00 01 ACC_PUBLIC
name_index 0x00 08 对应常量池索引为8的常量,即<init>
descriptor_index 0x00 09 对应常量池索引为9,即()V
attributes_count 0x00 01 属性个数为1
attribute_name_index 0x00 0A Code
  • 2.3 code

接下来,来看第一个表结构Code

Code_attribute { 
    u2 attribute_name_index; 
    u4 attribute_length; 
    u2 max_stack; 
    u2 max_locals; 
    u4 code_length; 
    u1 code[code_length]; 
    u2 exception_table_length; { 
        u2 start_pc; 
        u2 end_pc; 
        u2 handler_pc; 
        u2 catch_type; 
    } exception_table[exception_table_length]; 
    u2 attributes_count; 
    attribute_info attributes[attributes_count];
}
Option Values Description
attribute_name_index 0x00 0A Code
attribute_length 0x00 00 00 0D 当前属性的长度,不包括开始的6个字节(即不包括attribute_name_index和attribute_length的长度)
max_stack 0x00 01 当前方法的操作数栈在运行执行的任何时间点的最大深度
max_locals 0x00 01 分配在当前方法引用的局部变量表中的局部变量个数
code_length 0x00 00 00 05 当前方法的code[]数组的字节数
code[5] 0x2A B7 00 01 B1 当前方法的Java虚拟机字节码
exception_table_length 0x00 00 给出了exception_table[]数组的成员个数量
attributes_count 0x00 01 Code属性中attributes表的成员个数
  • ** Java虚拟机指令集**
    在对code进行解析时,其中有一项code[5] = 2A B7 00 01 B1,这些值代表的是Java虚拟机字节码,分别对这5个指令进行解释。这个指令对应的值,都可以在 Java虚拟机规范(Java SE 7)的第6章 Java虚拟机指令集搜索到。

  • 2A对应的指令为aload_0,表示将第0个Slot中为reference类型的本地变量推送到操作数栈顶。我们通过LocalVariableTable来查看slot对应的本地变量,第0个Slot中为reference类型的本地变量就是this


    aload

在前面的示例中,使用javap解析出来的结果中没有包含LocalVariableTable字段,需要在使用javac编译java中时,加上-g的参数,生成的class文件中才带有LocalVariableTable的信息。

D:\TestClass>javac -g TestClass.java

D:\TestClass>javap -verbose TestClass
// 省略常量池部分
{
  public TestClass();
    flags: ACC_PUBLIC
    Code:
      stack=1, locals=1, args_size=1
         0: aload_0
         1: invokespecial #1                  // Method java/lang/Object."<init>":()V
         4: return
      LineNumberTable:
        line 2: 0
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
               0       5     0  this   LTestClass;
}
  • B7对应的指令为invokespecial,表示已栈顶的reference类型的数据所指向的对象作为方法的接收者,调用此对象的实例构造方法、private方法或者它的父类方法。invokespecial有一个u2类型的参数说明具体是调用的哪个方法。


    invokespecial
  • 00 01表示invokespecial的参数,指向的是常量池中索引值为1的常量,即java/lang/Object."<init>":()V

  • B1对应的指令为return,表示返回此方法,并且返回值为void。这条指令执行后,当前方法结束。


    Ireturn
  • 2.4 LineNumberTable_attribute

attribute_info { 
    u2 attribute_name_index; 
    u4 attribute_length; 
    u1 info[attribute_length]; 
}

由于attribute_name_index指向的是常量池中的LineNumberTable,因此,该项attribute为LineNumberTable_attribute

LineNumberTable_attribute { 
    u2 attribute_name_index; 
    u4 attribute_length; 
    u2 line_number_table_length;
    { 
        u2 start_pc; 
        u2 line_number; 
    } line_number_table[line_number_table_length]; 
}
Option Values Description
attribute_name_index 0x0B 指向常量池中的第11个常量,LineNumberTable
attribute_length 0x00 06 当前属性的长度,不包括开始的6个字节
line_number_table_length 0x00 01 line_number_table[]数组的成员个数为1
start_pc 0x00 00 code[]数组在该索引处的字符表示源文件中新的行的起点
line_number 0x00 02 line_number项的值必须与源文件的行数相匹配

3. attributes_count、attributes

attribute_info表结构如下:

attribute_info { 
    u2 attribute_name_index; 
    u4 attribute_length; 
    u1 info[attribute_length]; 
}

由于attribute_name_index指向的是常量池中的SourceFile,因此,该项attribute为SourceFile_attribute

SourceFile_attribute { 
    u2 attribute_name_index; 
    u4 attribute_length; 
    u2 sourcefile_index; 
}
Option Values Description
attributes_count 0x00 01 属性个数为1
attribute_name_index 0x00 0C 指向常量池中的SourceFile
attribute_length 0x00 00 02 属性长度2
sourcefile_index 0x00 0D 执行常量池中的TestClass.java
attributes字段

4. 参考

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容