Javap的使用以及java字节码分析实例

标签:

本文出自jvm123.com-java技术分享站:http://jvm123.com/2019/10/javap-de-shi-yong.html

javap是JDK中的一个工具,有以下使用方法。

在cmd输入javap可以查看用法说明,如下:

D:\ideaProject\demo-java\target\classes>javap
 用法: javap  
 其中, 可能的选项包括:
   -help  --help  -?        输出此用法消息
   -version                 版本信息
   -v  -verbose             输出附加信息
   -l                       输出行号和本地变量表
   -public                  仅显示公共类和成员
   -protected               显示受保护的/公共类和成员
   -package                 显示程序包/受保护的/公共类
                            和成员 (默认)
   -p  -private             显示所有类和成员
   -c                       对代码进行反汇编
   -s                       输出内部类型签名
   -sysinfo                 显示正在处理的类的
                            系统信息 (路径, 大小, 日期, MD5 散列)
   -constants               显示最终常量
   -classpath         指定查找用户类文件的位置
   -cp                指定查找用户类文件的位置
   -bootclasspath     覆盖引导类文件的位置

javap比较常用的选项是-c,对.class文件进行反汇编。如下:

D:\ideaProject\demo-java\target\classes>javap -c TestJava
 Compiled from "TestJava.java"
 public class TestJava {
   public TestJava();
     Code:
        0: aload_0
        1: invokespecial #1                  // Method java/lang/Object."":()V
        4: return
 public static void main(java.lang.String[]);
     Code:
        0: iconst_3
        1: istore_1
        2: ldc           #2                  // String hello world!
        4: astore_2
        5: getstatic     #3                  // Field java/lang/System.out:Ljava/io/PrintStream;
        8: iload_1
        9: invokevirtual #4                  // Method java/io/PrintStream.println:(I)V
       12: getstatic     #3                  // Field java/lang/System.out:Ljava/io/PrintStream;
       15: aload_2
       16: invokevirtual #5                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V
       19: return
 }

javap也可以使用-v,输出附加信息,如下:

D:\ideaProject\demo-java\target\classes>javap -v TestJava.class
 Classfile /D:/ideaProject/demo-java/target/classes/TestJava.class
   Last modified 2019-10-18; size 653 bytes
   MD5 checksum 2f8f8462713a922998e2e2a697f94647
   Compiled from "TestJava.java"
 public class TestJava
   minor version: 0
   major version: 52
   flags: ACC_PUBLIC, ACC_SUPER
 Constant pool:
    #1 = Methodref          #7.#26         // java/lang/Object."":()V
    #2 = String             #27            // hello world!
    #3 = Fieldref           #28.#29        // java/lang/System.out:Ljava/io/PrintStream;
    #4 = Methodref          #30.#31        // java/io/PrintStream.println:(I)V
    #5 = Methodref          #30.#32        // java/io/PrintStream.println:(Ljava/lang/String;)V
    #6 = Class              #33            // TestJava
    #7 = Class              #34            // java/lang/Object
    #8 = Utf8               
    #9 = Utf8               ()V
   #10 = Utf8               Code
   #11 = Utf8               LineNumberTable
   #12 = Utf8               LocalVariableTable
   #13 = Utf8               this
   #14 = Utf8               LTestJava;
   #15 = Utf8               main
   #16 = Utf8               ([Ljava/lang/String;)V
   #17 = Utf8               args
   #18 = Utf8               [Ljava/lang/String;
   #19 = Utf8               a
   #20 = Utf8               I
   #21 = Utf8               str
   #22 = Utf8               Ljava/lang/String;
   #23 = Utf8               MethodParameters
   #24 = Utf8               SourceFile
   #25 = Utf8               TestJava.java
   #26 = NameAndType        #8:#9          // "":()V
   #27 = Utf8               hello world!
   #28 = Class              #35            // java/lang/System
   #29 = NameAndType        #36:#37        // out:Ljava/io/PrintStream;
   #30 = Class              #38            // java/io/PrintStream
   #31 = NameAndType        #39:#40        // println:(I)V
   #32 = NameAndType        #39:#41        // println:(Ljava/lang/String;)V
   #33 = Utf8               TestJava
   #34 = Utf8               java/lang/Object
   #35 = Utf8               java/lang/System
   #36 = Utf8               out
   #37 = Utf8               Ljava/io/PrintStream;
   #38 = Utf8               java/io/PrintStream
   #39 = Utf8               println
   #40 = Utf8               (I)V
   #41 = Utf8               (Ljava/lang/String;)V
 {
   public TestJava();
     descriptor: ()V
     flags: ACC_PUBLIC
     Code:
       stack=1, locals=1, args_size=1
          0: aload_0
          1: invokespecial #1                  // Method java/lang/Object."":()V
          4: return
       LineNumberTable:
         line 5: 0
       LocalVariableTable:
         Start  Length  Slot  Name   Signature
             0       5     0  this   LTestJava;
 public static void main(java.lang.String[]);
     descriptor: ([Ljava/lang/String;)V
     flags: ACC_PUBLIC, ACC_STATIC
     Code:
       stack=2, locals=3, args_size=1
          0: iconst_3
          1: istore_1
          2: ldc           #2                  // String hello world!
          4: astore_2
          5: getstatic     #3                  // Field java/lang/System.out:Ljava/io/PrintStream;
          8: iload_1
          9: invokevirtual #4                  // Method java/io/PrintStream.println:(I)V
         12: getstatic     #3                  // Field java/lang/System.out:Ljava/io/PrintStream;
         15: aload_2
         16: invokevirtual #5                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V
         19: return
       LineNumberTable:
         line 8: 0
         line 9: 2
         line 10: 5
         line 11: 12
         line 12: 19
       LocalVariableTable:
         Start  Length  Slot  Name   Signature
             0      20     0  args   [Ljava/lang/String;
             2      18     1     a   I
             5      15     2   str   Ljava/lang/String;
     MethodParameters:
       Name                           Flags
       args
 }
 SourceFile: "TestJava.java"

发表评论