类文件结构理解之通过Jclasslib实验查看

实验条件

环境:JDK1.8
工具:Jclasslib

Class文件格式

General Information

写个测试代码,打开Jclasslib

public class ClassTest extends SuperClass implements Interface1 {
    String str;

    {
        str = "static";
    }

    private int num = 1;

    public String getString(String str1) throws IOException {
        return str + num + str1;
    }
}

class SuperClass{}

interface Interface1{}

对应上面Class文件格式:

  • magic(通过二进制文件查看)
  • Minor version: 0
  • Major version: 52
  • Constant pool count: 49
  • Constant Pool: (稍后分析)
  • access_flags: 0x0021
  • this_class: cp_info_#10
  • super_class: cp_info_#11
  • interfaces_count: 1
  • interfaces:(稍后分析)
  • fields_count:2
  • fields:(稍后分析)
  • methods_count:2
  • methods:(稍后分析)
  • attributes_count:1
  • attributes:(稍后分析)

interfaces

指向常量表中第12个常量,第12个常量是CONSTANT_Class_info,所以Class name指向第42个常量,Utf8_info,得到接口名。


Constant Pool

最后附有从《深入理解Java虚拟机》所截的常量池中数据结构。

对应此类中则是,如:
CONSTANT_Utf8_info:

  • str
  • num
  • 1
  • this
  • ()V
  • Code
    ......

CONSTANT_Methodref_info

Class name: cp_info_11 <com/java/study/jvm/string/SuperClass>, Name and type: cp_info_#32 <😦)V>

CONSTANT_String_info

String: cp_info_#33

CONSTANT_Fieldref_info

Class name: cp_info_10 <com/java/study/jvm/string/ClassTest>, Name and type: cp_info_#34 <str: Ljava/lang/String;>

CONSTANT_Class_info

Class name: cp_info_40 <com/java/study/jvm/string/ClassTest>

CONSTANT_NameAndType_info

Name: cp_info_44 , Descriptor: cp_info_#45 <(Ljava/lang/String;)Ljava/lang/StringBuilder;>

fields

Methods

Attributes

常量池中数据结构:


posted @ 2020-12-02 14:26  曹自标  阅读(164)  评论(0编辑  收藏  举报