Jvm学习笔记(二)

2016-11-12  本文已影响0人  windfall_

Class类文件结构


Class类文件结构 常量池标志位

CONSTANT_Class_info结构里包含一个name_index,指向一个 CONSTANT_Utf8_info,为这个类的全限定名。CONSTANT_Utf8_info结构包含length,和bytes。

CONSTANT_Class_info {
    u1 tag;   //tag取值为7,代表CONSTANT_Class_info
    u2 name_index;  //name_index表示代表自己类名的字符串信息位于于常量池数组中哪一个,也就是索引
}
CONSTANT_Utf8_info {
    u1 tag;
    u2 length;  //下面就是存储UTF8字符串的地方了
    u1 bytes[length];
}
CONSTANT_NameAndType_info {
   u1 tag;
   u2 name_index;  //方法名或域名对应的字符串索引
   u2 descriptor_index; //方法信息(参数+返回值),或者成员变量的信息(类型)对应的字符串索引
}
CONSTANT_Fieldref_info {
   u1 tag;
   u2 class_index;  
   u2 name_and_type_index; 
}
CONSTANT_Methodref_info {
   u1 tag;
   u2 class_index;  
   u2 name_and_type_index;
}
CONSTANT_InterfaceMethodref_info {
   u1 tag;
   u2 class_index;  
   u2 name_and_type_index; 
}

示例:

public class TestMain{
    public int x = 0;

    public static void main (String [] args){
        TestMain testMain = new TestMain();
        testMain.test();
    }

    public TestMain(){

    }
    public void test(){
        
    }
}

javap -verbose

public class TestMain                                                                 
  minor version: 0                                                                    
  major version: 52                                                                   
  flags: ACC_PUBLIC, ACC_SUPER                                                        
Constant pool:                                                                        
   #1 = Class              #18            // TestMain                                 
   #2 = Methodref          #1.#19         // TestMain."<init>":()V                    
   #3 = Methodref          #1.#20         // TestMain.test:()V                        
   #4 = Methodref          #6.#19         // java/lang/Object."<init>":()V            
   #5 = Fieldref           #1.#21         // TestMain.x:I                             
   #6 = Class              #22            // java/lang/Object                         
   #7 = Utf8               x                                                          
   #8 = Utf8               I                                                          
   #9 = Utf8               main                                                       
  #10 = Utf8               ([Ljava/lang/String;)V                                     
  #11 = Utf8               Code                                                       
  #12 = Utf8               LineNumberTable                                            
  #13 = Utf8               <init>                                                     
  #14 = Utf8               ()V                                                        
  #15 = Utf8               test                                                       
  #16 = Utf8               SourceFile                                                 
  #17 = Utf8               TestMain.java                                              
  #18 = Utf8               TestMain                                                   
  #19 = NameAndType        #13:#14        // "<init>":()V                             
  #20 = NameAndType        #15:#14        // test:()V                                 
  #21 = NameAndType        #7:#8          // x:I                                      
  #22 = Utf8               java/lang/Object                                           
{                                                                                     
  public int x;                                                                       
    descriptor: I                                                                     
    flags: ACC_PUBLIC                                                                 
                                                                                      
  public static void main(java.lang.String[]);                                        
    descriptor: ([Ljava/lang/String;)V                                                
    flags: ACC_PUBLIC, ACC_STATIC                                                     
    Code:                                                                             
      stack=2, locals=2, args_size=1                                                  
         0: new           #1                  // class TestMain                       
         3: dup                                                                       
         4: invokespecial #2                  // Method "<init>":()V                  
         7: astore_1                                                                  
         8: aload_1                                                                   
         9: invokevirtual #3                  // Method test:()V                      
        12: return                                                                    
      LineNumberTable:                                                                
        line 5: 0                                                                     
        line 6: 8                                                                     
        line 7: 12                                                                    
                                                                                      
  public TestMain();                                                                  
    descriptor: ()V                                                                   
    flags: ACC_PUBLIC                                                                 
    Code:                                                                             
      stack=2, locals=1, args_size=1                                                  
         0: aload_0                                                                   
         1: invokespecial #4                  // Method java/lang/Object."<init>":()V 
         4: aload_0                                                                   
         5: iconst_0                                                                  
         6: putfield      #5                  // Field x:I                            
         9: return                                                                    
      LineNumberTable:                                                                
        line 9: 0                                                                     
        line 2: 4                                                                     
        line 11: 9                                                                    
                                                                                      
  public void test();                                                                 
    descriptor: ()V                                                                   
    flags: ACC_PUBLIC                                                                 
    Code:                                                                             
      stack=0, locals=1, args_size=1                                                  
         0: return                                                                    
      LineNumberTable:                                                                
        line 14: 0                                                                    
}                                                                                     
SourceFile: "TestMain.java"                                                           
                                                                                      
上一篇 下一篇

猜你喜欢

热点阅读