class文件 简单分析
== == == == == == == == Person. java== == == == == == == == == == =
package com. lt. test;
public class Person {
public int mAge = 0 ;
public String mName = "" ;
public Person ( ) { }
public Person ( int age, String name) {
mAge = age;
mName = name;
}
}
== == == == == == == == == Person. class == == == == == == == == == =
++ ++ ++ ++ ++ ++ ++ ++ ++ ++ ++ ++ ++ ++ ++ ++ +
CA FE BA BE 00 00 00 34 00 1D 07 00 02 01 00 12
63 6F 6D 2F 6 C 74 2F 74 65 73 74 2F 50 65 72 73
6F 6 E 07 00 04 01 00 10 6 A 61 76 61 2F 6 C 61 6 E
67 2F 4F 62 6 A 65 63 74 01 00 04 6D 41 67 65 01
00 01 49 01 00 05 6D 4 E 61 6D 65 01 00 12 4 C 6 A
61 76 61 2F 6 C 61 6 E 67 2F 53 74 72 69 6 E 67 3 B
01 00 06 3 C 69 6 E 69 74 3 E 01 00 03 28 29 56 01
00 04 43 6F 64 65 0 A 00 03 00 0D 0 C 00 09 00 0 A
09 00 01 00 0F 0 C 00 05 00 06 08 00 11 01 00 00
09 00 01 00 13 0 C 00 07 00 08 01 00 0F 4 C 69 6 E
65 4 E 75 6D 62 65 72 54 61 62 6 C 65 01 00 12 4 C
6F 63 61 6 C 56 61 72 69 61 62 6 C 65 54 61 62 6 C
65 01 00 04 74 68 69 73 01 00 14 4 C 63 6F 6D 2F
6 C 74 2F 74 65 73 74 2F 50 65 72 73 6F 6 E 3 B 01
00 16 28 49 4 C 6 A 61 76 61 2F 6 C 61 6 E 67 2F 53
74 72 69 6 E 67 3 B 29 56 01 00 03 61 67 65 01 00
04 6 E 61 6D 65 01 00 0 A 53 6F 75 72 63 65 46 69
6 C 65 01 00 0 B 50 65 72 73 6F 6 E 2 E 6 A 61 76 61
00 21 00 01 00 03 00 00 00 02 00 01 00 05 00 06
00 00 00 01 00 07 00 08 00 00 00 02 00 01 00 09
00 0 A 00 01 00 0 B 00 00 00 46 00 02 00 01 00 00
00 10 2 A B7 00 0 C 2 A 03 B5 00 0 E 2 A 12 10 B5 00
12 B1 00 00 00 02 00 14 00 00 00 12 00 04 00 00
00 07 00 04 00 05 00 09 00 06 00 0F 00 07 00 15
00 00 00 0 C 00 01 00 00 00 10 00 16 00 17 00 00
00 01 00 09 00 18 00 01 00 0 B 00 00 00 6 C 00 02
00 03 00 00 00 1 A 2 A B7 00 0 C 2 A 03 B5 00 0 E 2 A
12 10 B5 00 12 2 A 1 B B5 00 0 E 2 A 2 C B5 00 12 B1
00 00 00 02 00 14 00 00 00 1 A 00 06 00 00 00 08
00 04 00 05 00 09 00 06 00 0F 00 09 00 14 00 0 A
00 19 00 0 B 00 15 00 00 00 20 00 03 00 00 00 1 A
00 16 00 17 00 00 00 00 00 1 A 00 19 00 06 00 01
00 00 00 1 A 00 1 A 00 08 00 02 00 01 00 1 B 00 00
00 02 00 1 C
++ ++ ++ ++ ++ ++ ++ ++ ++ ++ ++ ++ ++ ++ ++ ++ +
== == == == == == == == == Person. class hex 分析 == == == == == == == == == =
classFile文件格式:
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] ; / 属性表
}
== == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == =
CA FE BA BE magic
00 00 minor_version
00 34 major_version
00 1D ( 16 + 13 = 29 ) ( 29 - 1 = 28 ,常量池中共28 个常量)
当前是第10 个字节:0 1 2 4 5 . . . 9 a
# == == == == == == == == == == == = 》 第 1 个元素
07 tag(7 ): CONSTANT_Class_info
00 02 name_index 指向 CONSTANT_Utf8_info com/ lt/ test/ Person
# == == == == == == == == == == == = 》 第 2 个元素
01 tag(1 ): CONSTANT_Utf8_info
00 12 length 18
63 6F 6D 2F 6 C 74 2F 74 65 73 74 2F 50 65 72 73 6F 6 E com/ lt/ test/ Person
# == == == == == == == == == == == = 》 第 3 个元素
07 tag(7 ): CONSTANT_Class_info
00 04 name_index 指向 CONSTANT_Utf8_info java/ lang/ Object
# == == == == == == == == == == == = 》 第 4 个元素
01 tag(1 ): CONSTANT_Utf8_info
00 10 length
6 A 61 76 61 2F 6 C 61 6 E 67 2F 4F 62 6 A 65 63 74 java/ lang/ Object
# == == == == == == == == == == == = 》 第 5 个元素
01 tag(1 ): CONSTANT_Utf8_info
00 04 length
6D 41 67 65 mAge
# == == == == == == == == == == == = 》 第 6 个元素
01 tag(1 ): CONSTANT_Utf8_info
00 01 length
49 I
# == == == == == == == == == == == = 》 第 7 个元素
01 tag(1 ): CONSTANT_Utf8_info
00 05 length
6D 4 E 61 6D 65 mName
# == == == == == == == == == == == = 》 第 8 个元素
01 tag(1 ): CONSTANT_Utf8_info
00 12 length
4 C 6 A 61 76 61 2F 6 C 61 6 E 67 2F 53 74 72 69 6 E 67 3 B Ljava/ lang/ String; ( 18 )
# == == == == == == == == == == == = 》 第 9 个元素
01 tag(1 ): CONSTANT_Utf8_info
00 06 length
3 C 69 6 E 69 74 3 E < init>
# == == == == == == == == == == == = 》 第 10 个元素
01 tag(1 ): CONSTANT_Utf8_info
00 03 length
28 29 56 ( ) V
# == == == == == == == == == == == = 》 第 11 个元素
01 tag(1 ): CONSTANT_Utf8_info
00 04 length
43 6F 64 65 Code
# == == == == == == == == == == == = 》 第 12 个元素
0 A tag(10 ): CONSTANT_Methodref_info
00 03 class_index 3 == > CONSTANT_Class_info == > java/ lang/ Object
00 0D name_and_type_index 13 == 》 CONSTANT_NameAndType_info == > < init> 方法(类型:( ) V)
# == == == == == == == == == == == = 》 第 13 个元素
0 C tag(12 ): CONSTANT_NameAndType_info
00 09 name_index: 9 == > CONSTANT_Utf8_info == > < init>
00 0 A descriptor_index: 10 == > CONSTANT_Utf8_info == > ( ) V
# == == == == == == == == == == == = 》 第 14 个元素
09 tag(9 ): CONSTANT_Fieldref_info
00 01 class_index 1 == 》 CONSTANT_Class_info == 》 com/ lt/ test/ Person
00 0F name_and_type_index 15 == > CONSTANT_NameAndType_info == > mAge属性(类型:I)
# == == == == == == == == == == == = 》 第 15 个元素
0 C tag(12 ): CONSTANT_NameAndType_info
00 05 name_index: 5 == > CONSTANT_Utf8_info == > mAge
00 06 descriptor_index: 6 == > CONSTANT_Utf8_info == > I
# == == == == == == == == == == == = 》 第 16 个元素 ( 10 )
08 tag(8 ): CONSTANT_String_info
00 11 string_index 11 == > CONSTANT_Utf8_info == > Code
# == == == == == == == == == == == = 》 第 17 个元素
01 tag(1 ): CONSTANT_Utf8_info
00 00 length: 0
# == == == == == == == == == == == = 》 第 18 ( 0x12 ) 个元素
09 tag(9 ): CONSTANT_Fieldref_info
00 01 class_index 1 == 》 CONSTANT_Class_info == 》 com/ lt/ test/ Person
00 13 name_and_type_index 19 == > CONSTANT_NameAndType_info == >
# == == == == == == == == == == == = 》 第 19 个元素
0 C tag(12 ): CONSTANT_NameAndType_info
00 07 name_index: 7 == > CONSTANT_Utf8_info == > mName
00 08 descriptor_index: 8 == > CONSTANT_Utf8_info == > Ljava/ lang/ String;
* * * * * * * * * * * * * * * * * * * * * * * * * * * * *
# == == == == == == == == == == == = 》 第 20 个元素
01 tag(1 ): CONSTANT_Utf8_info
00 0F length
4 C 69 6 E 65 4 E 75 6D 62 65 72 54 61 62 6 C 65 LineNumberTable
# == == == == == == == == == == == = 》 第 21 个元素
01 tag(1 ): CONSTANT_Utf8_info
00 12 length
4 C 6F 63 61 6 C 56 61 72 69 61 62 6 C 65 54 61 62 6 C 65 LocalVariableTable
# == == == == == == == == == == == = 》 第 22 个元素
01 tag(1 ): CONSTANT_Utf8_info
00 04 length
74 68 69 73 this
# == == == == == == == == == == == = 》 第 23 个元素
01 tag(1 ): CONSTANT_Utf8_info
00 14 length
4 C 63 6F 6D 2F 6 C 74 2F 74 65 73 74 2F 50 65 72 73 6F 6 E 3 B Lcom/ lt/ test/ Person;
# == == == == == == == == == == == = 》 第 24 个元素
01 tag(1 ): CONSTANT_Utf8_info
00 16 length
28 49 4 C 6 A 61 76 61 2F 6 C 61 6 E 67 2F 53 74 72 69 6 E 67 3 B 29 56 ( ILjava/ lang/ String; ) V
# == == == == == == == == == == == = 》 第 25 个元素
01 tag(1 ): CONSTANT_Utf8_info
00 03 length
61 67 65 age
# == == == == == == == == == == == = 》 第 26 个元素
01 tag(1 ): CONSTANT_Utf8_info
00 04 length
6 E 61 6D 65 name
# == == == == == == == == == == == = 》 第 27 个元素
01 tag(1 ): CONSTANT_Utf8_info
00 0 A length
53 6F 75 72 63 65 46 69 6 C 65 SourceFile
# == == == == == == == == == == == = 》 第 28 个元素
01 tag(1 ): CONSTANT_Utf8_info
00 0 B length ( 11 )
50 65 72 73 6F 6 E 2 E 6 A 61 76 61 Person. java
00 21 access_flags: ( 类的访问标志以及类型信息) 20 + 1 : ACC_PUBLIC, ACC_SUPER
00 01 this_class == > CONSTANT_Class_info == > com/ lt/ test/ Person
00 03 super_class == > CONSTANT_Class_info == > java/ lang/ Object
00 00 interfaces_count 0
00 02 fields_count
#########################
# field_info 1
#########################
00 01 public 类型
00 05 name_index 5 == 》 CONSTANT_Utf8_info == > mAge
00 06 descriptor_index 6 == > CONSTANT_Utf8_info == > I
00 00 attributes_count 0
#########################
# field_info 2
#########################
00 01 public 类型
00 07 name_index 7 == 》 CONSTANT_Utf8_info == > mName
00 08 descriptor_index 8 == > CONSTANT_Utf8_info == > Ljava/ lang/ String;
00 00 attributes_count 0
00 02 methods_count 2
#########################
# method_info 1 ( 函数:void < init> ( ) 的具体内容)
#########################
00 01 access_flags public
00 09 name_index 9 == > CONSTANT_Utf8_info == > < init>
00 0 A descriptor_index 10 == > CONSTANT_Utf8_info == > ( ) V == > 函数:void ( )
00 01 attributes_count 1
attribute_info
00 0 B attribute_name_index == > CONSTANT_Utf8_info == > Code
00 00 00 46 attribute_length 16 * 4 + 6 = 70 byte
00 02 00 01 00 00 00 10 2 A B7
00 0 C 2 A 03 B5 00 0 E 2 A 12 10
B5 00 12 B1 00 00 00 02 00 14
00 00 00 12 00 04 00 00 00 07
00 04 00 05 00 09 00 06 00 0F
00 07 00 15 00 00 00 0 C 00 01
00 00 00 10 00 16 00 17 00 00
#########################
# method_info 2 ( 函数:void < init> ( int , String) 的具体内容)
#########################
00 01 access_flags public
00 09 name_index 9 == > CONSTANT_Utf8_info == > < init>
00 18 descriptor_index 0x18 = 24 == > CONSTANT_Utf8_info == > ( ILjava/ lang/ String; ) V == > 函数:void < init> ( int , String)
00 01 attributes_count 1
attribute_info
00 0 B attribute_name_index == > CONSTANT_Utf8_info == > Code
00 00 00 6 C attribute_length 所占大小:16 * 6 + 12 = 96 + 12 = 108 byte
00 02 00 03 00 00 00 1 A 2 A B7
00 0 C 2 A 03 B5 00 0 E 2 A 12 10
B5 00 12 2 A 1 B B5 00 0 E 2 A 2 C
B5 00 12 B1 00 00 00 02 00 14
00 00 00 1 A 00 06 00 00 00 08
00 04 00 05 00 09 00 06 00 0F
00 09 00 14 00 0 A 00 19 00 0 B
00 15 00 00 00 20 00 03 00 00
00 1 A 00 16 00 17 00 00 00 00
00 1 A 00 19 00 06 00 01 00 00
00 1 A 00 1 A 00 08 00 02
00 01 attributes_count 1
00 1 B attribute_name_index ( 1 B = 16 + 11 = 27 ) == > CONSTANT_Utf8_info == > SourceFile
00 00 00 02 attribute_length 所占大小:2 byte
00 1 C 指向CONSTANT_Utf8_info的索引 1 C = 28 == > CONSTANT_Utf8_info == > Person. java
== == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == ==
== == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == ==
== == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == ==
method_info中attribute_info 类型解析
1. Code Attribute
2. LineNumberTable Attribute
3. Exceptions Attribute
4. RuntimeVisibleParameterAnnotations Attribute
5. RuntimeInvisibleParameterAnnotations Attribute
6. AnnotationDefault Attribute
7. Synthetic Attribute
8. Signature Attribute
9. Deprecated Attribute
10. RuntimeVisibleAnnotations Attribute
11. RuntimeInvisibleAnnotations Attribute
method_info中attribute_info 类型解析
1. StackMapTable Attribute
2. LineNumberTable Attribute
3. LocalVariableTable Attribute
4. LocalVariableTypeTable Attribute
== == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == ==
method_info 1 中的 attribute_info
1. attribute_info (Code Attribute)
00 0 B attribute_name_index == > CONSTANT_Utf8_info == > Code
00 00 00 46 attribute_length (70 )
00 02 max_stack 2
00 01 max_locals 1
00 00 00 10 code_length 16
u1 code[ code_length]
2 A B7 00 0 C 2 A 03 B5 00 0 E 2 A 12 10 B5 00 12 B1
00 00 exception_table_length
00 02 attributes_count 2
attributes[ ] (attribute_info 类型的数组,当前就是attribute_info类型):
【attribute_info 1 (LineNumberTable Attribute)】
00 14 attribute_name_index ( 20 )
00 00 00 12 attribute_length ( 18 )
00 04 line_number_table_length
[ line_number_table]
00 00 start_pc 一条指令的开始索引(字节码数组中的索引号)
00 07 line_number 源代码中的行号
00 04
00 05
00 09
00 06
00 0F
00 07
【attribute_info 2 (LocalVariableTable Attribute)】
00 15 attribute_name_index ( 21 ) == > CONSTANT_Utf8_info == > LocalVariableTable
00 00 00 0 C attribute_length ( 12 )
00 01 local_variable_table_length 1
[ local_variable_table]
00 00 start_pc 记录该局部变量的有值范围,字节码数组中的索引范围[ start_pc, start_pc+ length) 。
00 10 length
00 16 name_index 记录该项代表的局部变量名。0x16 = 22 == > CONSTANT_Utf8_info == > this
00 17 descriptor_index 0x17 = 23 == > CONSTANT_Utf8_info == > Lcom/ lt/ test/ Person;
00 00 index 记录该项代表的局部变量在方法的局部变量数组中的索引
== == == == == == == == == == == == == == = info[ ] ( 函数:void < init> ( int , String) 的具体内容) == == == == == == == == == == == == == == ==
method_info 2 中的 attribute_info
00 0 B attribute_name_index== > CONSTANT_Utf8_info == > Code
00 00 00 6 C attribute_length (108 )
00 02 max_stack
00 03 max_locals
00 00 00 1 A code_length 26
u1 code[ code_length]
2 A B7 00 0 C 2 A 03 B5 00 0 E 2 A 12 10
B5 00 12 2 A 1 B B5 00 0 E 2 A 2 C B5 00
12 B1
00 00 exception_table_length
00 02 attributes_count
attributes[ 2 ] (attribute_info类型的数组,当前就是attribute_info类型):
【attribute_info 1 (LineNumberTable Attribute)】
00 14 attribute_name_index ( 20 ) == > CONSTANT_Utf8_info == > LineNumberTable
00 00 00 1 A attribute_length ( 26 )
00 06 line_number_table_length 6
[ line_number_table]
00 00 start_pc 一条指令的开始索引(字节码数组中的索引号)
00 08 line_number 源代码中的行号
00 04
00 05
00 09
00 06
00 0F
00 09
00 14
00 0 A
00 19
00 0 B
【attribute 2 】
00 15 attribute_name_index ( 21 ) == > CONSTANT_Utf8_info == > LocalVariableTable
00 00 00 20 attribute_length ( 32 )
00 03 line_number_table_length 3
[ local_variable_table]
00 00 start_pc 记录该局部变量的有值范围,字节码数组中的索引范围[ start_pc, start_pc+ length) 。
00 1 A length
00 16 name_index 记录该项代表的局部变量名。0x16 = 22 == > CONSTANT_Utf8_info == > this
00 17 descriptor_index 0x17 = 23 == > CONSTANT_Utf8_info == > Lcom/ lt/ test/ Person;
00 00 index 记录该项代表的局部变量在方法的局部变量数组中的索引
00 00
00 1 A
00 19
00 06
00 01
00 00
00 1 A
00 1 A
00 08
00 02
== == == == == == == == == == == == == == == == == == == == == == =
field_info{
u2 access_flags;
u2 name_index;
u2 descriptor_index;
u2 attributes_count;
attribute_info attributes[ attributes_count]
}
method_info{
u2 access_flags;
u2 name_index;
u2 descriptor_index;
u2 attributes_count;
attribute_info attributes[ attributes_count]
}
attribute_info{
u2 attribute_name_index;
u4 attribute_length;
u1* attribute_length info[ attribute_length] ( info类型:Code_attribute、ConstantValue_attribute、 LineNumberTable_attribute. . . )
}
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] ;
}
ConstantValue_attribute {
u2 attribute_name_index;
u4 attribute_length;
u2 constantvalue_index;
}
LineNumberTable_attribute {
u2 attribute_name_index;
u4 attribute_length;
u2 line_number_table_length;
{
u2 start_pc;
u2 line_number;
} line_number_table[ line_number_table_length] ;
}
LocalVariableTable_attribute {
u2 attribute_name_index;
u4 attribute_length;
u2 local_variable_table_length;
{
u2 start_pc;
u2 length;
u2 name_index;
u2 descriptor_index;
u2 index;
} local_variable_table[ local_variable_table_length] ;
}
LocalVariableTypeTable_attribute {
u2 attribute_name_index;
u4 attribute_length;
u2 local_variable_type_table_length;
{
u2 start_pc;
u2 length;
u2 name_index;
u2 signature_index;
u2 index;
} local_variable_type_table[ local_variable_type_table_length
] ;
StackMapTable_attribute {
u2 attribute_name_index;
u4 attribute_length;
u2 number_of_entries;
stack_map_frame entries[ number_of_entries] ;
}