.class文件中方法中的代码藏在那儿

java能将一个类编译成字节码,然后放到虚拟机上执行。

class文件格式

Class文件是一组以8位字节为基础单位的二进制流,各个数据项按顺序紧密的从前向后排列。我们可以将class文件看做一个巨大的结构体,这个类的所有信息全部都在这儿,类似于json。

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

method_info methods[methods_count];这是指里面有一个方法数组,这个数组里面包含的这个类所有方法的信息,这个数组的每一项都是method_info这也是结构体,里面包含了一个方法的所有信息。
这是 method_info结构体的定义伪码

method_info {
    u2             access_flags;
    u2             name_index;//方法名
    u2             descriptor_index;
    u2             attributes_count;
    attribute_info attributes[attributes_count];
}

attributes包含了方法各方面的属性,代码属性也在里面。虽然这里看似是数组形式,实际上每个属性大小应该不一样,所以这里的attributes应该是个包含很多属性的结构体。

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

方法体的二进制代码就在code数组里面。
这里的二进制代码可以就是java的字节码,可以理解为一种平台无关的汇编语言。虚拟机负责将这种汇编翻译成为真正的机器语言。

class的常量池

用来存放类中用到的字符串常量,类名,接口名,方法名,以及实例成员名等等。

参考:
https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html#jvms-4.7.3
https://zh.wikipedia.org/wiki/Java%E5%AD%97%E8%8A%82%E7%A0%81

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值