1. Class files

1.0 Characters and bytes

**ASCII code: ** One letter (case insensitive) occupies one byte of space. A sequence of binary digits, usually 8 bits, used as a numeric unit in a computer. In decimal notation, the minimum value is -128 and the maximum value is 127. An ASCII code, for example, is a byte.

**UTF-8 encoding: ** One English character is equal to one byte, and one Chinese (including traditional Chinese) is equal to three bytes. Chinese punctuation is three bytes and English punctuation is one byte

**Unicode encoding: ** One English is equal to two bytes, and one Chinese (including traditional) is equal to two bytes. Chinese punctuation takes two bytes and English punctuation takes two bytes

1.1 Introduction and development history of Class files

The specification for class changed very little from version 1 to version 8.

1.2 Class File Structure Overview

1.2.1 overview

  • CA FE BA BE: character C is hexadecimal (4 bits,0 to 15),CA(2 4 bits,8 bits,1 byte). Is unsigned.
  • The Class file is a group of binary streams based on 8-bit bytes. Each data item is arranged in the Class file in strict order and compact, without adding any delimiter in the middle. The content stored in the Class file is almost all the necessary data for the operation of the program, and there is no gap.
  • When encountering data items with more than 8-bit space, it will be divided into several 8-bit bytes for storage according to the way with the highest order.
  • There are two types of data in the Class file, unsigned numbers (roughly speaking the underlying data types) and tables (roughly speaking the reference data types, which can contain both unsigned numbers and tables).

Advantages: Save storage space and improve program performance

1.2.2 Class File Structure

  • The magic number
  • Class file version
  • Constant pool
  • Access tokens
  • Class index, superclass index, interface index collection
  • Set of field tables
  • Method table collection
  • Property sheet collection

Class File format

type descriptor[number] remark
u4 magic[1] 0xCAFEBABE
u2 minor_version[1]
u2 major_version[1]
u2 constant_pool_count[1]
cp_info Constant_pool [1] [cosntant_pool_count – 1) index 0 is invalid
u2 access_flags[1]
u2 this_class[1]
u2 super_class[1]
u2 interfaces_count[1]
u2 interfaces[interfaces_count]
u2 fields_count[1]
field_info fields[fields_count]
u2 methods_count[1]
method_info methods[methods_count]
u2 attributes_count[1]
attribute_info attributes[attributes_count]

One U represents one byte (two characters, eight bits). U4 represents four bytes

1.3 Class document design concept and significance

Syntax -> compiler -> bytecode ->JVM implements syntax and compilers: Clojure, Groovy, Jruby, Jython, Scala(languages that run on JVM)

2 File Structure

Public class HelloWorld {public static void main(String []args) {system.out.println ("Hello World..") ); }}Copy the code

Links to native class files for compiled class interpretation; Image links

View class files through ue:

Hexadecimal digit: Each hexadecimal digit contains four bits, so two hexadecimal characters containing eight bits are a Byte.

2.1 File Structure – Magic number, version number (magic,minor_version,major_version)

type descriptor[number] remark
u4 magic[1] 0xCAFEBABE
u2 minor_version[1]
u2 major_version[1]

CAFE BABE 4 bytes start with 1 row 0 columns,1 row 1 column,1 row 2 columns,1 row 3 columns; Then the version number: takes up 4 bytes: the first 2 bytes (5 and 6 bits) are the minor version number, and the last 2 bytes are the major version number (7 and 8 bits).

JDK 1.8 = 52 JDK 1.7 = 51 JDK 1.6 = 50 JDK 1.5 = 49 JDK 1.4 = 48 JDK 1.3 = 47 JDK 1.2 = 46 JDK 1.1 = 45

In helloworld.class,1 row 4 columns,1 row 5 columns,1 row 6 columns,1 row 7 columns are 00 00 00 34– decimal –>0,52, i.e. JDK1.8

2.2 File Structure – Constant Pool (constant_pool_count,constant_pool)

type descriptor[number] remark
u2 constant_pool_count[1]
cp_info Constant_pool [cosntant_pool_count – 1) index 0 is invalid

Reference -Class_ Constant pool reference article

See -class file for constant pool details

See -class file for constant pool details

Java bytecode (.class file) format:

2.2.1 Constant Pool Type/Meaning List

Each constant in the constant pool is a table (14 types of tables)

type mark describe
There is no 0 Indicates that no constant is referenced
CONSTANT_utf8_info 1 The character string is utF-8 encoded
CONSTANT_Integer_info 3 Integer literal
CONSTANT_Float_info 4 Floating point literals
CONSTANT_Long_info 5 Long integer literals
CONSTANT_Double_info 6 A double – precision floating-point literal
CONSTANT_Class_info 7 Symbolic reference to a class or interface
CONSTANT_String_info 8 String type literals
CONSTANT_Fieldref_info 9 Symbolic reference to a field
CONSTANT_Methodref_info 10 Symbolic references to methods in a class
CONSTANT_InterfaceMethodref_info 11 Symbolic references to methods in the interface
CONSTANT_NameAndType_info 12 Symbolic reference to a field or method
__ 15 Represents a method handle
CONSTANT_MethodType_info 16 Flag method type
CONSTANT_InvokeDynamic_info 18 Represents a dynamic method call point

Format template ==cp_info format(length)

type descriptor remark
u1 tag Number of types
u1 info[] The set of tables

1== constant string values. String content is encoded in modified UTF-8.

