美文网首页Java
Class文件解析

Class文件解析

作者: shen_coder | 来源:发表于2019-08-04 22:05 被阅读0次

    Class文件结构

    整体结构

    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];
    }
    

    从结构上看可以分为几大块 文件头、常量池、接口、字段、函数、属性

    文件头

     u4             magic;  //固定值 0xCAFEBABE
     u2             minor_version;
     u2             major_version;
     u2             access_flags;  //访问修饰
     u2             this_class; //类名 常量池下标
     u2             super_class; //父类名 常量池下标  如果是0 就是java/lang/Object;
    </pre></code>
    我把这几个描述了类基本信息的字段称为文件头
    
    major_version.minor_version表示该class文件的版本号,由编译器版本决定,然而不同版本的虚拟机只会支持一定版本范围内的class文件,如果不在则会拒绝解析。
    例如 openJDK中的实现
    <pre><code>
    // Check version numbers - we check this even with verifier off
      if (!is_supported_version(major_version, minor_version)) {
        if (name == NULL) {
          Exceptions::fthrow(
            THREAD_AND_LOCATION,
            vmSymbols::java_lang_UnsupportedClassVersionError(),
            "Unsupported major.minor version %u.%u",
            major_version,
            minor_version);
        } 
    

    常量池

    常量池包含了class文件中使用到的例如 函数标识/类型信息/字符串等等,运行时加载到内存中形成运行时常量池

    常量项中文件中使用变长结构描述

    cp_info {
        u1 tag;  //表示常量的类型
        u1 info[];  //具体常量的内容结构 不同类型常量有不同的结构
    }
    
    /*常量类型*/
    Constant Type   Value
    CONSTANT_Class  7
    CONSTANT_Fieldref   9
    CONSTANT_Methodref  10
    CONSTANT_InterfaceMethodref 11
    CONSTANT_String 8
    CONSTANT_Integer    3
    CONSTANT_Float  4
    CONSTANT_Long   5
    CONSTANT_Double 6
    CONSTANT_NameAndType    12
    CONSTANT_Utf8   1
    CONSTANT_MethodHandle   15
    CONSTANT_MethodType 16
    CONSTANT_InvokeDynamic  18
    
    

    例如:Utf8_info常量,更多的可以查看规范

    CONSTANT_Utf8_info {
        u1 tag;
        u2 length;  //字符串长度
        u1 bytes[length]; //字符串数据(utf-8编码)
    }
    

    解析常量池的时候要注意:常量池长度为 constant_pool_count -1 但是 下标从1开始

    属性表

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

    属性项可以包含在class、method、field、code中,作为具体信息项

    在class中可以描述文件信息,在method中可以描述字节码内容,函数栈信息,在code中可以描述行号等 所以attribute_info同样是具备不同类型的变长结构。但是attribute_info并没有tag这样的专门标记去标识类型,而是使用名字attribute_name。

    //一部分Attribute Name
    Attribute   Section class file  Java SE
    ConstantValue   §4.7.2  45.3    1.0.2
    Code    §4.7.3  45.3    1.0.2
    StackMapTable   §4.7.4  50.0    6
    Exceptions  §4.7.5  45.3    1.0.2
    InnerClasses    §4.7.6  45.3    1.1
    //.......
    

    Code_attribute结构

    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;  //异常处理表 由try,catch/finaly 产生
        {   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];
    }
    

    函数表/字段表

    
    method_info {
        u2             access_flags;
        u2             name_index;
        u2             descriptor_index;
        u2             attributes_count;
    //一定会包含一个code属性项
        attribute_info attributes[attributes_count];
    }
    
    field_info {
        u2             access_flags;
        u2             name_index;
        u2             descriptor_index;
        u2             attributes_count;
        attribute_info attributes[attributes_count];
    }
    
    

    可以看到函数/字段中的内容具体有属性来描述

    字节码解析

    对于class文件解析,我们最关心的可能就两个 常量池和字节码

    一条字节码由操作码,操作数组成,不同的字节码操作数的长度/表示意义可能不一样,对着规范翻译就好

    例如invokevirtual字节码就是 0xb6 u2 2字节的操作数在运行时指向的是一个instance method ref

    参考文档

    1. JVM规范-Class文件结构

    2. JVM规范-字节码

    本文代码

    ClassParserDemo

    相关文章

      网友评论

        本文标题:Class文件解析

        本文链接:https://www.haomeiwen.com/subject/ltnydctx.html