asm之ClassFile

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

在method_info结构中,方法当中方法体的代码,是存在于Code属性结构中,其结构如下:

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];
}
public class HelloWorld extends Exception implements Cloneable, Serializable {
    private static final int intValue = 10;
    public void test() {
        int a = 1;
        int b = 2;
        int c = a + b;
    }
}

2、ClassFile之HelloWorld的字节码

CA FE BA BE 00 00 00 34 00 2F 0A 00 06 00 1F 09 00 20 00 21 08 00 22 0A 00 23 00 24 07 00 25 07
00 26 07 00 27 07 00 28 01 00 08 69 6E 74 56 61 6C 75 65 01 00 01 49 01 00 0D 43 6F 6E 73 74 61
6E 74 56 61 6C 75 65 03 00 00 00 0A 01 00 06 3C 69 6E 69 74 3E 01 00 03 28 29 56 01 00 04 43 6F
64 65 01 00 0F 4C 69 6E 65 4E 75 6D 62 65 72 54 61 62 6C 65 01 00 12 4C 6F 63 61 6C 56 61 72 69
61 62 6C 65 54 61 62 6C 65 01 00 04 74 68 69 73 01 00 1B 4C 6E 65 74 2F 61 73 6D 2F 73 61 6D 70
6C 65 2F 48 65 6C 6C 6F 57 6F 72 6C 64 3B 01 00 04 74 65 73 74 01 00 01 61 01 00 01 62 01 00 01
63 01 00 04 6D 61 69 6E 01 00 16 28 5B 4C 6A 61 76 61 2F 6C 61 6E 67 2F 53 74 72 69 6E 67 3B 29
56 01 00 04 61 72 67 73 01 00 13 5B 4C 6A 61 76 61 2F 6C 61 6E 67 2F 53 74 72 69 6E 67 3B 01 00
10 4D 65 74 68 6F 64 50 61 72 61 6D 65 74 65 72 73 01 00 0A 53 6F 75 72 63 65 46 69 6C 65 01 00
0F 48 65 6C 6C 6F 57 6F 72 6C 64 2E 6A 61 76 61 0C 00 0D 00 0E 07 00 29 0C 00 2A 00 2B 01 00 0A
48 65 6C 6C 6F 57 6F 72 6C 64 07 00 2C 0C 00 2D 00 2E 01 00 19 6E 65 74 2F 61 73 6D 2F 73 61 6D
70 6C 65 2F 48 65 6C 6C 6F 57 6F 72 6C 64 01 00 13 6A 61 76 61 2F 6C 61 6E 67 2F 45 78 63 65 70
74 69 6F 6E 01 00 13 6A 61 76 61 2F 6C 61 6E 67 2F 43 6C 6F 6E 65 61 62 6C 65 01 00 14 6A 61 76
61 2F 69 6F 2F 53 65 72 69 61 6C 69 7A 61 62 6C 65 01 00 10 6A 61 76 61 2F 6C 61 6E 67 2F 53 79
73 74 65 6D 01 00 03 6F 75 74 01 00 15 4C 6A 61 76 61 2F 69 6F 2F 50 72 69 6E 74 53 74 72 65 61
6D 3B 01 00 13 6A 61 76 61 2F 69 6F 2F 50 72 69 6E 74 53 74 72 65 61 6D 01 00 07 70 72 69 6E 74
6C 6E 01 00 15 28 4C 6A 61 76 61 2F 6C 61 6E 67 2F 53 74 72 69 6E 67 3B 29 56 00 21 00 05 00 06
00 02 00 07 00 08 00 01 00 1A 00 09 00 0A 00 01 00 0B 00 00 00 02 00 0C 00 03 00 01 00 0D 00 0E
00 01 00 0F 00 00 00 2F 00 01 00 01 00 00 00 05 2A B7 00 01 B1 00 00 00 02 00 10 00 00 00 06 00
01 00 00 00 05 00 11 00 00 00 0C 00 01 00 00 00 05 00 12 00 13 00 00 00 01 00 14 00 0E 00 01 00
0F 00 00 00 5D 00 02 00 04 00 00 00 09 04 3C 05 3D 1B 1C 60 3E B1 00 00 00 02 00 10 00 00 00 12
00 04 00 00 00 09 00 02 00 0A 00 04 00 0B 00 08 00 0C 00 11 00 00 00 2A 00 04 00 00 00 09 00 12
00 13 00 00 00 02 00 07 00 15 00 0A 00 01 00 04 00 05 00 16 00 0A 00 02 00 08 00 01 00 17 00 0A
00 03 00 09 00 18 00 19 00 02 00 0F 00 00 00 37 00 02 00 01 00 00 00 09 B2 00 02 12 03 B6 00 04
B1 00 00 00 02 00 10 00 00 00 0A 00 02 00 00 00 0F 00 08 00 10 00 11 00 00 00 0C 00 01 00 00 00
09 00 1A 00 1B 00 00 00 1C 00 00 00 05 01 00 1A 00 00 00 01 00 1D 00 00 00 02 00 1E

