Learn more about the Java Virtual Machine (3) class file structure

Source: Internet
Author: User
Tags bitwise deprecated field table instance method

Virtual machine Execution Subsystem I. CLASS file structure

1. Magic number and class version

1.magic-Number of demons: 0xcafebabe;4 bytes

2.minor_version: Minor version, followed by number; 2 bytes

3.major_version: Main version, previous number; 2 bytes

2. Constant pool

1.constant_pool_count: Constant Pool constant number (= this value-1): 2 bytes

Because the number of constants in a constant pool is not fixed, the entry for a constant pool needs to place a U2 type of data that represents the constant pool capacity count value.

2.constant_pool: Constants, the first bit is the type bit, followed by the definition of the respective constants: N bytes

    

   

   

   

3. Access identifiers

1.access_flags: Access Identity

This flag is used to identify some class or interface-level access information, including whether the class or interface is defined as a public type, whether it is defined as an abstract type, or whether it is declared final, if it is a class.

   

such as: 0x0001 0x0020 description is a public class

4. Class index, parent index, interface index: The class file is determined by the three data to determine the inheritance relationship

1.this_class: Class Index: 2 bytes

The class index is used to determine the fully qualified name of this class

2.super_class: Parent class Index: 2 bytes

The parent class index is used to determine the fully qualified name of the parent class of this class. Because the Java language does not allow multiple inheritance, the parent class index has only one, except Java.lang.Object, all Java classes have a parent class, so all Java classes except Java.lang.Object, the parent class index is not 0

3.interfaces: Interface index: 2 byte array

The interface index collection is used to describe which interfaces this class implements, and the interfaces that are implemented will be ordered from left to right in the interface index collection by pressing the Implements statement (if the class itself is an interface, then the extends statement)

Interface index starts with quantity: 2 bytes

  Find :

The class index, the parent class index, and the interface index collection are sorted sequentially after the access flag, and the class and parent indexes are represented by index values of two U2 types, each pointing to a class description constant of type Constant_class_info, by Constant_class_ An index value in a constant of type info can find the fully qualified name string defined in a constant of type Constant_utf8_info

    

5. Field table Collection

The information that can be included is:

The scope of the field (public, private, protected modifier), instance variable or class variable (static modifier), variability (final), concurrency visibility (volatile modifier, whether to force read and write from main memory), can be serialized (transient modifier), field data type (base type, object, array), field name. In the above information, each modifier is a Boolean value, either with a modifier or not, and is suitable for use with a flag bit.

Field names, field data types, which cannot be fixed, can only refer to constants in a constant pool to describe

1.access_flags: Field Access ID: U2

2.name_index: Field simple name: U2

Referencing Chang constants

3.descriptor_index: Method Descriptor: U2

Describing Fields: field type

Describe method: (parameter list) descriptor

Referencing Chang constants

6. Collection of method tables

  

  

Attribute table for 7.class, fields, methods, etc.

There are 21 predefined, each of which has its own structure.

1.Code Properties

    

Attribute_name_index is an index that points to a Constant_utf8_info constant whose constant value is fixed as "Code", which represents the property name of the attribute

Attribute_length indicates the length of the property value, so the value of the property is fixed to the entire property sheet length minus 6 bytes.

Max_stack represents the maximum value of the operand stack (Operand Stacks) depth. At any point in the execution of the method, the operand stack does not exceed this depth. When a virtual machine is running, it is necessary to allocate the stack depth in stacks frame (stackframe) based on this value.

Max_locals represents the storage space required for a local variable table.

Code_length and code are used to store bytecode instructions generated by the Java source program after compilation.

Code: Byte code

Exception: Exception table

      

If the byte code occurs between line START_PC [1] and line end_pc (without the line end_pc), an exception of type Catch_type or its subclass (Catch_type to an index to a Constant_class_info type constant) appears

Then go to line handler_pc to continue processing. When the value of Catch_type is 0 o'clock, it is necessary to turn to handler_pc to handle any abnormal situation.

//Java source Code Public intInc () {intx;Try{x=1;returnx;}Catch(Exception e) {x=2;returnx;}finally{x=3;}}//compiled bytecode byte code and exception table Public intInc. (code:stack);=1,locals=5,args_size=10:iconst_1//X=1 in a try block1: Istore_12:iload_1//save X to ReturnValue, at which point X=13:istore 45:iconst_3//X=3 in the finaly block6: Istore_17:iload 4//put the value in ReturnValue to the top of the stack, ready to return to Ireturn9: Ireturn10:astore_2//assign value to exception e defined in catch, stored in slot 211:iconst_2//x=2 in a catch block12: Istore_113:iload_1//save X to ReturnValue, at which point x=214:istore 416:iconst_3//X=3 in the finaly block17: Istore_118:iload 4//put the value in ReturnValue to the top of the stack, ready to return to Ireturn20: Ireturn21:astore_3//If there is an exception that does not belong to Java.lang.Exception and its subclasses, it will come here .22:iconst_3//X=3 in the finaly block23: Istore_124:aload_3//Place the exception to the top of the stack and throw25: athrowexception table:from to target type0 5 Class java/lang/Exception0 5 21 anyTen + any

