java反编译字节码

 

 

class代码:

package my;

public class MyTest {

    public static void main(String[] args) {
        String a = "abc";
        String r = a + "xyz" + "swq";
    }
}

 

执行编译,生成MyTest.class文件:

javac MyTest.java

执行反编译:

javap -c MyTest.class

也可以反编译到文件:

javap -c MyTest.class ->test.txt

反编译后的字节码:

Compiled from "MyTest.java"
public class my.MyTest {
  public my.MyTest();
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."":()V
       4: return

  public static void main(java.lang.String[]);
    Code:
       0: ldc           #2                  // String abc
       2: astore_1
       3: new           #3                  // class java/lang/StringBuilder
       6: dup
       7: invokespecial #4                  // Method java/lang/StringBuilder."":()V
      10: aload_1
      11: invokevirtual #5                  // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      14: ldc           #6                  // String xyz
      16: invokevirtual #5                  // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      19: ldc           #7                  // String swq
      21: invokevirtual #5                  // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      24: invokevirtual #8                  // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
      27: astore_2
      28: return
}

 

查看class文件:

javap -verbose MyTest.class ->test2.txt

class文件信息:

Classfile /E:/workspace/idea/ccpx/target/test-classes/my/MyTest.class
  Last modified 2019-7-14; size 624 bytes
  MD5 checksum 58bed288d9160333441c618441885985
  Compiled from "MyTest.java"
public class my.MyTest
  minor version: 0
  major version: 52
  flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
   #1 = Methodref          #10.#27        // java/lang/Object."":()V
   #2 = String             #28            // abc
   #3 = Class              #29            // java/lang/StringBuilder
   #4 = Methodref          #3.#27         // java/lang/StringBuilder."":()V
   #5 = Methodref          #3.#30         // java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
   #6 = String             #31            // xyz
   #7 = String             #32            // swq
   #8 = Methodref          #3.#33         // java/lang/StringBuilder.toString:()Ljava/lang/String;
   #9 = Class              #34            // my/MyTest
  #10 = Class              #35            // java/lang/Object
  #11 = Utf8               
  #12 = Utf8               ()V
  #13 = Utf8               Code
  #14 = Utf8               LineNumberTable
  #15 = Utf8               LocalVariableTable
  #16 = Utf8               this
  #17 = Utf8               Lmy/MyTest;
  #18 = Utf8               main
  #19 = Utf8               ([Ljava/lang/String;)V
  #20 = Utf8               args
  #21 = Utf8               [Ljava/lang/String;
  #22 = Utf8               a
  #23 = Utf8               Ljava/lang/String;
  #24 = Utf8               r
  #25 = Utf8               SourceFile
  #26 = Utf8               MyTest.java
  #27 = NameAndType        #11:#12        // "":()V
  #28 = Utf8               abc
  #29 = Utf8               java/lang/StringBuilder
  #30 = NameAndType        #36:#37        // append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
  #31 = Utf8               xyz
  #32 = Utf8               swq
  #33 = NameAndType        #38:#39        // toString:()Ljava/lang/String;
  #34 = Utf8               my/MyTest
  #35 = Utf8               java/lang/Object
  #36 = Utf8               append
  #37 = Utf8               (Ljava/lang/String;)Ljava/lang/StringBuilder;
  #38 = Utf8               toString
  #39 = Utf8               ()Ljava/lang/String;
{
  public my.MyTest();
    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   Lmy/MyTest;

  public static void main(java.lang.String[]);
    descriptor: ([Ljava/lang/String;)V
    flags: ACC_PUBLIC, ACC_STATIC
    Code:
      stack=2, locals=3, args_size=1
         0: ldc           #2                  // String abc
         2: astore_1
         3: new           #3                  // class java/lang/StringBuilder
         6: dup
         7: invokespecial #4                  // Method java/lang/StringBuilder."":()V
        10: aload_1
        11: invokevirtual #5                  // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
        14: ldc           #6                  // String xyz
        16: invokevirtual #5                  // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
        19: ldc           #7                  // String swq
        21: invokevirtual #5                  // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
        24: invokevirtual #8                  // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
        27: astore_2
        28: return
      LineNumberTable:
        line 6: 0
        line 7: 3
        line 8: 28
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0      29     0  args   [Ljava/lang/String;
            3      26     1     a   Ljava/lang/String;
           28       1     2     r   Ljava/lang/String;
}
SourceFile: "MyTest.java"

 

你可能感兴趣的:(java)