3、ClassFile之HelloWorld的字节码

字节码是由16进制表示的,两个16进制数代表一个字节。

magic_number='CAFEBABE'  // 根据ClassFile规范之4个字节的magic  8个十六进制数即4个字节
version='00000034'
constant_pool_count='002F' (47) // 常量池统计数  2F ~ 00100111 ~ 转换为十进制为47
constant_pool
    |001| CONSTANT_Methodref {Value='#6.#31', HexCode='0A0006001F'}
    |002| CONSTANT_Fieldref {Value='#32.#33', HexCode='0900200021'}
    |003| CONSTANT_String {Value='#34', HexCode='080022'}
    |004| CONSTANT_Methodref {Value='#35.#36', HexCode='0A00230024'}
    |005| CONSTANT_Class {Value='#37', HexCode='070025'}// 查看 #37
    |006| CONSTANT_Class {Value='#38', HexCode='070026'}// 查看 #38
    |007| CONSTANT_Class {Value='#39', HexCode='070027'}
    |008| CONSTANT_Class {Value='#40', HexCode='070028'}
    |009| CONSTANT_Utf8 {Value='intValue', HexCode='010008696E7456616C7565'}
    |010| CONSTANT_Utf8 {Value='I', HexCode='01000149'} // 大写 I 表示int类型
    |011| CONSTANT_Utf8 {Value='ConstantValue', HexCode='01000D436F6E7374616E7456616C7565'}
    |012| CONSTANT_Integer {Value='10', HexCode='030000000A'}
    |013| CONSTANT_Utf8 {Value='', HexCode='0100063C696E69743E'}
    |014| CONSTANT_Utf8 {Value='()V', HexCode='010003282956'}
    |015| CONSTANT_Utf8 {Value='Code', HexCode='010004436F6465'}
    |016| CONSTANT_Utf8 {Value='LineNumberTable', HexCode='01000F4C696E654E756D6265725461626C65'}
    |017| CONSTANT_Utf8 {Value='LocalVariableTable', HexCode='0100124C6F63616C5661726961626C655461626C65'}
    |018| CONSTANT_Utf8 {Value='this', HexCode='01000474686973'}
    |019| CONSTANT_Utf8 {Value='Lnet/asm/sample/HelloWorld;', HexCode='01001B4C6E65742F61736D2F73616D706C652F48656C6C6F576F726C643B'}
    |020| CONSTANT_Utf8 {Value='test', HexCode='01000474657374'}
    |021| CONSTANT_Utf8 {Value='a', HexCode='01000161'}
    |022| CONSTANT_Utf8 {Value='b', HexCode='01000162'}
    |023| CONSTANT_Utf8 {Value='c', HexCode='01000163'}
    |024| CONSTANT_Utf8 {Value='main', HexCode='0100046D61696E'}
    |025| CONSTANT_Utf8 {Value='([Ljava/lang/String;)V', HexCode='010016285B4C6A6176612F6C616E672F537472696E673B2956'}
    |026| CONSTANT_Utf8 {Value='args', HexCode='01000461726773'}
    |027| CONSTANT_Utf8 {Value='[Ljava/lang/String;', HexCode='0100135B4C6A6176612F6C616E672F537472696E673B'}
    |028| CONSTANT_Utf8 {Value='MethodParameters', HexCode='0100104D6574686F64506172616D6574657273'}
    |029| CONSTANT_Utf8 {Value='SourceFile', HexCode='01000A536F7572636546696C65'}
    |030| CONSTANT_Utf8 {Value='HelloWorld.java', HexCode='01000F48656C6C6F576F726C642E6A617661'}
    |031| CONSTANT_NameAndType {Value='#13:#14', HexCode='0C000D000E'}
    |032| CONSTANT_Class {Value='#41', HexCode='070029'}
    |033| CONSTANT_NameAndType {Value='#42:#43', HexCode='0C002A002B'}
    |034| CONSTANT_Utf8 {Value='HelloWorld', HexCode='01000A48656C6C6F576F726C64'}
    |035| CONSTANT_Class {Value='#44', HexCode='07002C'}
    |036| CONSTANT_NameAndType {Value='#45:#46', HexCode='0C002D002E'}
    |037| CONSTANT_Utf8 {Value='net/asm/sample/HelloWorld', HexCode='0100196E65742F61736D2F73616D706C652F48656C6C6F576F726C64'}
    |038| CONSTANT_Utf8 {Value='java/lang/Exception', HexCode='0100136A6176612F6C616E672F457863657074696F6E'}
    |039| CONSTANT_Utf8 {Value='java/lang/Cloneable', HexCode='0100136A6176612F6C616E672F436C6F6E6561626C65'}
    |040| CONSTANT_Utf8 {Value='java/io/Serializable', HexCode='0100146A6176612F696F2F53657269616C697A61626C65'}
    |041| CONSTANT_Utf8 {Value='java/lang/System', HexCode='0100106A6176612F6C616E672F53797374656D'}
    |042| CONSTANT_Utf8 {Value='out', HexCode='0100036F7574'}
    |043| CONSTANT_Utf8 {Value='Ljava/io/PrintStream;', HexCode='0100154C6A6176612F696F2F5072696E7453747265616D3B'}
    |044| CONSTANT_Utf8 {Value='java/io/PrintStream', HexCode='0100136A6176612F696F2F5072696E7453747265616D'}
    |045| CONSTANT_Utf8 {Value='println', HexCode='0100077072696E746C6E'}
    |046| CONSTANT_Utf8 {Value='(Ljava/lang/String;)V', HexCode='010015284C6A6176612F6C616E672F537472696E673B2956'}