type descriptor remark
u1 tag CONSTANT_Utf8 (1)
u2 length The length of the string represented by bytes
u1 bytes[length] Byte data for a string, which can be read by the readUtf() method in DataInputStream (instance method or static method to read the value of the binary string).

3==CONSTANT_Integer_info represents 4-byte numeric (int) constants 🙂

type descriptor remark
u1 tag CONSTANT_Integer (3)
u4 bytes Integer constant value

4==CONSTANT_Float_info used to record float constant values (represent 4-byte numeric (float) constants 🙂

type descriptor remark
u1 tag CONSTANT_Float(4)
u4 bytes Single-precision floating-point constant value

5==CONSTANT_Long_info used to record constant values of type long (represent 8-byte numeric (long) constants 🙂

type descriptor remark
u1 tag CONSTANT_Long (5)
u4 high_bytes The high four-digit value of a long integer
u4 low_bytes The lowest four-digit value of a long integer

6==CONSTANT_Double_info Used to record constant values of type double (Represent 8-byte numeric (double) constants:

type descriptor remark
u1 tag CONSTANT_Double(6)
u4 high_bytes The high four-digit value of a double – precision floating-point
u4 low_bytes The low four – digit value of a double – precision floating-point

7==CONSTANT_Class_info format Used to represent a class or an interface

type descriptor remark
u1 tag CONSTANT_Class (7)
u2 name_index Constant_pool index of type CONSTANT_Utf8_info. Represents the name of a class or interface.

8==CONSTANT_String_info Represents constant objects of the type String

type descriptor remark
u1 tag CONSTANT_String(8)
u2 string_index Constant_pool index of type CONSTANT_Utf8_info. Represents a String value.

9==CONSTANT_Fieldref_info Used to record field information (including fields defined in a class or interface as well as fields used in code)

type descriptor remark
u1 tag CONSTANT_Fieldref(9)
u2 class_index Constant_pool index of type CONSTANT_Class_info. Records the class or interface that defines the field.
u2 name_and_type_index Constant_pool index of type CONSTANT_NameAndType_info. Specifies the field name and field descriptor in the class or interface.

10==CONSTANT_Methodref_info Is used to record method information (including methods defined in the class and those used in the code)

type descriptor remark
u1 tag CONSTANT_Methodref(10)
u2 class_index Constant_pool index of type CONSTANT_Class_info. Record the class that defines the method.
u2 name_and_type_index Constant_pool index of type CONSTANT_NameAndType_info. Specify a method name and method descriptor in a way in which the class is passed.

11==CONSTANT_InterfaceMethodref_info Records method information in the interface (including methods defined in the interface and methods used in the code).

type descriptor remark
u1 tag CONSTANT_InterfaceMethodref(11)
u2 class_index Constant_pool index of type CONSTANT_Class_info. Record the interface that defines the method.
u2 name_and_type_index Constant_pool index of type CONSTANT_NameAndType_info. Specifies the method name and method descriptor in the interface.

12==CONSTANT_NameAndType_info Record method or field name and descriptor (represent a field or method, Where did I send it to the world?

type descriptor remark
u1 tag CONSTANT_NameAndType (12)
u2 name_index Constant_pool index of type CONSTANT_Utf8_info. Specifies the name of a field or method.
u2 descriptor_index Constant_pool index of type CONSTANT_utf8_info. Specify field or method descriptors (see Appendix C)

16==CONSTANT_MethodType_info Temporarily unavailable 18==CONSTANT_InvokeDynamic_info temporarily unavailable

2.2.3 interpretation

Constant pool length (constant_pool_count) accounted for U2 (2 bytes): helloworld.class file 1 row 8 columns,1 row 9 columns is 00 22; 00 22– Decimal –>34 (groups) constants.

The first set of constants, line a, is 0A– decimal –>10(10 refers to 10=CONSTANT_Methodref_info, and 0A is the first constant pool (constant pool number 1)), Now look at 10 = CONSTANT_Methodref_info structure specification of 2.2.1 10 [u1 | tag, u2 | classindex, u2 | name_and_type_index]. 00 06 is the classindex of CONSTANT_Methodref_info,00 06– decimal –>6(pointing to the sixth constant pool location). 00 14 is the name_and_type_index of CONSTANT_Methodref_info,00 14– decimal –>20(pointing to the 20th constant pool location)

The second set of constants 1 row f column is 09– decimal –>9(9 stands for 9=CONSTANT_Fieldref_info, and 09 is the second constant pool); Now look at 9 = CONSTANT_Fieldref_info specification structure [u1 | tag, u2 | class_index, u2 | name_and_type_index]. 00 15 is class_index,00 15– decimal –>21(pointing to position 21), The last two bytes are 2 rows,2 columns,2 rows, 3 columns, i.e. 00 16 name_AND_type_index,00 16– decimal –>22(pointing to position 22),

A third group of constants with 2 rows and 4 columns is 08– decimal –>8(8=CONSTANT_String_info, and 08 is the third constant pool); Now see = = 8 CONSTANT_String_info specification structure [u1 | tag, u2 | string_index], that is 2 bytes after 2 line 5 column, 2 row 6 columns is 00 17 for string_index decimal — — – > 23 pointed out (23)

The fourth group of constants, 2 rows and 7 columns, is 0A– decimal –>10(10=CONSTANT_Methodref_info, and 0A is the fourth constant pool); Now look at 10 = CONSTANT_Methodref_info structure specification [u1 | tag, u2 | classindex, u2 | name_and_type_index]. 00 18 is classIndex,00 18– decimal –>24(pointing to the 24th constant pool position). 00 19 name_and_type_index,00 19– decimal –>25(pointing to the 25th constant pool location)

The fifth group of constants, 2 rows in c column, is 07– decimal –>7(7=CONSTANT_Class_info format, and 07 is the fifth constant pool); Now look at 7 = = CONSTANT_Class_info format specification structure [u1 | tag, u2 | name_index], that is after two characters line 2 d column, 2 row e column 1 a is name_index 00; 00 1A– Decimal –>26(pointing to the 26th constant pool position)

The sixth group of constants, 2 rows inf column, is 07– decimal –>7(7=CONSTANT_Class_info format, and 07 is the sixth constant pool); Now look at 7 = = CONSTANT_Class_info format specification structure [u1 | tag, u2 | name_index], that is two characters after 3 row 0, column, 3 row 1 column 1 b for name_index namely 00; 00 1B– Decimal –>27(pointing to the 27th constant pool position)

The seventh group of constants with 3 rows and 2 columns is 01– decimal –>1(1=CONSTANT_Utf8_info, and 01 is the seventh constant pool); At this point to see 1 = = CONSTANT_Utf8_info structure specification [u1 | tag, u2 | length, u1 | bytes [length]], that is after 2 column 3 rows 3 columns, 3 line 4 column 0 06 for length, namely 00 06 – decimal – > 6, 3C 69, 6E 74, 3E is lbytes[length], 3C 69 6E 74 3E– Decimal system –>60 89 105 110 116 62 –>… .

You can read backwards until you have 34 constants.

#1 points to #6(Java /lang/Object),#20(#7(),#8(()V)), and #6,#20.

javap -verbose HelloWorld.class Classfile /D:/workspace/***/HelloWorld.class Last modified 2017-12-27; size 536 bytes MD5 checksum a77b7f0d7e173b1c9b6040311535c45d Compiled from "HelloWorld.java" public class HelloWorld minor version: 0 major version: 52 flags: ACC_PUBLIC, ACC_SUPER Constant pool: #1 = Methodref #6.#20 // java/lang/Object."<init>":()V #2 = Fieldref #21.#22 // java/lang/System.out:Ljava/io/Prin Stream; #3 = String #23 // Hello World .. #4 = Methodref #24.#25 // java/io/PrintStream.println:(Ljava lang/String;) V #5 = Class #26 // HelloWorld #6 = Class #27 // java/lang/Object #7 = Utf8 <init> #8 = Utf8 ()V #9 = Utf8 Code #10 = Utf8 LineNumberTable #11 = Utf8 LocalVariableTable #12 = Utf8 this #13 = Utf8 LHelloWorld; #14 = Utf8 main #15 = Utf8 ([Ljava/lang/String;)V #16 = Utf8 args #17 = Utf8 [Ljava/lang/String; #18 = Utf8 SourceFile #19 = Utf8 HelloWorld.java #20 = NameAndType #7:#8 // "<init>":()V #21 = Class #28 // java/lang/System #22 = NameAndType  #29:#30 // out:Ljava/io/PrintStream; #23 = Utf8 Hello World .. #24 = Class #31 // java/io/PrintStream #25 = NameAndType  #32:#33 // println:(Ljava/lang/String;)V #26 = Utf8 HelloWorld #27 = Utf8 java/lang/Object #28 = Utf8 java/lang/System #29 = Utf8 out #30 = Utf8 Ljava/io/PrintStream; #31 = Utf8 java/io/PrintStream #32 = Utf8 println #33 = Utf8 (Ljava/lang/String;)V { public HelloWorld(); 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 LHelloWorld; public static void main(java.lang.String[]); descriptor: ([Ljava/lang/String;)V flags: ACC_PUBLIC, ACC_STATIC Code: stack=2, locals=1, args_size=1 0: getstatic #2 // Field java/lang/System.out:Lja a/io/PrintStream; 3: ldc #3 // String Hello World .. 5: invokevirtual #4 // Method java/io/PrintStream.pri tln:(Ljava/lang/String;)V 8: return LineNumberTable: line 3: 0 line 4: 8 LocalVariableTable: Start Length Slot Name Signature 0 9 0 args [Ljava/lang/String; } SourceFile: "HelloWorld.java"Copy the code

2.3 File Structure – Access flags

type descriptor[number] remark
u2 access_flags[1]

2.3.1 Overview of Access flags

After the constant pool is the access flag.

U2 | access_flags, 2 bytes (16 bits). Access_flags:

Sign the name Flag values meaning Representation in code
ACC_PUBLIC 0x00 01 Whether the type is Public public
ACC_FINAL 0x00 10 Only the class can set whether or not to be declared final final
ACC_SUPER 0x00 20 Whether the new semantics of the Invokespecial bytecode instruction are allowed. extends
ACC_INTERFACE 0x02 00 Flag this is an interface interface
ACC_ABSTRACT 0x04 00 Whether it is of the abstract type. For interfaces or abstract classes, the second flag value is true and the other types are false abstract
ACC_SYNTHETIC 0x10 00 Indicates that this class is not generated by user code All types of
ACC_ANNOTATION 0x20 00 This is a note annotation
ACC_ENUM 0x40 00 Flag This is an enumeration enum
ACC_PUBLIC (public), ACC_PRIVATE (private), ACC_PROTECTED (protected), ACC_FINAL (final), ACC_SUPER (extends), ACC_INTERFACE, A CC_ABSTRACT (abstract class), ACC_ANNOTATION (annotation type), ACC_ENUM (enum type), ACC_DEPRECATED (class marked with @deprecated annotations), ACC_SYNTHETICCopy the code



Note: the value of ACC_INTERFACE in the picture is incorrect, the table above should prevail. ACC_INTERFACE=0x0400.

2.3.2 interpretation

2.3.2.1 unscramble the HelloWorld class

Last constant: #33 = Utf8 (Ljava/lang/String;) PNG: helloworld-class-binary. PNG: helloworld-class-binary. PNG: helloworld-class-binary. PNG It can be seen from the picture that the access flag starts from row 0 of 180h. Row 0 of 180h and column 1 of 180h, i.e. 00 21, compare with the combination table in 2.3.1. For table 00 21= 0x0001&0x0020,(acc_super = acc_public, acc_super = 1, acc_super =0, acc_super = 1) The value indicates public Class. You can also check the flags: ACC_PUBLIC, ACC_SUPER ** in 2.2.3 Displaying javap-verbose

2.3.2.2 interpretation HelloWorldInterface. Class

Public interface HelloWorldInterface {}Copy the code

The compiled class HelloWorldInterface. Class

HelloWorldInterface. Class javap – verbose results:

javap -verbose D:\workspace\HelloWorldInterface.class Classfile /D:/workspace/HelloWorldInterface.class Last modified The 2017-12-30; size 119 bytes MD5 checksum e8d0ce8260a978e105c60e9852245e1a Compiled from "HelloWorldInterface.java" public interface HelloWorldInterface minor version: 0 major version: 52 flags: ACC_PUBLIC, ACC_INTERFACE, ACC_ABSTRACT Constant pool: #1 = Class #5 // HelloWorldInterface #2 = Class #6 // java/lang/Object #3 = Utf8 SourceFile #4 = Utf8 HelloWorldInterface.java #5 = Utf8 HelloWorldInterface #6 = Utf8 java/lang/Object { } SourceFile: "HelloWorldInterface.java"Copy the code

060H row 1 column 060H row 2 column 060H row 2 column 06 01 = 0x04/0x02/0x00 ===>ACC_PUBLIC ACC_ABSTRACT ACC_INTERFACE , you can also check the flags: ACC_PUBLIC, ACC_INTERFACE, ACC_ABSTRACT in the javap result to get the same result.

2.2.3 Reference article

JVM – class files completely parsing – access tokens (concise) key interface ClassVisitor] [into bytecode – ASM my.oschina.net/u/1166271/b… (Principle basis)

1.3 access flags, class index, parent index, interface index collection in the class file

2.4 File Structure – Class index (this_class,super_class,interfaces_count)

type descriptor[number] remark
u2 this_class[1]
u2 super_class[1]
u2 interfaces_count
u2 interfaces[interfaces_count]
# # # against 2.4.1 overview
# # # read 2.4.2
Then interpret HelloWorldInterface. Class
Row 060h has 3 columns and row 060h has 4 columnsIf 0 01 is this_class, 00 01– decimal –>1, which refers to the first position in the constant pool, look at javap 2.3.2.2, see #1 = Class, #5 // HelloWorldInterface

060h row 5 columns,060h row 6 columns 00 02 is super_class, 00 02– decimal –>2, indicating the second position in the constant pool. #6 // Java /lang/Object

060h row 7 columns,060h row 8 columns,00 00 is interfaces_count, the number of interfaces,00 00– decimal –>0, indicating that no interface is implemented,

2.4.3 interpretation HelloExtendImp. Class

2.4.3.1 Preparing Information

// source code package classstruct; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.FileInputStream; import java.io.FileNotFoundException; public class HelloExtendImp extends FileInputStream implements Runnable,ActionListener{ public HelloExtendImp(String name) throws FileNotFoundException { super(name); } @Override public void actionPerformed(ActionEvent e) { } @Override public void run() { } }Copy the code

HelloExtendImp.class

The binary view is as follows:

D:\IdeaProjects\out\production\jvmStu\classstruct>javap -verb loExtendImp.class Classfile /D:/IdeaProjects/out/production/jvmStu/classstruct/ tendImp.class Last modified 2017-12-31; size 703 bytes MD5 checksum b0aba17a3e3cf2b32f0ad93f5d6e9b96 Compiled from "HelloExtendImp.java" public class classstruct.HelloExtendImp extends java.io.FileInputStream i ts java.lang.Runnable,java.awt.event.ActionListener minor version: 0 major version: 52 flags: ACC_PUBLIC, ACC_SUPER Constant pool: #1 = Methodref #3.#25 // java/io/FileInputStream."<in java/lang/String;) V #2 = Class #26 // classstruct/HelloExtendImp #3 = Class #27 // java/io/FileInputStream #4 = Class #28 // java/lang/Runnable #5 = Class #29 // java/awt/event/ActionListene #6 = Utf8 <init> #7 = Utf8 (Ljava/lang/String;) V #8 = Utf8 Code #9 = Utf8 LineNumberTable #10 = Utf8 LocalVariableTable #11 = Utf8 this #12 = Utf8 Lclassstruct/HelloExtendImp; #13 = Utf8 name #14 = Utf8 Ljava/lang/String; #15 = Utf8 Exceptions #16 = Class #30 // java/io/FileNotFoundExceptio #17 = Utf8 actionPerformed #18 = Utf8 (Ljava/awt/event/ActionEvent;) V #19 = Utf8 e #20 = Utf8 Ljava/awt/event/ActionEvent; #21 = Utf8 run #22 = Utf8 ()V #23 = Utf8 SourceFile #24 = Utf8 HelloExtendImp.java #25 = NameAndType #6:#7 // "<init>":(Ljava/lang/String; #26 = Utf8 classstruct/HelloExtendImp #27 = Utf8 java/io/FileInputStream #28 = Utf8 java/lang/Runnable #29 = Utf8 java/awt/event/ActionListener #30 = Utf8 java/io/FileNotFoundException { public classstruct.HelloExtendImp(java.lang.String) throws java.io.File dException; descriptor: (Ljava/lang/String;) V flags: ACC_PUBLIC Code: stack=2, locals=2, args_size=2 0: aload_0 1: aload_1 2: invokespecial #1 // Method java/io/FileInput "<init>":(Ljava/lang/String;) V 5: return LineNumberTable: line 13: 0 line 14: 5 LocalVariableTable: Start Length Slot Name Signature 0 6 0 this Lclassstruct/HelloExtendImp; 0 6 1 name Ljava/lang/String; Exceptions: throws java.io.FileNotFoundException public void actionPerformed(java.awt.event.ActionEvent); descriptor: (Ljava/awt/event/ActionEvent;) V flags: ACC_PUBLIC Code: stack=0, locals=2, args_size=2 0: return LineNumberTable: line 19: 0 LocalVariableTable: Start Length Slot Name Signature 0 1 0 this Lclassstruct/HelloExtendImp; 0 1 1 e Ljava/awt/event/ActionEvent; public void run(); descriptor: ()V flags: ACC_PUBLIC Code: stack=0, locals=1, args_size=1 0: return LineNumberTable: line 24: 0 LocalVariableTable: Start Length Slot Name Signature 0 1 0 this Lclassstruct/HelloExtendImp; } SourceFile: "HelloExtendImp.java"Copy the code

2.4.2.2 interpretation

By javap view the results: # 30 = Utf8 Java/IO/FileNotFoundException (1 d0h row 0, column, 1 d0h row 2 column) for the constant pool finally over.

00 21= 0x0001&0x0020 ==>flags: ACC_PUBLIC, ACC_SUPER. 1d0h row 5 columns,1d0h row 6 columns = 00 02 to this_class to a constant pool of # 02, view javap results is the class (classstruct/HelloExtendImpclassstruct/HelloExtendImp). 1 d0h line 7, 1 d0h row 8 column is 00 1d0h row 9 columns,1d0h row a column 00, 02 interfaces_count, 2 interfaces. 1d0h row D column,1d0h row E column 00 1d0h row D column,1d0h row E column 00 05 for interface2 to a constant pool of # 5, view javap results is the interface 2 (Java/awt/event/ActionListene).

2.5 File Structure – Collection of field_info tables

type descriptor[number] remark
u2 fields_count[1]
field_info fields[fields_count]

Jvm-class file fully parsed – collection of field tables

2.5.1 overview

The field table (field_info) is used to describe variables declared in an interface or class. Fields include class-level variables as well as instance-level variables, but do not include local variables declared inside methods.

2.5.1.1 Structure of the field (variable) table

field_info:

type The name of the The number of
u2 access_flags 1
u2 name_index 1
u2 descriptor_index 1
u2 attributes_count 1
attribute_info attributes attributes_count

2.5.1.2 Field access flag

The field modifier is placed in the access_flags project, which is very similar to the access_flags project in the class, and is a U2 data type.

Sign the name Flag values meaning
ACC_PUBLIC 0x00 01 Whether the field is public
ACC_PRIVATE 0x00 02 Whether the field is private
ACC_PROTECTED 0x00 04 Whether the field is protected
ACC_STATIC 0x00 08 Whether the field is static
ACC_FINAL 0x00 10 Whether the field is final
ACC_VOLATILE 0x00 40 Whether the field is volatile
ACC_TRANSTENT 0x00 80 Whether the field is TRANSIENT
ACC_SYNCHETIC 0x10 00 Whether the field is generated automatically by the compiler
ACC_ENUM 0x40 00 Whether the field is enum
Following the access_flags flag are two index values, name_index and Descriptor_index, which are references to the constant pool and represent the simple name of the field and the field method and method descriptor, respectively.
 
 
 
 
#### 2.5.1.3 Description of the descriptor
Descriptors are used to describe the data type of a field, the parameter list of a method (including the number, type, and order), and the return value. According to the descriptor rules, basic data types and void types representing no returned values are represented by an uppercase character, while object types are represented by fully qualified names of characters plus L plus object names.
identifier meaning
B Basic data type Byte
C Char, the base data type
D The base data type double
F The basic data type float
I Base data type int
J Base data type long
S The basic data type short
Z Boolean is the basic data type
V The base data type void
L Object type

For array types, each dimension will be described by a leading “[” character. For example, a two-dimensional array defined as type” java.lang.stirng [] “will be recorded as:” [[Ljava/lang/Stirng “, and an integer array “int []” will be recorded as “[I”.

Methods are described by descriptors in the order of the argument list followed by the return value, which is enclosed in a set of parentheses “()” in strict order.

The collection of field tables does not list fields inherited from a parent class or interface, but it may list fields that do not exist in the original Java code, such as fields that point to an external class instance that are automatically added to an internal class in order to maintain access to external classes. In addition, fields cannot be overloaded in the Java language. The data types and modifiers of two fields must have different names, regardless of whether they are the same or not. But for bytecodes, if the descriptors of consecutive fields are inconsistent, the same name is valid.

2.5.2 interpretation

2.5.2.1 Information to be interpreted

// source code package classstruct; public class HelloWorldField { private int a ; public byte b; public static Object obj; protected Object [] ojbs; }Copy the code

HelloWorldField.class

The binary view is as follows:

D:\workspace\javap -verbose HelloWorldField.class Classfile /D:/workspace/HelloWorldField.class Last modified The 2017-12-31; size 398 bytes MD5 checksum 8efd9fc928289285f5f89ce3c9f101d3 Compiled from "HelloWorldField.java" public class classstruct.HelloWorldField 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 // classstruct/HelloWorldField #3 = Class #23 // java/lang/Object #4 = Utf8 a #5 = Utf8 I #6 = Utf8 b #7 = Utf8 B #8 = Utf8 obj #9 = Utf8 Ljava/lang/Object; #10 = Utf8 ojbs #11 = Utf8 [Ljava/lang/Object; #12 = Utf8 <init> #13 = Utf8 ()V #14 = Utf8 Code #15 = Utf8 LineNumberTable #16 = Utf8 LocalVariableTable #17 = Utf8 this #18 = Utf8 Lclassstruct/HelloWorldField; #19 = Utf8 SourceFile #20 = Utf8 HelloWorldField.java #21 = NameAndType #12:#13 // "<init>":()V #22 = Utf8 classstruct/HelloWorldField #23 = Utf8 java/lang/Object { public byte b; descriptor: B flags: ACC_PUBLIC public static java.lang.Object obj; descriptor: Ljava/lang/Object; flags: ACC_PUBLIC, ACC_STATIC protected java.lang.Object[] ojbs; descriptor: [Ljava/lang/Object; flags: ACC_PROTECTED public classstruct.HelloWorldField(); 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 Lclassstruct/HelloWorldField; } SourceFile: "HelloWorldField.java"Copy the code

2.5.2.2 interpretation 2.5.2.1

00 21= 0x0001&0x0020 ==>flags: This_class = this_class; this_class = 0; this_class = 0 03 is super_class, which refers to the constant pool #03 120h row 1 column,120h row 2 columns i.e. 00 is interfaces_count, and the number of interfaces is 0. The number of interfaces will be 0, so there will be no interfaces, and then there will be fields_count, i.e. 120h row 3 columns,120h row 4 columns 00, 04 is 4, indicating that there are 4 field_info fields. First field_info: structure reference of field_info (2.5.1.1 structure of field table): 120 h line five columns, column namely 00 02 120 h line 6 to u2 | access_flags, instructions for private type (according to 2.5.1.2 field access). The 120 h line 7, 120 h row 8 column is 00 04 for u2 | name_index1, variable names to # 4 in the constant pool, (# 4 = Utf8 a) 120 h line 9, 120 h column a is 00 05 for u2 | descriptor_index, pointing to the constant pool of # 5 (# 5 = Utf8 I), I represent the basic types of int (2.5.1.3 descriptor symbol meaning). 120 h b column, line 120 h c column is 00 00 for u2 | attributes_count, length is zero, so the back of the attribute_info position is not accounted for, That’s ready for the next field_info:

Second field_info: structure reference of field_info (2.5.1.1 structure of field table): 120 h d column, line 120 h e column namely 00 01 for u2 | access_flags, instructions for private type (according to 2.5.1.2 field access). The 120 h row f column, 130 h row 0, column is 00 06 for u2 | name_index1, variable names to constant pool of # 6, 130 h line (# 6 = Utf8 b) 1, 130 h row 2 column namely 00 07 is u2 | descriptor_index, point to the constant pool of # 7, # 7 = Utf8 B), B is for byte. (2.5.1.3 descriptor symbol meaning). 130 h line 3, line 130 h 4 column namely 00 00 for u2 | attributes_count, length is zero, so the attribute_info behind the position is not accounted for,

That’s ready for the next field_info:

Third field_info: structure reference of field_info (2.5.1.1 structure of field table): 130 h line five columns, column namely 00 09 130 h line 6 to u2 | access_flags, 00 09 = 0 x00 01 & 0 x00 08 = = > public static according to 2.5.1.2 field access logo). 130 h line 7, 130 h row 8 column is 00 08 for u2 | name_index1, variable names to constant pool in # 8, 130 h (# 8 = Utf8 obj) line 9, 130 h column a is 00 09 for u2 | descriptor_index, point to the constant pool of # 09 (# 9 = Utf8 Ljava/lang/Object;) , on behalf of the object type. L (2.5.1.3 descriptor symbol meaning). The 130 h b column, line 130 h c column is 00 00 for u2 | attributes_count, length is zero, so the back of the attribute_info position is not accounted for,

That’s ready for the next field_info:

Fourth field_info: structure reference of field_info (2.5.1.1 structure of field table): 130 d row 5 column, line 130 h e column namely 00 04 for u2 | access_flags, s instructions for (according to 2.5.1.2 field access). The line 130 h f, 140 h row 0, column namely 00 0 a is u2 | name_index1, variable name point to the constant pool # 10, (# 10 140 h = Utf8 ojbs) line 1 column, 140 h row 2 column namely 00 0 b is u2 | descriptor_index, pointing to the constant pool of # 11 (# 11 = Utf8 [Ljava/lang/Object), [L type represents an array of objects. (2.5.1.3 Descriptor symbol meaning). 140 h line 3, line 140 h 4 column namely 00 00 for u2 | attributes_count, length is zero, so the attribute_info behind the position is not accounted for,

2.6 File structure – Collection of method tables

type descriptor[number] remark
u2 methods_count[1]
method_info methods[methods_count]
# # # 2.6.1 overview
A method table is constructed like a field table, consisting of access_flags, name_index, Descriptor_index, and Attributes.
#### 2.6.1.1 Method table structure
field_info:
type The name of the The number of
u2 access_flags 1
u2 name_index 1
u2 descriptor_index 1
u2 attributes_count 1
attribute_info attributes attributes_count

2.6.1.2 Method access flags

The field modifier is placed in the access_flags project, which is very similar to the access_flags project in the class, and is a U2 data type.

Sign the name Flag values meaning
ACC_PUBLIC 0x00 01 Whether the method is public
ACC_PRIVATE 0x00 02 Whether the method is private
ACC_PROTECTED 0x00 04 Whether the method is protected
ACC_STATIC 0x00 08 Whether the method is static
ACC_FINAL 0x00 10 Whether the method is final
ACC_SYHCHRONRIZED 0x00 20 Whether the method is synchronized
ACC_BRIDGE 0x00 40 Method is a compiler generated method
ACC_VARARGS 0x00 80 Whether the method accepts arguments
ACC_NATIVE 0x01 00 Whether the method is native
ACC_ABSTRACT 0x04 00 Whether the method is abstract
ACC_STRICTFP 0x08 00 Whether the method is strictFP
ACC_SYNTHETIC 0x10 00 Whether a method is generated automatically by a compiler

The Java Code in the method is compiled by the compiler into bytecode instructions and stored in a property called “Code” in the method property sheet, which is the most extensive data item in the CALSS file format.

In the Java language, to override a method, in addition to simple with the original method with the same name, also requires must have a different signature with the original method, the characteristics of the signature is a method of each parameter in the constant pool collection referenced by the field symbol, is because the return value will not included in the character signature, So there is no way in the Java language to heap an existing method and reload it just by returning a different value. But in the class file format, the scope of signature is larger, and two methods can coexist as long as the descriptors are not identical. That is, if two methods have the same name and signature but return different values, they can legally coexist in the same class file.

2.6.2 interpretation

There will be a constructor by default. It’s generated at compile time. When reading the class, it shows that there are two methods. You’ll find: init is the init method. Constructor ()V representation method and viod constructor modify attribute (II)I () two of the input type int,() I is the return type.

The source code

package classstruct; public class HelloWorldMethod { public int add(int a,int b){ return a+b; } private String conact(String str1,Object objStr){ return str1+objStr; }}Copy the code

HelloWorldMethod.class

Javap results

D:\workspacejavap -verbose HelloWorldMethod.class Classfile /D:/workspace/HelloWorldMethod.class Last modified 2018-1-2;  size 827 bytes MD5 checksum b8942bc40d0567eac771e2620646aee1 Compiled from "HelloWorldMethod.java" public class classstruct.HelloWorldMethod minor version: 0 major version: 52 flags: ACC_PUBLIC, ACC_SUPER Constant pool: #1 = Methodref #8.#29 // java/lang/Object."<init>":()V #2 = Class #30 // java/lang/StringBuilder #3 = Methodref #2.#29 // java/lang/StringBuilder."<init>":() V #4 = Methodref #2.#31 // java/lang/StringBuilder.append:(Lja va/lang/String;) Ljava/lang/StringBuilder; #5 = Methodref #2.#32 // java/lang/StringBuilder.append:(Lja va/lang/Object;) Ljava/lang/StringBuilder; #6 = Methodref #2.#33 // java/lang/StringBuilder.toString:() Ljava/lang/String; #7 = Class #34 // classstruct/HelloWorldMethod #8 = Class #35 // java/lang/Object #9 = Utf8 <init> #10 = Utf8 ()V #11 = Utf8 Code #12 = Utf8 LineNumberTable #13 = Utf8 LocalVariableTable #14 = Utf8 this #15 = Utf8 Lclassstruct/HelloWorldMethod; #16 = Utf8 add #17 = Utf8 (II)I #18 = Utf8 a #19 = Utf8 I #20 = Utf8 b #21 = Utf8 conact #22 = Utf8 (Ljava/lang/String; Ljava/lang/Object;) Ljava/lang/Stri ng; #23 = Utf8 str1 #24 = Utf8 Ljava/lang/String; #25 = Utf8 objStr #26 = Utf8 Ljava/lang/Object; #27 = Utf8 SourceFile #28 = Utf8 HelloWorldMethod.java #29 = NameAndType #9:#10 // "<init>":()V #30 = Utf8 java/lang/StringBuilder #31 = NameAndType #36:#37 // append:(Ljava/lang/String;) Ljava/la ng/StringBuilder; #32 = NameAndType #36:#38 // append:(Ljava/lang/Object;) Ljava/la ng/StringBuilder; #33 = NameAndType #39:#40 // toString:()Ljava/lang/String; #34 = Utf8 classstruct/HelloWorldMethod #35 = Utf8 java/lang/Object #36 = Utf8 append #37 = Utf8 (Ljava/lang/String;) Ljava/lang/StringBuilder; #38 = Utf8 (Ljava/lang/Object;) Ljava/lang/StringBuilder; #39 = Utf8 toString #40 = Utf8 ()Ljava/lang/String; { public classstruct.HelloWorldMethod(); 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 Lclassstruct/HelloWorldMethod; public int add(int, int); descriptor: (II)I flags: ACC_PUBLIC Code: stack=2, locals=3, args_size=3 0: iload_1 1: iload_2 2: iadd 3: ireturn LineNumberTable: line 5: 0 LocalVariableTable: Start Length Slot Name Signature 0 4 0 this Lclassstruct/HelloWorldMethod; 0 4 1 a I 0 4 2 b I } SourceFile: "HelloWorldMethod.java"Copy the code

Detailed interpretation process on the basis of the previous analysis can be continued. The attributes contained in methods are related to the following {} contents in 2.7. javap, that is, the information related to method attributes.

I #17 = Utf8 (II)ICopy the code

2.7 File Structure – Property list collection

type descriptor[number] remark
u2 attributes_count[1]
attribute_info attributes[attributes_count]

2.7.1 overview

Class files, field tables, and method tables can carry their own set of property tables (like the “code” property table used in the method table above) to describe information specific to certain scenarios

2.7.1.1 Structure of property Table definition

For each attribute, its name needs to refer to a constant class representation of type CONSTANT_utf8_info from the constant pool, while the structure of the attribute value is completely custom, with only a U4 length attribute field specifying the number of bits the attribute value takes

type The name of the The number of
u2 attribute_name_index 1
u2 attribute_length 1
u1 info attribute_length

Predefined attributes in 2.7.1.2 VMS

The attribute name Use location meaning
Code Method table Bytecode instructions compiled into Java code
ConstantValue Field in the table Constant pool defined by the final keyword
Deprecated Class, method, field list Methods and fields declared deprecated
Exceptions Method table Method throws an exception
EnclosingMethod The class file This property is available only if a class is local or anonymous and identifies the enclosing method of the class
InnerClass The class file Inner class list
LineNumberTable Code attributes The mapping of Java source line numbers to bytecode instructions
LocalVariableTable Code attributes Local defecate description of the method
StackMapTable Code attributes New property in JDK1.6 that allows the new type checker to check and process classes that are required to match local variables and operands of the target method
Signature Class, method table, field table Used to support method signatures in case of generics
SourceFile The class file Record the source file name
SourceDebugExtension The class file Store additional debugging information
Synthetic Class, method table, field table Flag methods or fields are automatically generated by the compiler
LocalVariableTypeTable class The use of characteristic signatures instead of descriptors was added to describe generic parameterized types after the introduction of generic syntax
RuntimeVisibleAnnotations Class, method table, field table Support for dynamic annotations
RuntimeInvisibleAnnotations Table, method table, field table Use to indicate which annotations are not visible at runtime
RuntimeVisibleParameterAnnotation Method table Role similar to RuntimeVisibleAnnotations attribute, only role for the object
RuntimeInvisibleParameterAnnotation Method table Like RuntimeInvisibleAnnotations attribute, function as object which for method parameters
AnnotationDefault Method table Use to record the default value of the annotation class element
BootstrapMethods The class file Bootstrap qualifier used to hold an InvokedDynamic instruction reference

2.7.1.3 Code Attribute Description

The Code in the method body of Java program is compiled and processed by Javac, and eventually becomes bytecode instructions, which are stored in the Code attribute. The Code property appears in the property set of the method table, but not all method tables have this property. Methods in interfaces or classes, for example, do not have Code attributes.

After the bytecode instruction is the method’s explicit exception handling table. The exception table is not a mandatory parameter for the Code attribute:

type The name of the The number of note
u2 attribute_name_index 1 The attribute name
u4 attribute_length 1 Attribute length
u2 max_stack 1 Maximum stack depth
u2 max_locals 1 Space required for local variable scale
u4 code_length 1
u1 code code_length
u2 exception_table_length 1
exception_info exception_table exception_length
u2 attributes_count 1
attribute_info attributes attributes_count

2.7.1.4 Exceptions Attribute Detailed explanation

The Exception attribute lists Check Exceptions that can be thrown by a method. That is, the Exceptions listed after the throws keyword in the method description

type The name of the The number of
u2 attribute_name_index 1
u2 attribute_lrngth 1
u2 attribute_of_exception 1
u2 exception_index_tsble number_of_exceptions
The number_of_exceptions field in the Exception property represents the number_of_exceptions checked that the method may throw. Each checked Exception is represented by an Exception_INDEx_tsble field. Exception_index_tsble is an index to a constant of type CONSTANT_Class_info in the constant pool, representing the type of exception checked.
  
#### 2.7.1.5 LineNumberTable attributes
type The name of the The number of
u2 attribute_name_index 1
u4 attribute_length 1
u2 line_number_table_length 1
line_number_info line_number_table line_number_table_length

Line_number_table is a set of line_number_table_length and type line_number_info, The line_number_info table contains two u2-type data items, start_PC, which is the bytecode line number, and line_number, which is the Java source line number. The LineNumberTable property describes the mapping between the Java source line number and the bytecode line number (the bytecode offset). It is not a required property at runtime, but is generated by default in a Class file. You can disable or require this information to be generated using the -g: None or -g:lines options in Javac. If you choose not to generate the LineNumberTable property table, the main impact on the program is that when an exception is thrown, the line number of the error is not displayed on the stack, and breakpoints cannot be set from the source code when debugging the program

2.7.1.6 summary

There are more than 20 predefined attributes of the VIRTUAL machine, I will not introduce one by one, basically and the above several attributes are similar. The {} content of Javap is the interpretation of methods and attributes.

3 Reference Article

This series will all JVM good 2