Java類編譯後Class檔案概述(下)

來源:互聯網
上載者:User

標籤:

具體的執行個體

Java檔案

package com.weixuan.test;public class MyTest {private int age;private String name;public int getAge() {return age;}public String getName() {return name;}public void setAge(int age) {this.age = age;}public void setName(String name) {this.name = name;}public static void main(String[] args) {}}

使用javac -g:vars MyTest.java編譯,顯示localvariable

C:\Users\weixu_000\Desktop>javac -g:vars MyTest.javaC:\Users\weixu_000\Desktop>javap -verbose MyTest.classClassfile /C:/Users/weixu_000/Desktop/MyTest.class  Last modified 2015-7-5; size 708 bytes  MD5 checksum 2d2527c48aba8637fbf99840391879aapublic class com.weixuan.test.MyTest  minor version: 0  major version: 52  flags: ACC_PUBLIC, ACC_SUPERConstant pool:   #1 = Methodref          #5.#28         // java/lang/Object."<init>":()V   #2 = Fieldref           #4.#29         // com/weixuan/test/MyTest.age:I   #3 = Fieldref           #4.#30         // com/weixuan/test/MyTest.name:Ljava/lang/String;   #4 = Class              #31            // com/weixuan/test/MyTest   #5 = Class              #32            // java/lang/Object   #6 = Utf8               age   #7 = Utf8               I   #8 = Utf8               name   #9 = Utf8               Ljava/lang/String;  #10 = Utf8               <init>  #11 = Utf8               ()V  #12 = Utf8               Code  #13 = Utf8               LocalVariableTable  #14 = Utf8               this  #15 = Utf8               Lcom/weixuan/test/MyTest;  #16 = Utf8               getAge  #17 = Utf8               ()I  #18 = Utf8               getName  #19 = Utf8               ()Ljava/lang/String;  #20 = Utf8               setAge  #21 = Utf8               (I)V  #22 = Utf8               setName  #23 = Utf8               (Ljava/lang/String;)V  #24 = Utf8               main  #25 = Utf8               ([Ljava/lang/String;)V  #26 = Utf8               args  #27 = Utf8               [Ljava/lang/String;  #28 = NameAndType        #10:#11        // "<init>":()V  #29 = NameAndType        #6:#7          // age:I  #30 = NameAndType        #8:#9          // name:Ljava/lang/String;  #31 = Utf8               com/weixuan/test/MyTest  #32 = Utf8               java/lang/Object{  public com.weixuan.test.MyTest();    descriptor: ()V    flags: ACC_PUBLIC    Code:      stack=1, locals=1, args_size=1         0: aload_0         1: invokespecial #1                  // Method java/lang/Object."<init>":()V         4: return      LocalVariableTable:        Start  Length  Slot  Name   Signature            0       5     0  this   Lcom/weixuan/test/MyTest;  public int getAge();    descriptor: ()I    flags: ACC_PUBLIC    Code:      stack=1, locals=1, args_size=1         0: aload_0         1: getfield      #2                  // Field age:I         4: ireturn      LocalVariableTable:        Start  Length  Slot  Name   Signature            0       5     0  this   Lcom/weixuan/test/MyTest;  public java.lang.String getName();    descriptor: ()Ljava/lang/String;    flags: ACC_PUBLIC    Code:      stack=1, locals=1, args_size=1         0: aload_0         1: getfield      #3                  // Field name:Ljava/lang/String;         4: areturn      LocalVariableTable:        Start  Length  Slot  Name   Signature            0       5     0  this   Lcom/weixuan/test/MyTest;  public void setAge(int);    descriptor: (I)V    flags: ACC_PUBLIC    Code:      stack=2, locals=2, args_size=2         0: aload_0         1: iload_1         2: putfield      #2                  // Field age:I         5: return      LocalVariableTable:        Start  Length  Slot  Name   Signature            0       6     0  this   Lcom/weixuan/test/MyTest;            0       6     1   age   I  public void setName(java.lang.String);    descriptor: (Ljava/lang/String;)V    flags: ACC_PUBLIC    Code:      stack=2, locals=2, args_size=2         0: aload_0         1: aload_1         2: putfield      #3                  // Field name:Ljava/lang/String;         5: return      LocalVariableTable:        Start  Length  Slot  Name   Signature            0       6     0  this   Lcom/weixuan/test/MyTest;            0       6     1  name   Ljava/lang/String;  public static void main(java.lang.String[]);    descriptor: ([Ljava/lang/String;)V    flags: ACC_PUBLIC, ACC_STATIC    Code:      stack=0, locals=1, args_size=1         0: return      LocalVariableTable:        Start  Length  Slot  Name   Signature            0       1     0  args   [Ljava/lang/String;}

對應的位元組碼

