2-6class字节码:常量池

参考:https://tech.meituan.com/2019/09/05/java-bytecode-enhancement.html

Class File

image.png

这些信息是给虚拟机用的,虚拟机为了处理咱们写的这些类而需要的信息。

Classfile /Users/xixi/workspace6/JVMStudy/target/classes/com/xixi/jvm/classdef/Hello.class
  Last modified 2020年9月12日; size 1123 bytes
  MD5 checksum d558027a518219013be49f4bef4d0dd5
  Compiled from "Hello.java"
public class com.xixi.jvm.classdef.Hello
  minor version: 0
  major version: 55
  flags: (0x0021) ACC_PUBLIC, ACC_SUPER
  this_class: #7                          // com/xixi/jvm/classdef/Hello
  super_class: #8                         // java/lang/Object
  interfaces: 0, fields: 1, methods: 3, attributes: 3
Constant pool:
   #1 = Methodref          #8.#25         // java/lang/Object."":()V
   #2 = Fieldref           #26.#27        // java/lang/System.out:Ljava/io/PrintStream;
   #3 = Fieldref           #7.#28         // com/xixi/jvm/classdef/Hello.msg:Ljava/lang/String;
   #4 = InvokeDynamic      #0:#32         // #0:makeConcatWithConstants:(Ljava/lang/String;)Ljava/lang/String;
   #5 = Methodref          #33.#34        // java/io/PrintStream.println:(Ljava/lang/String;)V
   #6 = String             #35            // Good morning
   #7 = Class              #36            // com/xixi/jvm/classdef/Hello
   #8 = Class              #37            // java/lang/Object
   #9 = Utf8               msg
  #10 = Utf8               Ljava/lang/String;
  #11 = Utf8               
  #12 = Utf8               ()V
  #13 = Utf8               Code
  #14 = Utf8               LineNumberTable
  #15 = Utf8               LocalVariableTable
  #16 = Utf8               this
  #17 = Utf8               Lcom/xixi/jvm/classdef/Hello;
  #18 = Utf8               main
  #19 = Utf8               ([Ljava/lang/String;)V
  #20 = Utf8               args
  #21 = Utf8               [Ljava/lang/String;
  #22 = Utf8               
  #23 = Utf8               SourceFile
  #24 = Utf8               Hello.java
  #25 = NameAndType        #11:#12        // "":()V
  #26 = Class              #38            // java/lang/System
  #27 = NameAndType        #39:#40        // out:Ljava/io/PrintStream;
  #28 = NameAndType        #9:#10         // msg:Ljava/lang/String;
  #29 = Utf8               BootstrapMethods
  #30 = MethodHandle       6:#41          // REF_invokeStatic java/lang/invoke/StringConcatFactory.makeConcatWithConstants:(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/invoke/CallSite;
  #31 = String             #42            // msg == \u0001
  #32 = NameAndType        #43:#44        // makeConcatWithConstants:(Ljava/lang/String;)Ljava/lang/String;
  #33 = Class              #45            // java/io/PrintStream
  #34 = NameAndType        #46:#47        // println:(Ljava/lang/String;)V
  #35 = Utf8               Good morning
  #36 = Utf8               com/xixi/jvm/classdef/Hello
  #37 = Utf8               java/lang/Object
  #38 = Utf8               java/lang/System
  #39 = Utf8               out
  #40 = Utf8               Ljava/io/PrintStream;
  #41 = Methodref          #48.#49        // java/lang/invoke/StringConcatFactory.makeConcatWithConstants:(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/invoke/CallSite;
  #42 = Utf8               msg == \u0001
  #43 = Utf8               makeConcatWithConstants
  #44 = Utf8               (Ljava/lang/String;)Ljava/lang/String;
  #45 = Utf8               java/io/PrintStream
  #46 = Utf8               println
  #47 = Utf8               (Ljava/lang/String;)V
  #48 = Class              #50            // java/lang/invoke/StringConcatFactory
  #49 = NameAndType        #43:#54        // makeConcatWithConstants:(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/invoke/CallSite;
  #50 = Utf8               java/lang/invoke/StringConcatFactory
  #51 = Class              #56            // java/lang/invoke/MethodHandles$Lookup
  #52 = Utf8               Lookup
  #53 = Utf8               InnerClasses
  #54 = Utf8               (Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/invoke/CallSite;
  #55 = Class              #57            // java/lang/invoke/MethodHandles
  #56 = Utf8               java/lang/invoke/MethodHandles$Lookup
  #57 = Utf8               java/lang/invoke/MethodHandles
{
  public com.xixi.jvm.classdef.Hello();
    descriptor: ()V
    flags: (0x0001) 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   Lcom/xixi/jvm/classdef/Hello;

  public static void main(java.lang.String[]);
    descriptor: ([Ljava/lang/String;)V
    flags: (0x0009) ACC_PUBLIC, ACC_STATIC
    Code:
      stack=2, locals=1, args_size=1
         0: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;
         3: getstatic     #3                  // Field msg:Ljava/lang/String;
         6: invokedynamic #4,  0              // InvokeDynamic #0:makeConcatWithConstants:(Ljava/lang/String;)Ljava/lang/String;
        11: invokevirtual #5                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V
        14: return
      LineNumberTable:
        line 7: 0
        line 8: 14
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0      15     0  args   [Ljava/lang/String;

  static {};
    descriptor: ()V
    flags: (0x0008) ACC_STATIC
    Code:
      stack=1, locals=0, args_size=0
         0: ldc           #6                  // String Good morning
         2: putstatic     #3                  // Field msg:Ljava/lang/String;
         5: return
      LineNumberTable:
        line 4: 0
}
SourceFile: "Hello.java"
InnerClasses:
  public static final #52= #51 of #55;    // Lookup=class java/lang/invoke/MethodHandles$Lookup of class java/lang/invoke/MethodHandles
BootstrapMethods:
  0: #30 REF_invokeStatic java/lang/invoke/StringConcatFactory.makeConcatWithConstants:(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/invoke/CallSite;
    Method arguments:
      #31 msg == \u0001

常量池中存储两类常量:字面量和符号引用。
字面量:就是代码中声明为Final的常量值,
符号引用:如类和接口的全局限定名,字段的名称和描述符,方法的名称和描述符,


image.png

常量计数器:由于常量的数量不固定,所以需要防止两个字节俩存储常量池的数量值。


image.png

你可能感兴趣的:(2-6class字节码:常量池)