美文网首页
JVM class 文件结构

JVM class 文件结构

作者: yangweigbh | 来源:发表于2018-01-18 20:58 被阅读33次

class 文件的结构如下:

ClassFile {
    u4          magic;
    u2          minor_version;
    u2          major_version;
    u2          constant_pool_count;
    cp_info     contant_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];
}
Constant Pool

每个Constant Pool Entry由cp_info结构来表示

cp_info {
    u1 tag;
    u1 info[];
}

tag 表示constant type:

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

不同tag对应的info[]不同。

类的属性

access_flags :类的访问属性
this_class:index to constant pool,指向一个CONSTANT_Class_info。当前类
super_class:index to constant pool(0或者valid index),指向一个CONSTANT_Class_info。当前类的父类

interface

interfaces:index to constant pool,指向一个CONSTANT_Class_info,当前类实现的接口。

fields

field_info的数组:

field_info {
    u2             access_flags;
    u2             name_index;    //index to constant pool, field的name
    u2             descriptor_index; //index to constant pool, field的type
    u2             attributes_count;
    attribute_info attributes[attributes_count];
}
methods
method_info {
    u2             access_flags;
    u2             name_index;
    u2             descriptor_index;
    u2             attributes_count;
    attribute_info attributes[attributes_count];
}

method_info中的attribute_info包括:Code , Exceptions , Synthetic , Signature , Deprecated , RuntimeVisibleAnnotations , RuntimeInvisibleAnnotations , RuntimeVisibleParameterAnnotations , RuntimeInvisibleParameterAnnotations , AnnotationDefault

其中Code attribute_info包含了method的字节码:

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

包含了当前class的一些attribute_info

Reference:

https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html

相关文章

  • ASM 库的介绍和使用

    前面几篇文章介绍了 .class 文件的结构、JVM 如何加载 .class 文件、JVM 中如何执行方法的调用和...

  • 访问者模式和 ASM

    之前三篇文章介绍了 .class 文件的结构、JVM 对 .class 文件加载以及在 JVM 中是怎么执行程序的...

  • JVM内存结构与GC

    JVM体系结构 类装载器ClassLoader 负责加载class文件,class文件在文件开头有特定的文件标示,...

  • JVM Class文件结构

    在class文件中,有两种数据类型:无符号数和表,其他的都是数据了无符号数:以u1、u2、u4、u8来分别代表1个...

  • JVM class 文件结构

    class 文件的结构如下: Constant Pool 每个Constant Pool Entry由cp_inf...

  • JVM学习笔记一

    JVM运行机制 JVM的启动过程 JVM的基本结构 JVM通过ClassLoader将class文件加载到内存中 ...

  • JVM了解

    java程序运行流程草图 说明:.java编译成面向jvm的class文件,供jvm解释执行 JVM结构 JVM的...

  • dex文件与class文件如何生成

    一、dex文件与class文件如何生成Class文件:可以被JVM所识别的文件。记录一个类文件的所有信息。文件结构...

  • JVM执行系统

    2.10 JVM执行系统 2.10.1 类文件结构 JVM是不和Java语言强绑定的,它只与Class文件这种特定...

  • 理解 JVM 中的类加载机制(读书笔记)

    上篇文章中,我们介绍了 .class 文件的结构,.class 文件只是一个静态的文件,那 JVM 是加载 .cl...

网友评论

      本文标题:JVM class 文件结构

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