cafe babe 0000 0034 001f 0a00 0500 1a090004 001b 0900 0400 1c07 001d 0700 1e010003 6167 6501 0001 4901 0004 6e61 6d650100 124c 6a61 7661 2f6c 616e 672f 53747269 6e67 3b01 0006 3c69 6e69 743e 01000328 2956 0100 0443 6f64 6501 000f 4c696e65 4e75 6d62 6572 5461 626c 6501 00066765 7441 6765 0100 0328 2949 0100 07676574 4e61 6d65 0100 1428 294c 6a61 76612f6c 616e 672f 5374 7269 6e67 3b01 00067365 7441 6765 0100 0428 4929 5601 00077365 744e 616d 6501 0015 284c 6a61 76612f6c 616e 672f 5374 7269 6e67 3b29 56010004 6d61 696e 0100 1628 5b4c 6a61 76612f6c 616e 672f 5374 7269 6e67 3b29 5601000a 536f 7572 6365 4669 6c65 0100 0b4d7954 6573 742e 6a61 7661 0c00 0a00 0b0c0006 0007 0c00 0800 0901 0017 636f 6d2f7765 6978 7561 6e2f 7465 7374 2f4d 79546573 7401 0010 6a61 7661 2f6c 616e 672f4f62 6a65 6374 0021 0004 0005 0000 00020002 0006 0007 0000 0002 0008 0009 00000006 0001 000a 000b 0001 000c 0000 001d0001 0001 0000 0005 2ab7 0001 b100 00000100 0d00 0000 0600 0100 0000 0300 01000e00 0f00 0100 0c00 0000 1d00 0100 01000000 052a b400 02ac 0000 0001 000d 00000006 0001 0000 0009 0001 0010 0011 0001000c 0000 001d 0001 0001 0000 0005 2ab40003 b000 0000 0100 0d00 0000 0600 01000000 0d00 0100 1200 1300 0100 0c00 00002200 0200 0200 0000 062a 1bb5 0002 b1000000 0100 0d00 0000 0a00 0200 0000 11000500 1200 0100 1400 1500 0100 0c00 00002200 0200 0200 0000 062a 2bb5 0003 b1000000 0100 0d00 0000 0a00 0200 0000 15000500 1600 0900 1600 1700 0100 0c00 00001900 0000 0100 0000 01b1 0000 0001 000d0000 0006 0001 0000 001a 0001 0018 00000002 0019 

具體的解釋

1、magic number : 前四個位元組 cafe babe,咖啡寶貝

2、次主要版本:接下來的四個位元組 0000 0034

3、常量池

前兩個位元組是常量池容量計數器,後面才是常量池

001f : 常量池容量計數器 十進位的31 也就是說,有30個常量,索引是1到30

4、訪問標誌

常量池結束之後,就是訪問標誌。

0021 : 只有ACC_PUBLIC和ACC_SUPER為真。即這個類是public 的普通類,無其他修飾符

5、類索引,父類索引與介面索引集合

訪問標誌之後,就是類索引,父類索引和介面索引集合。

0004 0005 00000004:類索引是4,查表, #4 = Class #29 // com/weixuan/test/MyTestindex是29,#29 = Utf8 com/weixuan/test/MyTest 全類名0005:父類索引是5,查表, #5 = Class #30 // java/lang/Objectindex是30,#30 = Utf8 java/lang/Object 父類是Object0000:介面資訊為0,無介面資訊。

6、欄位表集合

0002 0002 0006 0007 0000 0002 0008 0009 0000

介面索引集合之後就是欄位表的兩個位元組的欄位容量計數器

0002: 2個屬性。 在原始碼中是,age和name,我們驗證一下:

0002 0002 0006 00070002:訪問標誌,查欄位訪問標誌表,0002--->0x0002--->ACC_PRIVATE0006:name_index,值是6,查表,第一個屬性是age#6 = Utf8 age0007:第一個屬性的描述符索引,值是7,查表,屬性類型是I--->int#7 = Utf8 I0000:屬性資訊,無0002:第二個屬性的訪問標誌,ACC_PRIVATE0008:name_index,值是8,查表,第二個屬性是name#8 = Utf8 name0009:描述符索引 值為9,查表,L物件類型,即string 物件類型#9 = Utf8 Ljava/lang/String;0000:屬性資訊,無

7、方法表集合

欄位表結束就是方法表集合開頭的方法容量計數器,然後是方法表的內容

0006 0001 000a 000b 0001 000c

0006:methods_count 6個方法

原始碼中分別是getName(),getAge(),setName(),setAge(),main(),以及編譯器添加的<init>方法

0001:訪問標誌 0001 ---> ACC_PUBLIC,public 000a:name_index,值為10,查表#10 = Utf8 <init>000b:描述符索引,11,查表,方法是 public void()類型,無參#11 = Utf8 ()V0001:屬性數量 1個000c:屬性索引 值是12 查表,對應的常量是code#12 = Utf8 Code

8、 編譯時間,javac -g:vars MyTest.java,可以顯示localvariable

分析一下<init>方法的code屬性

public com.weixuan.test.MyTest();descriptor: ()Vflags: ACC_PUBLICCode:stack=1, locals=1, args_size=10: aload_01: invokespecial #1 // Method java/lang/Object."<init>":()V4: returnLocalVariableTable:Start Length Slot Name Signature0 5 0 this Lcom/weixuan/test/MyTest;

可以看到,statck:1,local:1,codelength:5

找到相應的位元組碼

0001 0001 0000 0005 2ab7 0001 b1

0001 0001 0000 0005 對應的是stack,local,codelength

2ab7 0001 b1是code的內容

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

Java類編譯後Class檔案概述(下)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.