Java反编译工具: javap

Java反编译工具: javap

将看不懂的.class文件转换成看得懂的.java文件。
编译:javac -encoding UTF-8 Hello.java

javap使用:

javap -c Hello.class

-c 对代码进行反汇编
-v 输出附加信息
-constants 显示最终常量

举例代码:String,StringBuider的使用:

public class Main {

    public static String balance(String[] str){
        String ret="";
        for (int i = 0; i <str.length ; i++) {
            ret+=str[i];
        }
        return ret;
    }

    public static String change(String[] str){
        StringBuilder ret = new StringBuilder();

        for (int i = 0; i <str.length ; i++) {
            ret.append(str[i]);
        }
        return ret.toString();
    }

    public static void main(String[] args) {
        String[]  strs={"hello","world","What"};
        balance(strs);
        change(strs);
    }
}

1 首先进行编译:
在这里插入图片描述

2 打开idea的终端,并找到Main.class文件所在的目录:
在这里插入图片描述
在这里插入图片描述
先看balance方法的反编译字节码:

1
之后再补充吧…javap -c -v -constans Main.class

Classfile /G:/1workplace/jingdong/out/production/jingdong/Main.class
  Last modified 2020527; size 1535 bytes
  MD5 checksum 011aea3cf523488384f29a3324b76208
  Compiled from "Main.java"
public class Main
  minor version: 0
  major version: 53
  flags: (0x0021) ACC_PUBLIC, ACC_SUPER
  this_class: #14                         // Main
  super_class: #15                        // java/lang/Object
  interfaces: 0, fields: 0, methods: 4, attributes: 3
Constant pool:
   #1 = Methodref          #15.#40        // java/lang/Object."<init>":()V
   #2 = String             #41            //
   #3 = InvokeDynamic      #0:#45         // #0:makeConcatWithConstants:(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;
   #4 = Class              #46            // java/lang/StringBuilder
   #5 = Methodref          #4.#40         // java/lang/StringBuilder."<init>":()V
   #6 = Methodref          #4.#47         // java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
   #7 = Methodref          #4.#48         // java/lang/StringBuilder.toString:()Ljava/lang/String;
   #8 = Class              #49            // java/lang/String
   #9 = String             #50            // hello
  #10 = String             #51            // world
  #11 = String             #52            // What
  #12 = Methodref          #14.#53        // Main.balance:([Ljava/lang/String;)Ljava/lang/String;
  #13 = Methodref          #14.#54        // Main.change:([Ljava/lang/String;)Ljava/lang/String;
  #14 = Class              #55            // Main
  #15 = Class              #56            // java/lang/Object
  #16 = Utf8               <init>
  #17 = Utf8               ()V
  #18 = Utf8               Code
  #19 = Utf8               LineNumberTable
  #20 = Utf8               LocalVariableTable
  #21 = Utf8               this
  #22 = Utf8               LMain;
  #23 = Utf8               balance
  #24 = Utf8               ([Ljava/lang/String;)Ljava/lang/String;
  #25 = Utf8               i
  #26 = Utf8               I
  #27 = Utf8               str
  #28 = Utf8               [Ljava/lang/String;
  #29 = Utf8               ret
  #30 = Utf8               Ljava/lang/String;
  #31 = Utf8               StackMapTable
  #32 = Utf8               change
  #33 = Utf8               Ljava/lang/StringBuilder;
  #34 = Utf8               main
  #35 = Utf8               ([Ljava/lang/String;)V
  #36 = Utf8               args
  #37 = Utf8               strs
  #38 = Utf8               SourceFile
  #39 = Utf8               Main.java
  #40 = NameAndType        #16:#17        // "<init>":()V
  #41 = Utf8
  #42 = Utf8               BootstrapMethods
  #43 = MethodHandle       6:#57          // REF_invokeStatic java/lang/invoke/StringConcatFactory.makeConcatWithConstants:(Ljava/lang/invoke/MethodHandles$Look
up;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/invoke/CallSite;
  #44 = String             #58            // \u0001\u0001
  #45 = NameAndType        #59:#60        // makeConcatWithConstants:(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;
  #46 = Utf8               java/lang/StringBuilder
  #47 = NameAndType        #61:#62        // append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
  #48 = NameAndType        #63:#64        // toString:()Ljava/lang/String;
  #49 = Utf8               java/lang/String
  #50 = Utf8               hello
  #51 = Utf8               world
  #52 = Utf8               What
  #53 = NameAndType        #23:#24        // balance:([Ljava/lang/String;)Ljava/lang/String;
  #54 = NameAndType        #32:#24        // change:([Ljava/lang/String;)Ljava/lang/String;
  #55 = Utf8               Main
  #56 = Utf8               java/lang/Object
  #57 = Methodref          #65.#66        // java/lang/invoke/StringConcatFactory.makeConcatWithConstants:(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/Str
ing;Ljava/lang/invoke/MethodType;Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/invoke/CallSite;
  #58 = Utf8               \u0001\u0001
  #59 = Utf8               makeConcatWithConstants
  #60 = Utf8               (Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;
  #61 = Utf8               append
  #62 = Utf8               (Ljava/lang/String;)Ljava/lang/StringBuilder;
  #63 = Utf8               toString
  #64 = Utf8               ()Ljava/lang/String;
  #65 = Class              #67            // java/lang/invoke/StringConcatFactory
  #66 = NameAndType        #59:#71        // makeConcatWithConstants:(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljav
