当前位置:   article > 正文

查看Java字节码内容的几种方式_怎么查看java的字节码

怎么查看java的字节码

1、javap

通过javacjavap查看:先通过javac.java文件编译成.class字节码文件,然后通过javap -verbose分析字节码。

(base) tinytongtongdeMacBook-Pro% javac JVMRunTimeStorageTest.java
(base) tinytongtongdeMacBook-Pro% javap -verbose JVMRunTimeStorageTest
  • 1
  • 2

这样你就能看到你的字节码信息了。

示例如下:

Classfile /Users/wangjianzhou/Documents/workspace/java/JavaStudy/src/com/tinytongtong/javastudy/jvm/JVMRunTimeStorageTest.class
  Last modified Mar 16, 2022; size 657 bytes
  MD5 checksum a13778df0b853b554f2a5da237c53672
  Compiled from "JVMRunTimeStorageTest.java"
public class com.tinytongtong.javastudy.jvm.JVMRunTimeStorageTest
  minor version: 0
  major version: 52
  flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
   #1 = Methodref          #5.#21         // java/lang/Object."<init>":()V
   #2 = Double             2.0d
   #4 = Class              #22            // com/tinytongtong/javastudy/jvm/JVMRunTimeStorageTest
   #5 = Class              #23            // java/lang/Object
   #6 = Utf8               <init>
   #7 = Utf8               ()V
   #8 = Utf8               Code
   #9 = Utf8               LineNumberTable
  #10 = Utf8               main
  #11 = Utf8               ([Ljava/lang/String;)V
  #12 = Utf8               add
  #13 = Utf8               (I)I
  #14 = Utf8               add1
  #15 = Utf8               add2
  #16 = Utf8               ()I
  #17 = Utf8               foreach
  #18 = Utf8               StackMapTable
  #19 = Utf8               SourceFile
  #20 = Utf8               JVMRunTimeStorageTest.java
  #21 = NameAndType        #6:#7          // "<init>":()V
  #22 = Utf8               com/tinytongtong/javastudy/jvm/JVMRunTimeStorageTest
  #23 = Utf8               java/lang/Object
{
  public com.tinytongtong.javastudy.jvm.JVMRunTimeStorageTest();
    descriptor: ()V
    flags: 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 8: 0

  public static void main(java.lang.String[]);
    descriptor: ([Ljava/lang/String;)V
    flags: ACC_PUBLIC, ACC_STATIC
    Code:
      stack=0, locals=1, args_size=1
         0: return
      LineNumberTable:
        line 12: 0

  public static int add(int);
    descriptor: (I)I
    flags: ACC_PUBLIC, ACC_STATIC
    Code:
      stack=2, locals=3, args_size=1
         0: iconst_1
         1: istore_1
         2: iconst_2
         3: istore_2
         4: iload_1
         5: iload_2
         6: iadd
         7: iload_0
         8: iadd
         9: ireturn
      LineNumberTable:
        line 28: 0
        line 29: 2
        line 30: 4

  public static int add1(int);
    descriptor: (I)I
    flags: ACC_PUBLIC, ACC_STATIC
    Code:
      stack=4, locals=5, args_size=1
         0: lconst_1
         1: lstore_1
         2: ldc2_w        #2                  // double 2.0d
         5: dstore_3
         6: lload_1
         7: l2d
         8: dload_3
         9: dadd
        10: iload_0
        11: i2d
        12: dadd
        13: d2i
        14: ireturn
      LineNumberTable:
        line 42: 0
        line 43: 2
        line 44: 6

  public static int add2();
    descriptor: ()I
    flags: ACC_PUBLIC, ACC_STATIC
    Code:
      stack=2, locals=3, args_size=0
         0: iconst_1
         1: istore_0
         2: iconst_2
         3: istore_1
         4: iload_0
         5: iload_1
         6: iadd
         7: istore_2
         8: iload_2
         9: bipush        10
        11: iadd
        12: ireturn
      LineNumberTable:
        line 55: 0
        line 56: 2
        line 57: 4
        line 58: 8

  public static int foreach(int);
    descriptor: (I)I
    flags: ACC_PUBLIC, ACC_STATIC
    Code:
      stack=2, locals=3, args_size=1
         0: iconst_0
         1: istore_1
         2: iconst_0
         3: istore_2
         4: iload_2
         5: iload_0
         6: if_icmpge     19
         9: iload_1
        10: iload_2
        11: iadd
        12: istore_1
        13: iinc          2, 1
        16: goto          4
        19: iload_1
        20: ireturn
      LineNumberTable:
        line 70: 0
        line 71: 2
        line 72: 9
        line 71: 13
        line 74: 19
      StackMapTable: number_of_entries = 2
        frame_type = 253 /* append */
          offset_delta = 4
          locals = [ int, int ]
        frame_type = 250 /* chop */
          offset_delta = 14
}
SourceFile: "JVMRunTimeStorageTest.java"
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152

demo-JVMRunTimeStorageTest

2、ASM Bytecode Outline 插件

给IDE安装ASM Bytecode Outline插件。

安装好之后,重启IDE。

接着选择对应的java文件,右键 --> Show Bytecode outline,可以看到ASM的输出,如下图所示
在这里插入图片描述
在这里插入图片描述

3、jclasslib Bytecode Viewer

jclasslib Bytecode Viewer

我们可以通过这个插件,以可视化的方式查看字节码中的信息,比如方法Code表中的局部变量表信息。如下图所示:

打开对应的java文件,View --> Show Bytecode With Jclasslib
在这里插入图片描述
在这里插入图片描述

我们可以看到局部变量表的长度。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/224168
推荐阅读
相关标签
  

闽ICP备14008679号