当前位置:网站首页>Field table collection of JVM virtual machine class file

Field table collection of JVM virtual machine class file

2020-11-13 11:35:47 Kill one in ten steps_ Never leave a journey for thousands of m

One 、 summary

After the interface index set Is the field table set . Field table (field_info) Used to describe variables declared in an interface or class . Fields include class level variables and instance level variables , But local variables declared inside methods are not included .

Two 、 The composition of the field table set

1. Field counter

The field table set is made up of many field_info Composed of , So the first two bytes of the field table set indicate how many fields there are , Two bytes ,16 position .

2.field_info

Every field_info The structure of is shown in the following table :

type      name Number
u2 access_flags 1
u2 name_index 1
u2 descriptor_index 1
u2 attributes_count 1
attribute_info attributes attributes_count

3.access_flags

access_flags Field modifiers , Also called field access flag .access_flag Occupy 2 Bytes ,16 position , It's related to access_flags The project is very similar , It includes the following types :

Logo name Flag value meaning
ACC_PUBLIC 0x00 01 Is the field public
ACC_PRIVATE 0x00 02 Is the field private
ACC_PROTECTED 0x00 04 Is the field protected
ACC_STATIC 0x00 08 Is the field static
ACC_FINAL 0x00 10 Is the field final
ACC_VOLATILE 0x00 40 Is the field volatile
ACC_TRANSTENT 0x00 80 Is the field transient
ACC_SYNCHETIC 0x10 00 Is the field generated automatically by the compiler
ACC_ENUM 0x40 00 Is the field enum

What flags does a field have , Is to use the access ID to get | operation :

Such as 0x0001|0x0008 by 0x0009.

4.name_index and descriptor_index

name_index and descriptor_index, Is a reference to the constant pool ,name_index The simple name of the field ,descriptor_index Refers to the field descriptor .

descriptor_index Descriptors are used to describe the data types of fields , The basic data type is represented by an uppercase character , The object type is character plus L Add the fully qualified name of the object name to indicate .

identifier meaning
B Basic data type byte
C Basic data type char
D Basic data type double
F Basic data type float
I Basic data type int
J Basic data type long
S Basic data type short
Z Basic data type boolean
V Basic data type void
L object type

To array type , One... Will be used for each dimension "[" Character to describe . Like a Stirng[][] Two dimensional array of type , Will be recorded as :[[Ljava/lang/Stirng, An integer array "int []" Will be recorded as "[I".

3、 ... and 、 Example disassembly

Define a class :

public class HelloWord{
    
    public int i;
    private Long l;
    protected String[][] arrs;
    public Object test(){return null;}
}

 

javap -verbose HelloWord.class

public class com.csdn.reader.HelloWord
  minor version: 0
  major version: 52
  flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
   #1 = Methodref          #3.#21         // java/lang/Object."<init>":()V
   #2 = Class              #22            // com/csdn/reader/HelloWord
   #3 = Class              #23            // java/lang/Object
   #4 = Utf8               i
   #5 = Utf8               I
   #6 = Utf8               l
   #7 = Utf8               Ljava/lang/Long;
   #8 = Utf8               arrs
   #9 = Utf8               [[Ljava/lang/String;
  #10 = Utf8               <init>
  #11 = Utf8               ()V
  #12 = Utf8               Code
  #13 = Utf8               LineNumberTable
  #14 = Utf8               LocalVariableTable
  #15 = Utf8               this
  #16 = Utf8               Lcom/csdn/reader/HelloWord;
  #17 = Utf8               test
  #18 = Utf8               ()Ljava/lang/Object;
  #19 = Utf8               SourceFile
  #20 = Utf8               HelloWord.java
  #21 = NameAndType        #10:#11        // "<init>":()V
  #22 = Utf8               com/csdn/reader/HelloWord
  #23 = Utf8               java/lang/Object
{
  public int i;
    descriptor: I
    flags: ACC_PUBLIC

  protected java.lang.String[][] arrs;
    descriptor: [[Ljava/lang/String;
    flags: ACC_PROTECTED

  public com.csdn.reader.HelloWord();
    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 3: 0
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0       5     0  this   Lcom/csdn/reader/HelloWord;

  public java.lang.Object test();
    descriptor: ()Ljava/lang/Object;
    flags: ACC_PUBLIC
    Code:
      stack=1, locals=1, args_size=1
         0: aconst_null
         1: areturn
      LineNumberTable:
        line 8: 0
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0       2     0  this   Lcom/csdn/reader/HelloWord;
}

Four 、 summary

Fields inherited from the parent class or parent interface will not be listed in the field table collection , But it's possible to list the original Java Fields and methods that do not exist in the code , For example, in order to maintain the accessibility of external classes in internal classes , Fields that point to external class instances are automatically added .

in addition , stay Java Fields in a language cannot be overloaded , The data types of the two fields , The modifier is the same or not , Must use a different name , But for bytecode , If the descriptors of successive fields are inconsistent , The duplicate name of that field is legal .

Reference material :

  1. https://www.cnblogs.com/lrh-xl/p/5350612.html
  2. https://blog.csdn.net/xiaoqiu_cr/article/details/86726474

版权声明
本文为[Kill one in ten steps_ Never leave a journey for thousands of m]所创,转载请带上原文链接,感谢

随机推荐