javap 反汇编

2021/8/3 17:09:03

本文主要是介绍javap 反汇编,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

文章目录

  • 1、把一个java类反汇编
    • 1.1、写一个类Hello.java
    • 1.2、javac 编译
    • 1.3、javap 反汇编
  • 2、分析汇编代码

1、把一个java类反汇编

1.1、写一个类Hello.java

Hello.java

public class Hello {
    private Integer num1 = 0;
    private Integer num2;

    public Integer getNum(Integer num){
        this.num1 += num;
        this.num2 = num * 100;
        return num2 + num1;
    }

    public static void main(String[] args) {
        Hello h = new Hello();
        System.out.println(h.getNum(20));
    }
}

1.2、javac 编译

输入指令:javac Hello.java
得到Hello.class

public class Hello {
    private Integer num1 = 0;
    private Integer num2;

    public Hello() {
    }

    public Integer getNum(Integer var1) {
        this.num1 = this.num1 + var1;
        this.num2 = var1 * 100;
        return this.num2 + this.num1;
    }

    public static void main(String[] var0) {
        Hello var1 = new Hello();
        System.out.println(var1.getNum(20));
    }
}

1.3、javap 反汇编

输入指令:javap -c Hello
得到反汇编内容

警告: 二进制文件Hello包含Hello
Compiled from "Hello.java"
public class Hello {
  public Hello();
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."<init>":()V
       4: aload_0
       5: iconst_0
       6: invokestatic  #2                  // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
       9: putfield      #3                  // Field num1:Ljava/lang/Integer;
      12: return

  public java.lang.Integer getNum(java.lang.Integer);
    Code:
       0: aload_0
       1: astore_2
       2: aload_2
       3: aload_2
       4: getfield      #3                  // Field num1:Ljava/lang/Integer;
       7: invokevirtual #4                  // Method java/lang/Integer.intValue:()I
      10: aload_1
      11: invokevirtual #4                  // Method java/lang/Integer.intValue:()I
      14: iadd
      15: invokestatic  #2                  // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      18: dup_x1
      19: putfield      #3                  // Field num1:Ljava/lang/Integer;
      22: pop
      23: aload_0
      24: aload_1
      25: invokevirtual #4                  // Method java/lang/Integer.intValue:()I
      28: bipush        100
      30: imul
      31: invokestatic  #2                  // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      34: putfield      #5                  // Field num2:Ljava/lang/Integer;
      37: aload_0
      38: getfield      #5                  // Field num2:Ljava/lang/Integer;
      41: invokevirtual #4                  // Method java/lang/Integer.intValue:()I
      44: aload_0
      45: getfield      #3                  // Field num1:Ljava/lang/Integer;
      48: invokevirtual #4                  // Method java/lang/Integer.intValue:()I
      51: iadd
      52: invokestatic  #2                  // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      55: areturn

  public static void main(java.lang.String[]);
    Code:
       0: new           #6                  // class Hello
       3: dup
       4: invokespecial #7                  // Method "<init>":()V
       7: astore_1
       8: getstatic     #8                  // Field java/lang/System.out:Ljava/io/PrintStream;
      11: aload_1
      12: bipush        20
      14: invokestatic  #2                  // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      17: invokevirtual #9                  // Method getNum:(Ljava/lang/Integer;)Ljava/lang/Integer;
      20: invokevirtual #10                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
      23: return
}

2、分析汇编代码

先看看里面方法大概有哪些

public class Hello {
  public Hello();                                           // 构造方法
    Code:
......

  public java.lang.Integer getNum(java.lang.Integer);
    Code:
......

  public static void main(java.lang.String[]);              // 主方法
    Code:
......
}

看看构造方法的代码内容

  public Hello();
    Code:
       0: aload_0                // 加载索引为0的变量的值
       1: invokespecial #1       
       4: aload_0
       5: iconst_0               //将常量0,压入到操作数栈
       6: invokestatic  #2      
       9: putfield      #3       
      12: return


这篇关于javap 反汇编的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程