總網頁瀏覽量

2016年2月18日 星期四

[Java] javap 觀察看看

A.1 Examining Java Classfiles

已知Java會先編譯成class檔,

也就是Source Code編譯成Byte Code。

但是要看到Byte Code內的情況一般都會略過不談,

所以現在要用到只好很雞八的來看一下了。

而一般Java的JDC工具包內其實就已經有個工具可以用了,也就是 Javap。

javap -help  

Usage: javap <options> <classes>
where possible options include:

  -help                       顯示常用指令資訊
  -version                   顯示目前所用JDK版本編號
  -verbose / -v [檔名]  顯示[檔名]的所以class檔資訊
  -l                             顯示 line number 和 local variable tables 資訊


  -public                     顯示 public classes 和 members (其他皆不列出)
  -protected                顯示 protected/public classes 和 members
  -package                 顯示 package/protected/public classes 和 members (default)
  -p  -private                顯示  Show all classes and members


  -c                                   打開 class看ByteCode( Disassemble the code)
  -s                                    Print internal type signatures
  -sysinfo                           Show system info of class being processed
  -constants                       Show final constants
  -classpath <path>           Specify where to find user class files
  -cp <path>                       Specify where to find user class files
  -bootclasspath <path>    Override location of bootstrap class files

   -------------------------------------------------------------------------------------------------------------

public class A {   
    public static void main(String[] args){
        int a = 9;
        String c = "123";
    }   
   
    public void move(int a){}
   
    public boolean move2(){    return true;}
}

   -------------------------------------------------------------------------------------------------------------

-v A

Classfile /C:/Users/haha/Desktop/New folder/A.class
  Last modified 2016/2/19; size 377 bytes
  MD5 checksum 47008851a8f4aad1f298978ae5976262
  Compiled from "A.java"
public class A
  minor version: 0
  major version: 52
  flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
   #1 = Methodref          #4.#17         // java/lang/Object."<init>":()V
   #2 = String             #18            // 123
   #3 = Class              #19            // A
   #4 = Class              #20            // java/lang/Object
   #5 = Utf8               <init>
   #6 = Utf8               ()V
   #7 = Utf8               Code
   #8 = Utf8               LineNumberTable
   #9 = Utf8               main
  #10 = Utf8               ([Ljava/lang/String;)V
  #11 = Utf8               move
  #12 = Utf8               (I)V
  #13 = Utf8               move2
  #14 = Utf8               ()Z
  #15 = Utf8               SourceFile
  #16 = Utf8               A.java
  #17 = NameAndType        #5:#6          // "<init>":()V
  #18 = Utf8               123
  #19 = Utf8               A
  #20 = Utf8               java/lang/Object
{
  public A();
    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
      LineNumberTable:
        line 1: 0

  public static void main(java.lang.String[]);
    descriptor: ([Ljava/lang/String;)V
    flags: ACC_PUBLIC, ACC_STATIC
    Code:
      stack=1, locals=3, args_size=1
         0: bipush        9
         2: istore_1
         3: ldc           #2                  // String 123
         5: astore_2
         6: return
      LineNumberTable:
        line 3: 0
        line 4: 3
        line 5: 6

  public void move(int);
    descriptor: (I)V
    flags: ACC_PUBLIC
    Code:
      stack=0, locals=2, args_size=2
         0: return
      LineNumberTable:
        line 7: 0

  public boolean move2();
    descriptor: ()Z
    flags: ACC_PUBLIC
    Code:
      stack=1, locals=1, args_size=1
         0: iconst_1
         1: ireturn
      LineNumberTable:
        line 9: 0
}
SourceFile: "A.java"

   -------------------------------------------------------------------------------------------------------------

-l A

Compiled from "A.java"
public class A {
  public A();
    LineNumberTable:
      line 1: 0

  public static void main(java.lang.String[]);
    LineNumberTable:
      line 3: 0
      line 4: 3
      line 5: 6

  public void move(int);
    LineNumberTable:
      line 7: 0

  public boolean move2();
    LineNumberTable:
      line 9: 0
}


   -------------------------------------------------------------------------------------------------------------

-public A

Compiled from "A.java"
public class A {
  public A();
  public static void main(java.lang.String[]);
  public void move(int);
  public boolean move2();
}

   -------------------------------------------------------------------------------------------------------------

把 public void move(int); 改成 private void move(int);

-p A

Compiled from "A.java"
public class A {
  public A();
  public static void main(java.lang.String[]);
  private void move(int);
  public boolean move2();
}

   -------------------------------------------------------------------------------------------------------------

-c A

Compiled from "A.java"
public class A {
  public A();
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."<init>":()V
       4: return

  public static void main(java.lang.String[]);
    Code:
       0: bipush        9
       2: istore_1
       3: ldc           #2                  // String 123
       5: astore_2
       6: return

  public boolean move2();
    Code:
       0: iconst_1
       1: ireturn
}

   -------------------------------------------------------------------------------------------------------------

-s A

Compiled from "A.java"
public class A {
  public A();
    descriptor: ()V

  public static void main(java.lang.String[]);
    descriptor: ([Ljava/lang/String;)V

  public boolean move2();
    descriptor: ()Z
}

   -------------------------------------------------------------------------------------------------------------

-sysinfo A

Classfile /C:/Users/haha/Desktop/New folder/A.class
  Last modified 2016/2/19; size 377 bytes
  MD5 checksum eaf868990ea17c4dd4ee7691a79c7cba
  Compiled from "A.java"
public class A {
  public A();
  public static void main(java.lang.String[]);
  public boolean move2();
}

 

 








沒有留言:

張貼留言