Skip to main content

Method Reference Expressions

· 2 min read

背景

了解java的class格式java file format

介绍

大部分的类的加载都会依赖defineClass(byte[] b, int off, int len)

顺序就算b =你的class文件的字节流 , 然后这个函数会读字节流,加载,然后塞进matespace

本篇内容

为什么我需要这篇文章:

  • 我看java的动态代理的时候,发现原来其实动态代理和静态编译原来只是接口不一样,实际上大家都是调用这个defineClass(byte[] b, int off, int len) 方法的

我想写一个Proxygenerate的demo , 他就是为了生成class 字节流

开始

我们看看编译好的是什么样子的:

//   touch A.java
public class A{
}

然后编译:

javac A.java
hexdump A.class
0000000 feca beba 0000 4100 0d00 000a 0002 0703
0000010 0400 000c 0005 0106 1000 616a 6176 6c2f
0000020 6e61 2f67 624f 656a 7463 0001 3c06 6e69
0000030 7469 013e 0300 2928 0756 0800 0001 4101
0000040 0001 4304 646f 0165 0f00 694c 656e 754e
0000050 626d 7265 6154 6c62 0165 0a00 6f53 7275
0000060 6563 6946 656c 0001 4106 6a2e 7661 0061
0000070 0021 0007 0002 0000 0000 0001 0001 0005
0000080 0006 0001 0009 0000 001d 0001 0001 0000
0000090 2a05 00b7 b101 0000 0100 0a00 0000 0600
00000a0 0100 0000 0100 0100 0b00 0000 0200 0c00
00000b0

我们开始看classfile structure

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

第一部分是magic,也就是0xCAFEBABE

magic

The magic item supplies the magic number identifying the class file format; it has the value 0xCAFEBABE.

相关阅读