javap命令解析

一、javap是什么?
javap是将字节码进行反编译(与javac对应),可以查看java编译器为我们生成的字节码。通过它,我们可以对照源代码和字节码,从而了解很多编译器内部的工作。
二、javap的使用

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

示例代码如下:

1  package com.meituan.mtlearner.cheetah.service.test;               
2                                                                                                    
3  public class CommonTest {
4     private String name;
5
6     public static void main(String[] args) throws Exception{
7         String a = "222";
8         System.out.println(a);
9         throw new Exception();
10     }
11  }
➜  test javap -v CommonTest   //和javap -verbose一样,输出额外信息
警告: 二进制文件CommonTest包含com.meituan.mtlearner.cheetah.service.test.CommonTest
Classfile /Users/shichangmin/workspace/cheetah/target/classes/com/meituan/mtlearner/cheetah/service/test/CommonTest.class
  Last modified May 21, 2018; size 727 bytes
  MD5 checksum 7110663fa12a58f7d74917d709a67c66
  Compiled from "CommonTest.java"
public class com.meituan.mtlearner.cheetah.service.test.CommonTest
  minor version: 0
  major version: 52
  flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
   #1 = Methodref          #8.#26         // java/lang/Object."":()V
   #2 = String             #27            // 222
   #3 = Fieldref           #28.#29        // java/lang/System.out:Ljava/io/PrintStream;
   #4 = Methodref          #30.#31        // java/io/PrintStream.println:(Ljava/lang/String;)V
   #5 = Class              #32            // java/lang/Exception
   #6 = Methodref          #5.#26         // java/lang/Exception."":()V
   #7 = Class              #33            // com/meituan/mtlearner/cheetah/service/test/CommonTest
   #8 = Class              #34            // java/lang/Object
   #9 = Utf8               name
  #10 = Utf8               Ljava/lang/String;
  #11 = Utf8               
  #12 = Utf8               ()V
  #13 = Utf8               Code
  #14 = Utf8               LineNumberTable
  #15 = Utf8               LocalVariableTable
  #16 = Utf8               this
  #17 = Utf8               Lcom/meituan/mtlearner/cheetah/service/test/CommonTest;
  #18 = Utf8               main
  #19 = Utf8               ([Ljava/lang/String;)V
  #20 = Utf8               args
  #21 = Utf8               [Ljava/lang/String;
  #22 = Utf8               a
  #23 = Utf8               Exceptions
  #24 = Utf8               SourceFile
  #25 = Utf8               CommonTest.java
  #26 = NameAndType        #11:#12        // "":()V
  #27 = Utf8               222
  #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:(Ljava/lang/String;)V
  #32 = Utf8               java/lang/Exception
  #33 = Utf8               com/meituan/mtlearner/cheetah/service/test/CommonTest
  #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               (Ljava/lang/String;)V
{
  public com.meituan.mtlearner.cheetah.service.test.CommonTest();
    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 3: 0
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0       5     0  this   Lcom/meituan/mtlearner/cheetah/service/test/CommonTest;

  public static void main(java.lang.String[]) throws java.lang.Exception;
    descriptor: ([Ljava/lang/String;)V
    flags: ACC_PUBLIC, ACC_STATIC
    Code:
      stack=2, locals=2, args_size=1
         0: ldc           #2                  // String 222
         2: astore_1
         3: getstatic     #3                  // Field java/lang/System.out:Ljava/io/PrintStream;
         6: aload_1
         7: invokevirtual #4                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V
        10: new           #5                  // class java/lang/Exception
        13: dup
        14: invokespecial #6                  // Method java/lang/Exception."":()V
        17: athrow
      LineNumberTable:     //代码中的行数对应code中的序号,例如示例代码的第7行(String a = "222";)对应Code块中的0号(idc #2 //String 222)
        line 7: 0
        line 8: 3
        line 9: 10
      LocalVariableTable:    //局部变量表,列出了所有栈帧中的局部变量
        Start  Length  Slot  Name   Signature
            0      18     0  args   [Ljava/lang/String;
            3      15     1     a   Ljava/lang/String;
    Exceptions:
      throws java.lang.Exception
}
SourceFile: "CommonTest.java"
➜  test javap -l CommonTest
警告: 二进制文件CommonTest包含com.meituan.mtlearner.cheetah.service.test.CommonTest
Compiled from "CommonTest.java"
public class com.meituan.mtlearner.cheetah.service.test.CommonTest {
  public com.meituan.mtlearner.cheetah.service.test.CommonTest();
    LineNumberTable:
      line 3: 0
    LocalVariableTable:
      Start  Length  Slot  Name   Signature
          0       5     0  this   Lcom/meituan/mtlearner/cheetah/service/test/CommonTest;

  public static void main(java.lang.String[]) throws java.lang.Exception;
    LineNumberTable:
      line 7: 0
      line 8: 3
      line 9: 10
    LocalVariableTable:
      Start  Length  Slot  Name   Signature
          0      18     0  args   [Ljava/lang/String;
          3      15     1     a   Ljava/lang/String;
}
➜  test javap -c CommonTest
警告: 二进制文件CommonTest包含com.meituan.mtlearner.cheetah.service.test.CommonTest
Compiled from "CommonTest.java"
public class com.meituan.mtlearner.cheetah.service.test.CommonTest {
  public com.meituan.mtlearner.cheetah.service.test.CommonTest();
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."":()V
       4: return

  public static void main(java.lang.String[]) throws java.lang.Exception;
    Code:
       0: ldc           #2                  // String 222
       2: astore_1
       3: getstatic     #3                  // Field java/lang/System.out:Ljava/io/PrintStream;
       6: aload_1
       7: invokevirtual #4                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V
      10: new           #5                  // class java/lang/Exception
      13: dup
      14: invokespecial #6                  // Method java/lang/Exception."":()V
      17: athrow
}

基本上javap -v就包含了所有的反编译信息,如果觉得不够,就增加个help里面的标签就好了。
其实通过javap -v展示的内容不难看出字节码文件的组成:最后修改时间,文件大小、MD5签名、全限定名、版本、访问权限标签、常量池、成员变量信息、方法信息、属性信息等。(http://www.blogjava.net/DLevin/archive/2011/09/05/358033.html)
而每个方法都包含至少三部分:签名、访问权限符、Code,Code中又包含指令集、对应信息、局部变量表。
对照书本上或者一些文档中的字节码文件的组成,通过javap这个小工具来了解字节码文件(.class)更直观一些。

你可能感兴趣的:(javap命令解析)