javap命令与Java-DcompilerIDEA等反编译的结果的差别

45次阅读

共计 3329 个字符,预计需要花费 9 分钟才能阅读完成。

源代码:

public enum Season {
    SPIRNG,
    SUMMER,
    AUTUMN,
    WINTER;

    public String allSeasons() {return SPIRNG.name() + "" + SUMMER.name() +" "+ AUTUMN.name() +" " +WINTER.name();}
}

编译之后使用 javap -c Season.class 命令反编译结果:

public final class Season extends java.lang.Enum<Season> {
  public static final Season SPIRNG;

  public static final Season SUMMER;

  public static final Season AUTUMN;

  public static final Season WINTER;

  public static Season[] values();
    Code:
       0: getstatic     #1                  // Field $VALUES:[LSeason;
       3: invokevirtual #2                  // Method "[LSeason;".clone:()Ljava/lang/Object;
       6: checkcast     #3                  // class "[LSeason;"
       9: areturn

  public static Season valueOf(java.lang.String);
    Code:
       0: ldc           #4                  // class Season
       2: aload_0
       3: invokestatic  #5                  // Method java/lang/Enum.valueOf:(Ljava/lang/Class;Ljava/lang/String;)Ljava/lang/Enum;
       6: checkcast     #4                  // class Season
       9: areturn

  public java.lang.String allSeasons();
    Code:
       0: new           #7                  // class java/lang/StringBuilder
       3: dup
       4: invokespecial #8                  // Method java/lang/StringBuilder."<init>":()V
       7: getstatic     #9                  // Field SPIRNG:LSeason;
      10: invokevirtual #10                 // Method name:()Ljava/lang/String;
      13: invokevirtual #11                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      16: ldc           #12                 // String
      18: invokevirtual #11                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      21: getstatic     #13                 // Field SUMMER:LSeason;
      24: invokevirtual #10                 // Method name:()Ljava/lang/String;
      27: invokevirtual #11                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      30: ldc           #12                 // String
      32: invokevirtual #11                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      35: getstatic     #14                 // Field AUTUMN:LSeason;
      38: invokevirtual #10                 // Method name:()Ljava/lang/String;
      41: invokevirtual #11                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      44: ldc           #12                 // String
      46: invokevirtual #11                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      49: getstatic     #15                 // Field WINTER:LSeason;
      52: invokevirtual #10                 // Method name:()Ljava/lang/String;
      55: invokevirtual #11                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      58: invokevirtual #16                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
      61: areturn

  static {};
    Code:
       0: new           #4                  // class Season
       3: dup
       4: ldc           #17                 // String SPIRNG
       6: iconst_0
       7: invokespecial #18                 // Method "<init>":(Ljava/lang/String;I)V
      10: putstatic     #9                  // Field SPIRNG:LSeason;
      13: new           #4                  // class Season
      16: dup
      17: ldc           #19                 // String SUMMER
      19: iconst_1
      20: invokespecial #18                 // Method "<init>":(Ljava/lang/String;I)V
      23: putstatic     #13                 // Field SUMMER:LSeason;
      26: new           #4                  // class Season
      29: dup
      30: ldc           #20                 // String AUTUMN
      32: iconst_2
      33: invokespecial #18                 // Method "<init>":(Ljava/lang/String;I)V
      36: putstatic     #14                 // Field AUTUMN:LSeason;
      39: new           #4                  // class Season
      42: dup
      43: ldc           #21                 // String WINTER
      45: iconst_3
      46: invokespecial #18                 // Method "<init>":(Ljava/lang/String;I)V
      49: putstatic     #15                 // Field WINTER:LSeason;
      52: iconst_4
      53: anewarray     #4                  // class Season
      56: dup
      57: iconst_0
      58: getstatic     #9                  // Field SPIRNG:LSeason;
      61: aastore
      62: dup
      63: iconst_1
      64: getstatic     #13                 // Field SUMMER:LSeason;
      67: aastore
      68: dup
      69: iconst_2
      70: getstatic     #14                 // Field AUTUMN:LSeason;
      73: aastore
      74: dup
      75: iconst_3
      76: getstatic     #15                 // Field WINTER:LSeason;
      79: aastore
      80: putstatic     #1                  // Field $VALUES:[LSeason;
      83: return
}

编译之后使用 Java Decompiler 的反编译结果:

public enum Season
{
  SPIRNG,  SUMMER,  AUTUMN,  WINTER;
  
  private Season() {}
  
  public String allSeasons()
  {return SPIRNG.name() + "" + SUMMER.name() +" "+ AUTUMN.name() +" " + WINTER.name();}
}

反编译结果不相同可以分为两部分:

  1. javap 反编译结果包含了 static(),valueOf 方法;
  2. javap 反编译结果的方法体使用了 jvm 指令。

正文完
 0