class_info='002100050006000200070008'
    access_flags='0021' ([ACC_PUBLIC,ACC_SUPER]) // 0021 可以表示修饰符 PUBLIC 
    this_class='0005' (#5) // 表示通过常量池中序列5的表示 即当前class之HelloWorld
    super_class='0006' (#6) //当前class的父类
    interfaces_count='0002' (2) // 当前类实现的接口数
    interfaces='00070008' ([#7,#8]) // 真正实现的接口类型
fields_count='0001' (1)// 当前类只有一个属性字段
fields
|000| intValue:I //属性字段名
HexCode: 001A0009000A0001000B00000002000C
access_flags='001A'([ACC_PRIVATE,ACC_STATIC,ACC_FINAL]) // 字段修饰符
name_index='0009'(#9)
descriptor_index='000A'(#10)
attributes_count='0001'(1)
--->ConstantValue=000B00000002000C

methods_count='0002' (2) // 当前类中存在2个方法
methods
|000| <init>:()V // 默认无参的构造方法
HexCode: 0001000D000E0001000F0000002F00010001000000052AB70001B10000000200100000000600010000000500110000000C000100000005001200130000
access_flags='0001'([ACC_PUBLIC])
name_index='000D'(#13)
descriptor_index='000E'(#14)
attributes_count='0001'(1)
--->Code=000F0000002F00010001000000052AB70001B10000000200100000000600010000000500110000000C000100000005001200130000 // 方法对应的code属性

|001| test:()V //test方法
HexCode: 00010014000E0001000F0000005D0002000400000009043C053D1B1C603EB1000000020010000000120004000000090002000A0004000B0008000C00110000002A000400000009001200130000000200070015000A0001000400050016000A0002000800010017000A0003
access_flags='0001'([ACC_PUBLIC])
name_index='0014'(#20)
descriptor_index='000E'(#14)//方法返回值类型&参数类型
attributes_count='0001'(1)
--->Code=000F0000005D0002000400000009043C053D1B1C603EB1000000020010000000120004000000090002000A0004000B0008000C00110000002A000400000009001200130000000200070015000A0001000400050016000A0002000800010017000A0003

--->MethodParameters=001C0000000501001A0000

attributes_count='0001' (1)
attributes
--->|000| SourceFile:
HexCode: 001D00000002001E
attribute_name_index='001D' (#29)
attribute_length='00000002' (2)
info='001E'

4、method_info 结构中Code属性结构中Code字段

constant_pool_count='001C' (28)
constant_pool
    |001| CONSTANT_Methodref {Value='java/lang/Exception.<init>:()V', HexCode='0A00030017'}
    |002| CONSTANT_Class {Value='net/asm/sample/HelloWorld', HexCode='070018'}
    |003| CONSTANT_Class {Value='java/lang/Exception', HexCode='070019'}
    |004| CONSTANT_Class {Value='java/lang/Cloneable', HexCode='07001A'}
    |005| CONSTANT_Class {Value='java/io/Serializable', HexCode='07001B'}
    |006| CONSTANT_Utf8 {Value='intValue', HexCode='010008696E7456616C7565'}
    |007| CONSTANT_Utf8 {Value='I', HexCode='01000149'}
    |008| CONSTANT_Utf8 {Value='ConstantValue', HexCode='01000D436F6E7374616E7456616C7565'}
    |009| CONSTANT_Integer {Value='10', HexCode='030000000A'}
    |010| CONSTANT_Utf8 {Value='', HexCode='0100063C696E69743E'}
    |011| CONSTANT_Utf8 {Value='()V', HexCode='010003282956'}
    |012| CONSTANT_Utf8 {Value='Code', HexCode='010004436F6465'}
    |013| CONSTANT_Utf8 {Value='LineNumberTable', HexCode='01000F4C696E654E756D6265725461626C65'}
    |014| CONSTANT_Utf8 {Value='LocalVariableTable', HexCode='0100124C6F63616C5661726961626C655461626C65'}
    |015| CONSTANT_Utf8 {Value='this', HexCode='01000474686973'}
    |016| CONSTANT_Utf8 {Value='Lnet/asm/sample/HelloWorld;', HexCode='01001B4C6E65742F61736D2F73616D706C652F48656C6C6F576F726C643B'}
    |017| CONSTANT_Utf8 {Value='test', HexCode='01000474657374'}
    |018| CONSTANT_Utf8 {Value='a', HexCode='01000161'}
    |019| CONSTANT_Utf8 {Value='b', HexCode='01000162'}
    |020| CONSTANT_Utf8 {Value='c', HexCode='01000163'}
    |021| CONSTANT_Utf8 {Value='SourceFile', HexCode='01000A536F7572636546696C65'}
    |022| CONSTANT_Utf8 {Value='HelloWorld.java', HexCode='01000F48656C6C6F576F726C642E6A617661'}
    |023| CONSTANT_NameAndType {Value='<init>:()V', HexCode='0C000A000B'}
    |024| CONSTANT_Utf8 {Value='net/asm/sample/HelloWorld', HexCode='0100196E65742F61736D2F73616D706C652F48656C6C6F576F726C64'}
    |025| CONSTANT_Utf8 {Value='java/lang/Exception', HexCode='0100136A6176612F6C616E672F457863657074696F6E'}
    |026| CONSTANT_Utf8 {Value='java/lang/Cloneable', HexCode='0100136A6176612F6C616E672F436C6F6E6561626C65'}
    |027| CONSTANT_Utf8 {Value='java/io/Serializable', HexCode='0100146A6176612F696F2F53657269616C697A61626C65'}
=== === ===  === === ===  === === ===
Method test:()V
=== === ===  === === ===  === === ===
max_stack = 2
max_locals = 4
code_length = 9
code = 043C053D1B1C603EB1  
=== === ===  === === ===  === === ===
//左边值表示其对应16进制数在code中下标
0000: iconst_1 //变量实际取值            // 04
0001: istore_1 //实际变量值对应 LocalVariableTable中index对应的变量赋值        // 3C
0002: iconst_2             // 05
0003: istore_2             // 3D
0004: iload_1              // 1B
0005: iload_2              // 1C
0006: iadd                 // 60
0007: istore_3             // 3E
0008: return               // B1
=== === ===  === === ===  === === ===
LocalVariableTable:
index  start_pc  length  name_and_type
    0         0       9  this:Lnet/asm/sample/HelloWorld;
    1         2       7  a:I
    2         4       5  b:I
    3         8       1  c:I

你可能感兴趣的:(java,数学建模,开发语言)