a/lang/String;[Ljava/lang/Object;)Ljava/lang/invoke/CallSite;
  #67 = Utf8               java/lang/invoke/StringConcatFactory
  #68 = Class              #73            // java/lang/invoke/MethodHandles$Lookup
  #69 = Utf8               Lookup
  #70 = Utf8               InnerClasses
  #71 = Utf8               (Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/String;[Ljava/lang/Object;)Ljava/la
ng/invoke/CallSite;
  #72 = Class              #74            // java/lang/invoke/MethodHandles
  #73 = Utf8               java/lang/invoke/MethodHandles$Lookup
  #74 = Utf8               java/lang/invoke/MethodHandles
{
  public Main();
    descriptor: ()V
    flags: (0x0001) ACC_PUBLIC
    Code:
      stack=1, locals=1, args_size=1
         0: aload_0
         1: invokespecial #1                  // Method java/lang/Object."<init>":()V
         4: return
      LineNumberTable:
        line 1: 0
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0       5     0  this   LMain;

  public static java.lang.String balance(java.lang.String[]);
    descriptor: ([Ljava/lang/String;)Ljava/lang/String;
    flags: (0x0009) ACC_PUBLIC, ACC_STATIC
    Code:
      stack=3, locals=3, args_size=1
         0: ldc           #2                  // String
         2: astore_1
         3: iconst_0
         4: istore_2
         5: iload_2
         6: aload_0
         7: arraylength
         8: if_icmpge     27
        11: aload_1
        12: aload_0
        13: iload_2
        14: aaload
        15: invokedynamic #3,  0              // InvokeDynamic #0:makeConcatWithConstants:(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;
        20: astore_1
        21: iinc          2, 1
        24: goto          5
        27: aload_1
        28: areturn
      LineNumberTable:
        line 4: 0
        line 5: 3
        line 6: 11
        line 5: 21
        line 8: 27
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            5      22     2     i   I
            0      29     0   str   [Ljava/lang/String;
            3      26     1   ret   Ljava/lang/String;
      StackMapTable: number_of_entries = 2
        frame_type = 253 /* append */
          offset_delta = 5
          locals = [ class java/lang/String, int ]
        frame_type = 250 /* chop */
          offset_delta = 21

  public static java.lang.String change(java.lang.String[]);
    descriptor: ([Ljava/lang/String;)Ljava/lang/String;
    flags: (0x0009) ACC_PUBLIC, ACC_STATIC
    Code:
      stack=3, locals=3, args_size=1
         0: new           #4                  // class java/lang/StringBuilder
         3: dup
         4: invokespecial #5                  // Method java/lang/StringBuilder."<init>":()V
         7: astore_1
         8: iconst_0
         9: istore_2
        10: iload_2
        11: aload_0
        12: arraylength
        13: if_icmpge     30
        16: aload_1
        17: aload_0
        18: iload_2
        19: aaload
        20: invokevirtual #6                  // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
        23: pop
        24: iinc          2, 1
        27: goto          10
        30: aload_1
        31: invokevirtual #7                  // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
        34: areturn
      LineNumberTable:
        line 12: 0
        line 14: 8
        line 15: 16
        line 14: 24
        line 17: 30
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
           10      20     2     i   I
            0      35     0   str   [Ljava/lang/String;
            8      27     1   ret   Ljava/lang/StringBuilder;
      StackMapTable: number_of_entries = 2
        frame_type = 253 /* append */
          offset_delta = 10
          locals = [ class java/lang/StringBuilder, int ]
        frame_type = 250 /* chop */
          offset_delta = 19

  public static void main(java.lang.String[]);
    descriptor: ([Ljava/lang/String;)V
    flags: (0x0009) ACC_PUBLIC, ACC_STATIC
    Code:
      stack=4, locals=2, args_size=1
         0: iconst_3
         1: anewarray     #8                  // class java/lang/String
         4: dup
         5: iconst_0
         6: ldc           #9                  // String hello
         8: aastore
         9: dup
        10: iconst_1
        11: ldc           #10                 // String world
        13: aastore
        14: dup
        15: iconst_2
        16: ldc           #11                 // String What
        18: aastore
        19: astore_1
        20: aload_1
        21: invokestatic  #12                 // Method balance:([Ljava/lang/String;)Ljava/lang/String;
        24: pop
        25: aload_1
        26: invokestatic  #13                 // Method change:([Ljava/lang/String;)Ljava/lang/String;
        29: pop
        30: return
      LineNumberTable:
        line 21: 0
        line 22: 20
        line 23: 25
        line 24: 30
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0      31     0  args   [Ljava/lang/String;
           20      11     1  strs   [Ljava/lang/String;
}
SourceFile: "Main.java"
InnerClasses:
  public static final #69= #68 of #72;    // Lookup=class java/lang/invoke/MethodHandles$Lookup of class java/lang/invoke/MethodHandles
BootstrapMethods:
  0: #43 REF_invokeStatic java/lang/invoke/StringConcatFactory.makeConcatWithConstants:(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invo
ke/MethodType;Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/invoke/CallSite;
    Method arguments:
      #44 \u0001\u0001

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值