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

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.
0003    3 minor_version
002D    45 major_version

3. constant_pool_count
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.

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


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]
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
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
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.

个人分类: Java
想对作者说点什么? 我来说一句

2018 FRM Schweser notes part1

2018年03月07日 118.94MB 下载


2018年03月03日 63.41MB 下载



JVM Notes - 2