'深入理解Java虚拟机'2018-04-16

Java Class文件结构:


image.png

u1,u2,u3,u4表示1个,2个,3个,4个字节。
Class文件结构中只有两种数据类型:无符号数和表,表就相当于是结构体了。

//TestClass.java

package com.ejushang.TestClass;
 public class TestClass implements Super{
     private static final int staticVar = 0;
     private int instanceVar=0;
     public int instanceMethod(int param){
         return param+1;
     }
 }
 interface Super{ }

TestClass.class
00000000h: CA FE BA BE 00 00 00 34 00 18 0A 00 04 00 13 09 ; 漱壕...4........
00000010h: 00 03 00 14 07 00 15 07 00 16 07 00 17 01 00 09 ; ................
00000020h: 73 74 61 74 69 63 56 61 72 01 00 01 49 01 00 0D ; staticVar...I...
00000030h: 43 6F 6E 73 74 61 6E 74 56 61 6C 75 65 03 00 00 ; ConstantValue...
00000040h: 00 00 01 00 0B 69 6E 73 74 61 6E 63 65 56 61 72 ; .....instanceVar
00000050h: 01 00 06 3C 69 6E 69 74 3E 01 00 03 28 29 56 01 ; ......()V.
00000060h: 00 04 43 6F 64 65 01 00 0F 4C 69 6E 65 4E 75 6D ; ..Code...LineNum
00000070h: 62 65 72 54 61 62 6C 65 01 00 0E 69 6E 73 74 61 ; berTable...insta
00000080h: 6E 63 65 4D 65 74 68 6F 64 01 00 04 28 49 29 49 ; nceMethod...(I)I
00000090h: 01 00 0A 53 6F 75 72 63 65 46 69 6C 65 01 00 0E ; ...SourceFile...
000000a0h: 54 65 73 74 43 6C 61 73 73 2E 6A 61 76 61 0C 00 ; TestClass.java..
000000b0h: 0B 00 0C 0C 00 0A 00 07 01 00 20 63 6F 6D 2F 65 ; .......... com/e
000000c0h: 6A 75 73 68 61 6E 67 2F 54 65 73 74 43 6C 61 73 ; jushang/TestClas
000000d0h: 73 2F 54 65 73 74 43 6C 61 73 73 01 00 10 6A 61 ; s/TestClass...ja
000000e0h: 76 61 2F 6C 61 6E 67 2F 4F 62 6A 65 63 74 01 00 ; va/lang/Object..
000000f0h: 1C 63 6F 6D 2F 65 6A 75 73 68 61 6E 67 2F 54 65 ; .com/ejushang/Te
00000100h: 73 74 43 6C 61 73 73 2F 53 75 70 65 72 00 21 00 ; stClass/Super.!.
00000110h: 03 00 04 00 01 00 05 00 02 00 1A 00 06 00 07 00 ; ................
00000120h: 01 00 08 00 00 00 02 00 09 00 02 00 0A 00 07 00 ; ................
00000130h: 00 00 02 00 01 00 0B 00 0C 00 01 00 0D 00 00 00 ; ................
00000140h: 26 00 02 00 01 00 00 00 0A 2A B7 00 01 2A 03 B5 ; &........?..?
00000150h: 00 02 B1 00 00 00 01 00 0E 00 00 00 0A 00 02 00 ; ..?............
00000160h: 00 00 03 00 04 00 07 00 01 00 0F 00 10 00 01 00 ; ................
00000170h: 0D 00 00 00 1C 00 02 00 02 00 00 00 04 1B 04 60 ; ...............`
00000180h: AC 00 00 00 01 00 0E 00 00 00 06 00 01 00 00 00 ; ?..............
00000190h: 0A 00 01 00 11 00 00 00 02 00 12 ; ...........

分析class文件:

magic: CA FE BA BE
version: 00 00 00 34
cp_count: 00 18
cp[1]: 
    flag: 0A (CONSTANT_Methodref_info 类中方法的符号引用)
    class_index: 00 04
    name_and_type_index: 00 13
cp[2]:
    flag: 09 (CONSTANT_Fieldref_info 字段的符号引用)
    class_index: 00 03
    name_and_type_index: 00 14
cp[3]:
    flag: 07(CONSTANT_Class_info 类或接口的符号引用)
    class_index: 00 15
cp[4]:  
    flag:07
    class_index: 00 16
cp[5]:
    flag: 07
    class_index: 00 17
cp[6]:
    flag: 01(CONSTANT_UTF8_info UTF-8编码的字符串)
    len: 00 09 
    value: 73 74 61 74 69 63 56 61 72(staticVar)
cp[7]:
    flag: 01
    len: 00 01
    value: 49(I)
cp[8]:
    flag: 01
    len: 00 0D 
    value: 43 6F 6E 73 74 61 6E 74 56 61 6C 75 65(ConstantValue)
cp[9]:
    flag: 03(CONSTANT_Integer_info整形字面量)
    value: 00 00 00 00
cp[0x0A]:
    flag: 01
    len: 00 0B
    value: 69 6E 73 74 61 6E 63 65 56 61 72(instanceVar) 
cp[0x0B]:
    flag: 01
    len: 00 06
    value: 3C 69 6E 69 74 3E()
cp[0x0C]:
    flag: 01
    len: 00 03
    value: 28 29 56(()I)
cp[0x0D]:
    flag: 01 
    len: 00 04
    value: 43 6F 64 65 (Code)
cp[0x0E]:
    flag: 01
    len: 00 0F
    value: 4C 69 6E 65 4E 75 6D 62 65 72 54 61 62 6C 65 (LineNumberTable)
cp[0x0F]:
    01 00 0E 69 6E 73 74 61 6E 63 65 4D 65 74 68 6F 64 (instanceMethod)
cp[0x10]:
    01 00 04 28 49 29 49 ((I)I) 
cp[0x11]:
    01 00 0A 53 6F 75 72 63 65 46 69 6C 65 (SourceFile)
cp[0x12]:
    01 00 0E 54 65 73 74 43 6C 61 73 73 2E 6A 61 76 61 (TestClass.java)
cp[0x13]:
    flag: 0C (CONSTANT_NameAndType_info 字段和方法的名称以及类型的符号引用)
    name_index: 00 0B
    type_index: 00 0C
cp[0x14]:
    0C 00 0A 00 07
cp[0x15]:
    01 00 20 63 6F 6D 2F 65 6A 75 73 68 61 6E 67 2F
    54 65 73 74 43 6C 61 73 73 2F 54 65 73 74 43 6C
    61 73 73 (com/ejushang/TestClass/TestClass)
cp[0x16]:
    01 00 10 6A 61 76 61 2F 6C 61 6E 67 2F 4F 62 6A
    65 63 74 (java/lang/Object)
cp[0x17]:
    01 00 1C 
    63 6F 6D 2F 65 6A 75 73 68 61 6E 67 2F 54 65 73
    74 43 6C 61 73 73 2F 53 75 70 65 72 (com/ejushang/TestClass/Super)
access_flags (访问标志):
    00 21 (ACC_PUBLIC ACC_SUPER)
this_class (CONSTANT_class_info 常量池索引):
    00 03
super_class:
    00 04
interface_count (实现的接口或者父类实现的接口的数量):
    00 01
interfaces[interface_count]:
    00 05
field_count:    
    00 02
fields[1]:
    access_flags: 00 1A
    name_index: 00 06
    destriptor_index: 00 07
    attribute_count: 00 01
    attributes[attribute_count]: 
        attribute_name_index: 00 08
        attribute_len: 00 00 00 02
        constantvalue_index: 00 09
fields[2]:
    access_flags: 00 02
    name_index: 00 0A
    destriptor_index: 00 07
    attribute_count: 00 00 
method_count: 00 02
methods[1]:
    access_flags: 00 01
    name_index: 00 0B
    destriptor_index: 00 0C
    attribute_count: 00 01
    attribute[1]:
        attribute_name_index: 00 0D
        attibute_len: 00 00 00 26
        max_stack: 00 02
        max_locals: 00 01
        code_len: 00 00 00 0A
        code: 2A B7 00 01 2A 03 B5 00 02 B1
        exception_table_len: 00 00
        attribute_count: 00 01
        attribute[1]:
            attribute_name_index: 00 0E
            attribute_len: 00 00 00 0A
            line_number_table_len: 00 02
            line[1]:
                start_pc: 00 00
                line_number: 00 03
            line[2]:
                start_pc: 00 04
                line_number: 00 07
methods[2]:
    access_flags: 00 01
    name_index: 00 0F
    destriptor_index: 00 10
    attribute_count: 00 01
    attibute[1]: 
        attribute_name_index: 00 0D
        attribute_len: 00 00 00 1C
        max_stack: 00 02
        max_locals: 00 02
        code_len: 00 00 00 04
        code: 1B 04 60 AC
        exception_table_len: 00 00
        attribute_count: 00 01
        attibute[1]: 
            attribute_name_index: 00 0E
            attibute_len: 00 00 00 06
            line_number_table_len: 00 01
            line[1]:
                start_pc: 00 00
                line_number: 00 0A
attribute_count: 00 01
attribute[1]:
    attribute_name_index: 00 11
    attribute_len: 00 00 00 02
    sourcefile_index: 00 12                  

常量池是什么东西?常量池存储字面量和符号引用。字面量包括字符串,final常量的值,以及某个属性的初始值等。符号引用主要
存储类和接口的全限定名称,字段的名称以及描述符,方法的名称以及描述符。
常量池中存储数据类型,字面量和符号引用,字面量就是属性/方法/类名或者描述符这种字符串,符号引用就是第几个常量。
常量池的项目包括:


image.png

Class文件结构中有字段表,方法表和属性表,就是变量,方法,属性的结构体
属性表集合:


image.png

Java方法中代码和字节码指令怎么转换?有的指令带参数有的不带,带多少参数都是固定的
假如有方法:

public void f(){
    a+b;
}

假如这个方法对应的字节码指令是...xxyyzz...,xx是加的指令,xx指令带有两个参数,跟着它的yy和zz指令就是它的参数,而yy和zz又有可能指向常量池中的内容。

你可能感兴趣的:('深入理解Java虚拟机'2018-04-16)