In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how to use field table and method table of Class file structure". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use field tables and method tables in Class file structure.
1. Field table
The field table immediately follows the interface table index, and the field table contains an access tag, a field name index, a descriptor index, and a property table, where the property table contains property counters and property collections.
Take this code as an example:
Package com.yang.testField; public class Main {private volatile int a = 1; public static final String b = "abc";}
The hexadecimal data is shown below:
As you can see, the field count is 0x0002 because there are two fields, an and b.
The access tag for field an is 0x0042, which is matched by the eigenvalue of the identifier. If the result is 1, the field has the corresponding identifier. The field identifier is as follows:
Here we can see that the access tags of an are ACC_PRIVATE and ACC_VOLATILE.
The name index of an is 0x0005. Let's take a look at the constant pool:
You can conclude that the name index of the first field points to the fifth constant item in the constant pool, "a".
The descriptor index of an is 0x0006, that is, the "I" in the constant pool. The comparison table between the completed field type and the descriptor is as follows:
Next is the property counter for a, which corresponds to a value of 0x0000, which means that a has no property sheet.
Post the property sheet in the b field table:
The attribute counter of b is 0x0001, which means that there is a property sheet. There is only one element in the property sheet, which is 0x0009, and the constant pool is displayed as ConstantValue, indicating that
The attribute is of type ConstantValue, the attribute length is 2, and the attribute value index is 0x000A, that is, find # 11 in the constant pool, and then find # 21, which turns out to be the string "abc".
Why does int a have a property sheet while static final b has a property sheet? This starts with the assignment strategy of the field:
For an instance field, such as a here, the assignment phase occurs in the constructor of the object instance, that is,
For a non-final static field, the assignment of the initial value occurs in the parsing phase, while the user-specified value occurs in the initialization phase, which is done in the class constructor method, that is.
For a static field of final, which is either a primitive type or a String type, the variable is assigned a user-specified value during compilation, and an attribute of type ConstantValue is formed in the constant pool, and the property value is the constant value. If it is a reference type other than the String type, the assignment is done during the initialization phase.
Here is an example:
Package com.yang.testField; public class Main {private volatile int a = 1; public static final String b = "abc"; public static String c = "def"; public static Thread d=new Thread ();}
The situation within the method:
What is done here is the assignment of instance variables.
The situation within the method:
What is done here is the assignment of the ordinary static variable c and the non-String reference type variable d.
For more information on the understanding of and methods, please refer to the in-depth understanding of clinit and init in the execution sequence of java in this article.
2. Method table
The method table is followed by the method table, which is similar to the field table. The method table contains method count, access tag, name index, descriptor index, and property table. The property table also contains attribute count and property collection.
Method counting and name indexing are no longer described here.
The access tags for the method are:
Here's a simple example:
Package com.yang.testMethod; public class Main {public Main () {} private int getInt (int k) {return k;} public static Thread getThread (int I, double d, Runnable runnable) {System.out.println (I * d); return new Thread (runnable);}}
The descriptor of the construction method is () V
The descriptor of the getInt method is (I) I
The descriptor of the getThread method is (IDLjava/lang/Runnable;) Ljava/lang/Thread
From this, we can see that the method descriptor is organized like this: (the descriptor of the field in the parameter list) the descriptor of the return value
Next, we discuss the property sheet of the method. As mentioned earlier, the property sheet contains the attribute count and the attribute collection, which in turn contains the attribute name index + attribute length + attribute value.
The most important attribute in the property sheet is the Code attribute. There are several important things in the Code attribute: bytecode, LineNumberTable line number table, LocalVariableTable local variable table, ExceptionTable exception table.
Take the following code as an example:
Public static Thread getThread (int I, double d, Runnable runnable) {try {System.out.println (I * d);} catch (Exception e) {return null;} return new Thread (runnable);}
Bytecode is the most important thing in class files. Jvm mainly extracts bytecode and executes it.
The correspondence between java source code and bytecode is maintained in LineNumberTable:
Local variable descriptions are recorded in LocalVariableTable:
For the details of the local variable scale, you can refer to my other article, the internal organs of the virtual machine stack.
ExceptionTable will tell the virtual machine the exception handling logic, such as the exception table in the following figure, indicating that if an exception of type type occurs in the bytecode from lines 0 to 10, it will jump to the bytecode on line 13 for processing.
At this point, I believe that everyone on the "Class file structure of how to use the field table and method table" have a deeper understanding, might as well to practical operation it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.