The exception table is actually part of the Java code, and the compiler uses exception tables instead of simple jump commands to implement Java exceptions and finally processing mechanisms

2.Exception Properties

Exception to method

    

Number_of_exceptions item representation method may throw Number_of_exceptions type-checked exception

Exception_index_table is an index to a constant pool of Constant_class_info constants that represents the type of the exception being checked.

3.LineNumberTable Properties

The linenumbertable attribute is used to describe the correspondence between the Java source line number and the byte code line number (the byte code offset) It is not a required property at runtime, but is generated by default into the class file

You can use the-g:none or-g:lines option in Javac to cancel or require this information to be generated separately. If you choose not to generate the Linenumbertable property

The most important effect on the program is that when an exception is thrown, the line number of the error will not be displayed in the stack, and the breakpoint cannot be set according to the source line when the program is debugged.

4.LocalVariableTable Properties

The Localvariabletable property is used to describe the relationship between a variable in a local variable table in a stack frame and a variable defined in Java source code, and it is not a required property at run time.

However, by default it is generated into the class file, and you can use the-g:none or-g:vars option in Javac to cancel or require this information to be generated.

If this property is not generated, the biggest effect is that when others refer to the method, all parameter names will be lost, and the IDE will replace the original parameter names with placeholders such as arg0, arg1, which have no effect on the program's operation, but will have great inconvenience to code writing. And it is not possible to get parameter values from the context for parameter names during debugging

5.SourceFile Properties

The SourceFile property is used to record the name of the source file that generated the class file. This property is also optional and can be turned off or required to generate this information using Javac's-g:none or-g:source options, respectively.

In Java, the class name and file name are consistent for most classes, but there are special cases (such as inner classes) that are exceptions. If this property is not generated, the file name to which the error code belongs is not displayed in the stack when an exception is thrown

6.ConstantValue Properties

The purpose of the Constantvalue property is to notify the virtual machine to automatically assign a value to a static variable.

Only variables that are modified by the static keyword (class variables) can use this property.

The assignment of a variable of a non-static type (that is, an instance variable) is done in the instance constructor method;

For class variables, there are two ways to choose:

If both final and static are used to decorate a variable, and the data type of the variable is a primitive type or java.lang.String, the Constantvalue property is generated to initialize it.

If the variable is not final decorated, or is not a primitive type and string, it is chosen to be initialized in the method.

7.InnerClasses Properties

The Innerclasses property is used to record the association between the inner class and the host class. If an inner class is defined in a class, the compiler generates innerclasses properties for it and the inner classes it contains

8.Deprecated and Synthetic properties

The deprecated property is used to represent a class, field, or method that has been designated by the program author to be deprecated and can be set by using the @deprecated annotation in the code.

The Synthetic property represents this field or method is not generated directly by the Java source code, but is added by the compiler itself,

9.StackMapTable Properties

BYTE code Verification

10.Signature Properties

After a generic is erased, gets the generic information

11.BootstrapMethods Properties

The Bootstrapmethods property was added to the class file specification after JDK 1.7 was released as a complex variable-length property in the class file's attribute table. This property is used to hold the boot method qualifier referenced by the invokedynamic directive.

8. Introduction to bytecode Instructions 1. Loading and storing instructions

The load and store directives are used to transfer data back and forth between the local variable table in the stack frame and the operand stack (see Chapter 2nd, Introduction to memory regions), which includes the following content.

Load a local variable into the Operation stack: iload, Iload_ , Lload, Lload_ , Fload, Fload_ , Dload, Dload_ , Aload, Aload_ .

Stores a number from the operand stack to the local variable table: istore, Istore_ , Lstore, Lstore_ , Fstore, Fstore_ , Dstore, Dstore_ , Astore, Astore_ .

Load a constant into the operand stack: Bipush, Sipush, LDC, Ldc_w, Ldc2_w, Aconst_null, ICONST_M1, Iconst_ , Lconst_ , Fconst_ , Dconst_ .

An instruction to expand the access index of a local variable table: wide.

2. Operational instructions

Addition instructions: Iadd, Ladd, Fadd, Dadd.
Subtraction directives: Isub, Lsub, Fsub, dsub.
Multiplication directives: Imul, Lmul, Fmul, Dmul.
Division instruction: Idiv, Ldiv, Fdiv, Ddiv.
The remainder instruction: Irem, Lrem, Frem, Drem.
Take Counter-instructions: Ineg, Lneg, Fneg, Dneg.
Displacement directives: ISHL, Ishr, Iushr, LSHL, Lshr, Lushr.
Bitwise OR instruction: IOR, lor.
Bitwise AND INSTRUCTION: Iand, land.
Bitwise XOR OR instruction: Ixor, Lxor.
Local variable self-increment directive: Iinc.
Comparison directives: Dcmpg, Dcmpl, Fcmpg, Fcmpl, lcmp.

3. Type conversion directives

