Java反编译器如何从while循环中识别for循环?

时间:2016-08-23 22:50:42

标签: java decompiling

考虑以下两种方法:

public static void forLoop(int start, int limit) {
    for (int i = start; i < limit; i++) {

    }
}

public static void whileLoop(int start, int limit) {
    int i = start;
    while (i < limit) {
        i++;
    }
}

编译时,它们会生成字节码(这是javap的详细输出):

  public static void forLoop(int, int);
    descriptor: (II)V
    flags: ACC_PUBLIC, ACC_STATIC
    Code:
      stack=2, locals=3, args_size=2
         0: iload_0
         1: istore_2
         2: iload_2
         3: iload_1
         4: if_icmpge     13
         7: iinc          2, 1
        10: goto          2
        13: return
      LineNumberTable:
        line 6: 0
        line 9: 13
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            2      11     2     i   I
            0      14     0 start   I
            0      14     1 limit   I

  public static void whileLoop(int, int);
    descriptor: (II)V
    flags: ACC_PUBLIC, ACC_STATIC
    Code:
      stack=2, locals=3, args_size=2
         0: iload_0
         1: istore_2
         2: iload_2
         3: iload_1
         4: if_icmpge     13
         7: iinc          2, 1
        10: goto          2
        13: return
      LineNumberTable:
        line 12: 0
        line 13: 2
        line 14: 7
        line 16: 13
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0      14     0 start   I
            0      14     1 limit   I
            2      12     2     i   I

正如您所看到的,这两种方法的代码部分完全相同。但是,当我使用JD反编译此类时,它会正确生成:

public static void forLoop(int start, int limit) {
  for (int i = start; i < limit; i++) {}
}

public static void whileLoop(int start, int limit)
{
  int i = start;
  while (i < limit) {
    i++;
  }
}

它是如何做到这一点的?这些方法的字节码完全相同!尽管LineNumberTableLocalVariableTable属性对于每种方法都不同,但我不愿意相信这是因为这些属性不是方法的Code属性的必需属性。包含(每section 4.7 of The Java Language Specification, Java SE 8 Edition)。

1 个答案:

答案 0 :(得分:5)

行号和局部变量范围。

for循环:

 LineNumberTable:
    line 6: 0
    line 9: 13
  LocalVariableTable:
    Start  Length  Slot  Name   Signature
        2      11     2     i   I
        0      14     0 start   I
        0      14     1 limit   I

while循环:

LineNumberTable:
        line 12: 0
        line 13: 2
        line 14: 7
        line 16: 13
  LocalVariableTable:
    Start  Length  Slot  Name   Signature
        0      14     0 start   I
        0      14     1 limit   I
        2      12     2     i   I

for循环具有较少的不同代码行 - 这是有道理的,因为它将初始化和增量包装在一行中。