文章目录
前言
一、编译生成带局部变量表的字节码文件
二、局部变量表
前言
上一篇博客 【Java 虚拟机原理】Class 字节码二进制文件分析 二 ( 常量池位置 | 常量池结构 | tag | info[] | 完整分析字节码文件中的常量池二进制数据 ) ;
一、编译生成带局部变量表的字节码文件
在 IntelliJ IDEA 中编写如下两个源码 :
Java 类源码 : 在 setName 方法下 , 声明 3 33 个局部变量 ;
public class Student { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; int i = 0; int j = 1; int k = 2; } }
在 main 函数中 创建上述 Student 类对象 : 一定要写这个 main 函数 , 否则虚拟机编译优化时 , 发现 setName 中的局部变量没有使用 , 直接优化掉 , 不生成相关的 局部变量表 ;
public class Main { public static void main(String[] args) { Student student = new Student(); } }
找到上述两个类编译后的字节码文件 : 根据上一篇博客 【Java 虚拟机原理】Class 字节码二进制文件分析 二 ( 常量池位置 | 常量池结构 | tag | info[] | 完整分析字节码文件中的常量池二进制数据 ) 分析 , 常量池是如下选中的区域 ;
Student.class 字节码文件的附加信息如下 :
Y:\002_WorkSpace\003_IDEA\Demo\out\production\Demo>javap -v Student.class Classfile /Y:/002_WorkSpace/003_IDEA/Demo/out/production/Demo/Student.class Last modified 2021-9-5; size 561 bytes MD5 checksum 76a00ba8cb4c4c6aadc52f90e550d7e8 Compiled from "Student.java" public class Student minor version: 0 major version: 52 flags: ACC_PUBLIC, ACC_SUPER Constant pool: #1 = Methodref #4.#24 // java/lang/Object."<init>":()V #2 = Fieldref #3.#25 // Student.name:Ljava/lang/String; #3 = Class #26 // Student #4 = Class #27 // java/lang/Object #5 = Utf8 name #6 = Utf8 Ljava/lang/String; #7 = Utf8 <init> #8 = Utf8 ()V #9 = Utf8 Code #10 = Utf8 LineNumberTable #11 = Utf8 LocalVariableTable #12 = Utf8 this #13 = Utf8 LStudent; #14 = Utf8 getName #15 = Utf8 ()Ljava/lang/String; #16 = Utf8 setName #17 = Utf8 (Ljava/lang/String;)V #18 = Utf8 i #19 = Utf8 I #20 = Utf8 j #21 = Utf8 k #22 = Utf8 SourceFile #23 = Utf8 Student.java #24 = NameAndType #7:#8 // "<init>":()V #25 = NameAndType #5:#6 // name:Ljava/lang/String; #26 = Utf8 Student #27 = Utf8 java/lang/Object { public Student(); 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 1: 0 LocalVariableTable: Start Length Slot Name Signature 0 5 0 this LStudent; public java.lang.String getName(); descriptor: ()Ljava/lang/String; flags: ACC_PUBLIC Code: stack=1, locals=1, args_size=1 0: aload_0 1: getfield #2 // Field name:Ljava/lang/String; 4: areturn LineNumberTable: line 5: 0 LocalVariableTable: Start Length Slot Name Signature 0 5 0 this LStudent; public void setName(java.lang.String); descriptor: (Ljava/lang/String;)V flags: ACC_PUBLIC Code: stack=2, locals=5, args_size=2 0: aload_0 1: aload_1 2: putfield #2 // Field name:Ljava/lang/String; 5: iconst_0 6: istore_2 7: iconst_1 8: istore_3 9: iconst_2 10: istore 4 12: return LineNumberTable: line 9: 0 line 10: 5 line 11: 7 line 12: 9 line 13: 12 LocalVariableTable: Start Length Slot Name Signature 0 13 0 this LStudent; 0 13 1 name Ljava/lang/String; 7 6 2 i I 9 4 3 j I 12 1 4 k I } SourceFile: "Student.java"