The Java Virtual machine directly supports (that is, conversion without explicit conversion instructions) The following numeric types of the widening type conversion (widening Numeric Conversions, the small-range type to a large-scope type of security conversion):

type int to a long, float, or double type.

A long type to a float, double type.

Float type to double type.
Processing of narrowing type conversions (Narrowing Numeric Conversions)

These conversion directives include: I2b, I²c, I2s, L2i, F2i, f2l, D2i, d2l, and d2f.

4. Object access and Creation

Instructions for creating an instance of a class: new.

Instructions for creating an array: NewArray, Anewarray, Multianewarray.

Accesses a class field (a static field, or a class variable) and an instance field (not a static field, or an instance variable): GetField, Putfield, Getstatic, putstatic.

An instruction that loads an array element into the operand stack: baload, Caload, Saload, Iaload, Laload, Faload, Daload, Aaload.

An instruction that stores the value of an operand stack to an array element: Bastore, Castore, Sastore, Iastore, Fastore, Dastore, Aastore.

The command to take the length of the array: Arraylength.

Check the instruction for the class instance type: Instanceof, Checkcast.

5. Operation Count Stack Management instructions

As with a stack in a normal data structure, the Java Virtual machine provides some instructions for manipulating the stack of operations directly.

Including:

Stacks top one or two elements of the stack stack: Pop, pop2.

Copy the stack top one or two values and re-press the copied value or double copy value into the top of the stack: DUP, dup2, dup_x1, dup2_x1, dup_x2, dup2_x2.

Swap the top two values of the stack: swap

6. Control Transfer Instructions

Conditional branches: ifeq, Iflt, Ifle, Ifne, IFGT, IFGE, Ifnull, Ifnonnull, If_icmpeq, If_icmpne, If_icmplt, IF_ICMPGT, If_icmple, if_ Icmpge, If_acmpeq and If_acmpne.

Compound conditional branches: Tableswitch, Lookupswitch.

Unconditional branching: Goto, Goto_w, JSR, Jsr_w, ret.

7. Method invocation and return

    The invokevirtual directive is used to invoke an instance method of an object, which is dispatched according to the actual type of the object (virtual method dispatch), which is the most common method of assigning methods in the Java language.

    The invokeinterface directive is used to invoke an interface method that searches for an object that implements the interface method at run time, and finds the appropriate method to invoke.

    The invokespecial directive is used to invoke some instance methods that require special handling, including instance initialization methods, private methods, and parent class methods.

    The invokestatic directive is used to invoke a class method (static method).

    The invokedynamic directive is used to dynamically parse the method referenced by the call Point qualifier at run time and execute the method, and the dispatch logic of the preceding 4 call instructions is cured inside the Java virtual machine.

The dispatch logic of the invokedynamic instruction is determined by the user-defined boot method. The method invocation instruction is independent of the data type, and the method return instruction is differentiated by the type of the return value, including Ireturn (used when the return value is Boolean, Byte, char, short, and int types), Lreturn, Freturn, Dreturn and Areturn, plus a return directive for methods declared void, instance initialization methods, and class initialization methods for classes and interfaces using

8. Exception Handling Instructions

An operation that explicitly throws an exception in a Java program (a throw statement) is implemented by the athrow directive

In addition to explicitly throwing exceptions with the throw statement, the Java Virtual Machine specification also specifies that many run-time exceptions will be thrown automatically when other Java Virtual machine instructions detect abnormal conditions.

For example, in the integer operation described earlier, when the divisor is zero, the virtual opportunity throws a ArithmeticException exception in the Idiv or ldiv instruction.

In Java virtual machines, processing exceptions (catch statements) are not implemented by bytecode directives (which were implemented long ago using the JSR and RET directives, which are now unused), but instead are done using exception tables.

9. Synchronization Instructions

Synchronizing a sequence of instruction sets is typically represented by a synchronized statement block in the Java language.

The Java Virtual machine instruction set has Monitorenter and monitorexit two instructions to support the semantics of the Synchronized keyword

voidOnlyme (Foo f) {synchronized(f) {dosomething ();}}//Instruction CodeMethodvoidOnlyme (Foo)0 aload_1//put object f into the stack1 DUP//Copy the top element of the stack (that is, the reference to f)2 astore_2//stores the top element of the stack in the local variable table slot 23 Monitorenter//start synchronizing with the top element of the stack (i.e. F) as the lock4 Aload_0//The elements of the local variable slot 0 (that is, the this pointer) are put into the stack5 invokevirtual#5//Call the DoSomething () method8 Aload_2//slow the element of local variable 2 (i.e. f) into the stack9 Monitorexit//Exit Synchronization10Goto18//method ends normally, jumps to 18 to returnAstore_3//from this step is the exception path, see the following exception table TagetAload_2//slow the element of local variable 2 (i.e. f) into the stackMonitorexit//Exit SynchronizationAload_3//slow elements of a local variable of 3 (that is, exception objects) into the stackAthrow//To re-throw the exception object to the caller of the Onlyme () method18return//method returns normallyException table:fromto Target Type4 10 13 anyAll

    

Learn more about the Java Virtual Machine (3) class file structure

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.