从一个基本的类的反编译说起让你读懂Java字节码文件

从一个基本的类的反编译说起:

javap是一个能够将class文件反汇编成人类可读的格式的工具。可以方便的查阅Java的字节码。
例如下面的例子:

public class Coo{

    private int tryBlock;

    private int catchBlock;

    private int finallyBlock;

    private int methodExit;

    public void test(){

        try{
            tryBlock = 0;
        }catch(Exception e){
            catchBlock = 1;
        }finally{
            finallyBlock = 2;
        }

        methodExit = 3;
    }

}

使用以下两条命令:

javac Coo.java

javap -p -v Coo

得到:

Classfile /C:/Users/dell/Desktop/bob/Coo.class
  Last modified ×××; size 540 bytes
  MD5 checksum d94c55f366ae593c8bb83fceda66f50a
  Compiled from "Coo.java"
public class Coo
  minor version: 0
  major version: 52
  flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
   #1 = Methodref          #8.#24         // java/lang/Object."<init>":()V
   #2 = Fieldref           #7.#25         // Coo.tryBlock:I
   #3 = Fieldref           #7.#26         // Coo.finallyBlock:I
   #4 = Class              #27            // java/lang/Exception
   #5 = Fieldref           #7.#28         // Coo.catchBlock:I
   #6 = Fieldref           #7.#29         // Coo.methodExit:I
   #7 = Class              #30            // Coo
   #8 = Class              #31            // java/lang/Object
   #9 = Utf8               tryBlock
  #10 = Utf8               I
  #11 = Utf8               catchBlock
  #12 = Utf8               finallyBlock
  #13 = Utf8               methodExit
  #14 = Utf8               <init>
  #15 = Utf8               ()V
  #16 = Utf8               Code
  #17 = Utf8               LineNumberTable
  #18 = Utf8               test
  #19 = Utf8               StackMapTable
  #20 = Class              #27            // java/lang/Exception
  #21 = Class              #32            // java/lang/Throwable
  #22 = Utf8               SourceFile
  #23 = Utf8               Coo.java
  #24 = NameAndType        #14:#15        // "<init>":()V
  #25 = NameAndType        #9:#10         // tryBlock:I
  #26 = NameAndType        #12:#10        // finallyBlock:I
  #27 = Utf8               java/lang/Exception
  #28 = NameAndType        #11:#10        // catchBlock:I
  #29 = NameAndType        #13:#10        // methodExit:I
  #30 = Utf8               Coo
  #31 = Utf8               java/lang/Object
  #32 = Utf8               java/lang/Throwable
{
  private int tryBlock;
    descriptor: I
    flags: ACC_PRIVATE

  private int catchBlock;
    descriptor: I
    flags: ACC_PRIVATE

  private int finallyBlock;
    descriptor: I
    flags: ACC_PRIVATE

  private int methodExit;
    descriptor: I
    flags: ACC_PRIVATE

  public Coo();
    descriptor: ()V
    flags: ACC_PUBLIC
    Code:
      stack=1, locals=1, args_size=1
         0: aload_0
         1: invokespecial #1                  // Method java/lang/Object."<init&g
  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值