JVM Notes - 2

The Java Class File
First the java programmer should know is that the class file is precisely defined
binary file(8-biy bytes). Every bit in it should be meaningful and can be validated
by the JVM.

The following is the item which defined in the class file sequencely:
1. Magic :
The identifier of the java class file which is the constant(0xCAFEBABE),
The derivation of this number is funny, it is the reference to a waiter in the Peet's
Coffee.

2. minor version and major_version.
As the java technology is growing fast, every time a new version comes, the format
of the class file may change, that there is such tag kept in the class file to
identify whether the class file is compatible for the JVM that user uses.
Eg.
0003    3 minor_version
002D    45 major_version

3. constant_pool_count
Eg.
0011    17 constant_pool_count
Indicates how many constant exist in the class file, it is also the total length
of constant_pool array. Note the zeroeth element does not appear in the class file
([:?:] where does it appear), JVM will expect to find element 1 to 16.

4. constant_pool
Every constants pool starts with a one-byte tag which indicates the type of constant.

Eg.
07    7 tag
000C    12 name_index

when the JVM encounter the 7, it will get know that the current entry(not byte) is the
CONTANT_Class_info, base on the JVM spec

java.sun.com/download/latest/java.sun.com/docs/books/vmspec/2nd-edition/html/ClassFile.doc.html#1221

CONSTANT_Class_info {
    u1 tag;//a single unsigned byte
    u2 name_index;//2 unsigned byte, it should be a valid index of the constant_pool table
                  //it is the symbolic reference to a class or interface.
}
From the point 3. the range is 1 to 16. Let's jump to constant_pool[12]
Eg.
01     1  tag
0010     16  length
6A6176612F6C616E672F4F626A656374  "java/lang/Object" bytes[length]
It is a Utf-8 entry
CONSTANT_Utf8_info {
     u1 tag;
     u2 length;
     u1 bytes[length];
}
From all those, JVM will know the super class is the object as it does not
explicitly specify any super calss. This procedure is to resolve the symbolic.

Actually, constant_pool contains:
1. class/interface info
2. field info
3. method info
And those ensure that the constant_pool will play an important role in the dynamic linking.


5. access_flag
The access_flag is followed by constant_pool. which can be the following possible values
ACC_PUBLIC     0x0001
ACC_FINAL      0x0010
ACC_SUPER      0x0020
ACC_INTERFACE  0x0200
ACC_ABSTRACT   0x0400
such flag is two byte, as you seen in the above definition, every flag is two byte then
how can the class tell the JVM it is the public final class? good question, see the
following example you will know about these :
public final = (ACC_PUBLIC | ACC_FINAL) = 00011;
: ), quite smart.

6. this class
this_class should be next to access flag .
0002  2 this_class

the detail of contant_pool[2]:
07    7 tag
000D  13 name_index
then jump to constant_pool[13]
01  1  tag
0003  3  length
416374  "Act"  bytes[length]
It indicates this_class is the Act class
7. supper class
this class should be next to supper class.
0001  1 super_class

the detail of contant_pool[1]:
07    7 tag
000C    12 name_index
then jump to constant_pool[12]
01     1  tag
0010    16  length
6A6176612F6C616E672F4F626A656374  "java/lang/Object" bytes[length]
It indicates the super_class is the Object.

8. interface count and interfaces
the interface count is the length of the interfaces array, which indicates
how many interface such class is implemented, the item of interfaces array
is the reference to constant_pool.

9. fields count and fields
similar with above

10. methods_count ad methods
eg.
0009   access_flags
0006  6 name_index
0010  16 descriptor_index

take a look at constant_pool[6]
01      1   tag
000D    13   length
646F4D617468466F7265766572  "doMathForever"  bytes[length]
it indicates the method name
take a look at constant_pool[16]
01      1 tag
0003  3 length
282956  "()V" bytes[length]
it indicates the method has no argument and no return values.

also it defines the stak(max) size and local variable size.

11. attribute_count and attributes
attribute is something like the exceptions, innerclasses and so forth.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值