Java Class ファイルを読む(2)

Java Class ファイルを読む(1) - アウトスタンディングなプログラマを目指しての続き。

constant_pool_count

u2 constant_pool_count;
00000000  ca fe ba be 00 00 00 32  00 0f 0a 00 03 00 0c 07  |.......2........|

次の constant_pool の要素数がいくつあるのか。15(0x000f) - 1 の14になる。

constatn_pool

cp_info constant_pool[constant_pool_count-1];

インデックスが1から始まる仕様になっている。なんでだろうか。

cp_info は

cp_info {
    u1 tag;
    u1 info[];
}

になっていて、tagによってinfoの長さ。内容が変わってくる。

tagの値 定数の型
1 CONSTANT_Utf8
7 CONSTANT_Class
10 CONSTANT_Methodref
12 CONSTANT_NameAndType
constant_pool[1]
00000000  ca fe ba be 00 00 00 32  00 0f 0a 00 03 00 0c 07  |.......2........|

tagの値が10(0x0a)なので、CONSTANT_Methodref。
CONSTANT_Methodrefの場合のフォーマットは

CONSTANT_Methodref_info {
    u1 tag;
    u2 class_index;
    u2 name_and_type_index;
}

class_indexもname_and_type_indexもconstant_poolのインデックス。
constant_pool[3]がメソッドのクラスで、constant_pool[12]にメソッド名とメソッドの型(引数の情報かもしれない)があることを示す。

constant_pool[2]
00000000  ca fe ba be 00 00 00 32  00 0f 0a 00 03 00 0c 07  |.......2........|
00000010  00 0d 07 00 0e 01 00 06  3c 69 6e 69 74 3e 01 00  |..........|

tagの値が7なので、CONSTANT_Classの場合のフォーマットは

CONSTANT_Class_info {
    u1 tag;
    u2 name_index;
}

name_indexはconstant_poolのインデックス。
constant_pool[13]がクラスの名前。

constant_pool[3]
00000010  00 0d 07 00 0e 01 00 06  3c 69 6e 69 74 3e 01 00  |..........|

constant_pool[14]がクラスの名前。


続きはJava Class ファイルを読む(3) - アウトスタンディングなプログラマを目指して