//环境的搭建:
1.创建一个类并编译它
2.使用java命令:javap -c -verbose build/classes/java/main/com/yang/jvm/Test.class 将16进制的字节码文件转成可视化的文件内容如下:
具体内容如下:
Classfile /F:/jvmdemo/build/classes/java/main/com/yang/jvm/Test.class Last modified 2019-10-17; size 573 bytes MD5 checksum f85b42e59f656b2cae810d27e91ccd7d Compiled from "Test.java" public class com.yang.jvm.Test minor version: 0 major version: 52 flags: ACC_PUBLIC, ACC_SUPER Constant pool: #1 = Methodref #6.#25 // java/lang/Object."<init>":()V #2 = Fieldref #5.#26 // com/yang/jvm/Test.i:I #3 = String #27 // hello #4 = Fieldref #5.#28 // com/yang/jvm/Test.str:Ljava/lang/String; #5 = Class #29 // com/yang/jvm/Test #6 = Class #30 // java/lang/Object #7 = Utf8 i #8 = Utf8 I #9 = Utf8 str #10 = Utf8 Ljava/lang/String; #11 = Utf8 <init> #12 = Utf8 ()V #13 = Utf8 Code #14 = Utf8 LineNumberTable #15 = Utf8 LocalVariableTable #16 = Utf8 this #17 = Utf8 Lcom/yang/jvm/Test; #18 = Utf8 getI #19 = Utf8 ()I #20 = Utf8 setI #21 = Utf8 (I)V #22 = Utf8 <clinit> #23 = Utf8 SourceFile #24 = Utf8 Test.java #25 = NameAndType #11:#12 // "<init>":()V #26 = NameAndType #7:#8 // i:I #27 = Utf8 hello #28 = NameAndType #9:#10 // str:Ljava/lang/String; #29 = Utf8 com/yang/jvm/Test #30 = Utf8 java/lang/Object { public com.yang.jvm.Test(); descriptor: ()V flags: ACC_PUBLIC Code: stack=2, locals=1, args_size=1 0: aload_0 1: invokespecial #1 // Method java/lang/Object."<init>":()V 4: aload_0 5: iconst_5 6: putfield #2 // Field i:I 9: return LineNumberTable: line 3: 0 line 4: 4 LocalVariableTable: Start Length Slot Name Signature 0 10 0 this Lcom/yang/jvm/Test; public int getI(); descriptor: ()I flags: ACC_PUBLIC Code: stack=1, locals=1, args_size=1 0: aload_0 1: getfield #2 // Field i:I 4: ireturn LineNumberTable: line 9: 0 LocalVariableTable: Start Length Slot Name Signature 0 5 0 this Lcom/yang/jvm/Test; public void setI(int); descriptor: (I)V flags: ACC_PUBLIC Code: stack=2, locals=2, args_size=2 0: aload_0 1: iload_1 2: putfield #2 // Field i:I 5: return LineNumberTable: line 13: 0 line 14: 5 LocalVariableTable: Start Length Slot Name Signature 0 6 0 this Lcom/yang/jvm/Test; 0 6 1 i I static {}; descriptor: ()V flags: ACC_STATIC Code: stack=1, locals=0, args_size=0 0: ldc #3 // String hello 2: putstatic #4 // Field str:Ljava/lang/String; 5: return LineNumberTable: line 6: 0 } SourceFile: "Test.java"
此外:idea也有插件可以得到上面的字节码内容,安装插件jclasslib:使用该插件的好处是,对应的指令如aload_0可以点击,链接到oracle官网进行解释
安装重启后,鼠标点击在对应的java文件上,不是class文件,如Test.java文件,然后点击view菜单如图:
在右边可以看到:
原文地址:https://www.cnblogs.com/yangxiaohui227/p/11690956.html
时间: 2024-10-11 13:58:21