Method invocation Directives

Source: Internet
Author: User

The bytecode instructions for the method call before JDK7 total four, invokeinterface, Invokespecial, Invokestatic, invokevirtual. These four instructions complete the invocation of all types of methods in Java.

Invokeinterface (Call interface method)


Unsigned number indexbyte1 and Indexbyte2 common component A current class constant Pool index (index), which is (indexbyte1<<8) |indexbyte2, which is the two-byte unsigned value in front of the high. A data item indexed as index in a constant pool must be a symbolic reference to an interface method that indicates the simple name and descriptor of the interface method, and indicates the interface on which the interface method resides. Then the method will be parsed, the parsing method cannot be the instance initialization method "<init> ()", or the interface or class initialization method "<clinit> ()".
Operand count is an unsigned number that is not 0. ObjectRef must be a reference type variable, in the Operation Stack, after objectref must follow several parameter values, the number of these parameter values, the type and order must be consistent with the parsed interface method descriptor. The fourth operand must have a value of 0.

Assuming that ObjectRef is a class C reference variable, the method that is actually called will look in the following order:
A. If Class C declares an instance method that is identical to the simple name and descriptor of the interface method, then this instance method is called and the lookup program ends.
B. Otherwise, if Class C has a parent class, then follow the inheritance relationship from the bottom up to step one of the parent classes of C.
C. Otherwise, the virtual machine throws a abstractmethoderror error.

If the called method contains the synchronized modifier, the OBJECTREF monitor will enter or redo as the monitorenter instruction executes in the current thread. If the method being called is not a local method, the Nargs parameter value will be popped out of the operand stack along with ObjectRef, and a new stack frame will be established for the method call in the Java Virtual machine stack. ObjectRef and parameter values are continuously placed in the local variable table of the new stack frame, ObjectRef enters the slot0,arg1 into slot1 (if Arg1 is a long or double type, it takes slot1 and Slot2), and so on. When the new stack frame becomes the current stack frame, the Java Virtual machine's PC points to the first bytecode instruction address of the called method, followed by the first instruction of the method.
If the method being called is a local method (native), if the code that implements the method is not yet bound to the Java virtual machine, then load the specific platform code into the virtual machine. The Nargs parameter values and ObjectRef are ejected from the operand stack and passed in as parameters that implement the local method code, and the specific platform code is invoked in a concrete platform manner. When the platform code execution finishes returning:
A. If the local method contains the synchronized modifier, the objectref associated monitor is updated after the current thread executes the monitorexit instruction and exits if the monitor value returns to 0.
B. If the local method has a return value, the return value is pressed into the operand stack after a specific implementation-related conversion.

Connection exception:
Any exception that is resolved by an interface method may be thrown during symbolic reference parsing of an interface method.

Run-time Exception:
Otherwise, if the OBJECTREF value is null, then the invokeinterface instruction throws a nullpointerexception.
Otherwise, if the ObjectRef class does not implement the parsed interface, then the invokeinterface instruction throws a incompatibleclasschangeerror error.
Otherwise, if there are no methods that match the simple name and descriptor, then Invokeinterface will throw abstractmethoderror.
Otherwise, if the selected method is not public, Invokeinterface throws a illegalaccesserror error.
Otherwise, if the selected method is abstract, Invokeinterface throws a abstractmethoderror error.
Otherwise, if the selected method is native and its local implementation method is not found, Nvokeinterface throws a unsatisfiedlinkerror error.

The count operand of the invokeinterface instruction records the number of parameter values for the selected method, and if the parameter type is long or double, the parameter is evaluated in two units, and any other parameter type is calculated in one unit. The 4th operand is used in Oracle's Java Virtual machine implementation to replace the invokeinterface instruction with a specialized pseudo-directive at run time, but it must guarantee forward compatibility.

Invokespecial (Call private method, parent class method, class instance constructor)


Unsigned number indexbyte1 and Indexbyte2 common component A current class constant Pool index (index), which is (indexbyte1<<8) |indexbyte2, which is the two-byte unsigned value in front of the high. A data item indexed as index in a constant pool must be a symbolic reference to a method that indicates the simple name and descriptor of the method, and indicates the class in which the method resides. If the method is parsed and found in protected, which is a member of the current class parent class, and it is not declared in the same Run-time package as the current class, then the ObjectRef class must be a subclass of the current class or the current class.
Next, the parsed method should be selected for execution, and all of the following conditions must be met:
A. The Acc_super tag is set in the access token for the current class (1)
B. The class to which the parsed method belongs is the parent class of the current class
C. The parsed method is not an instance initialization method "<init>" (although the instance initialization method is called by the invokespecial directive, it has already been executed at the initialization stage of the current class, the class initialization method cannot be executed again)

If all of the above conditions are true, then the actual executed method will be chosen in the following order. Suppose C is the immediate parent class of the current class:
1. If Class C declares an instance method that is the same as the simple name and descriptor of the parsed method, it will be called and the lookup program ends.
2. Otherwise, if Class C also has a parent class, then follow the inheritance relationship from the bottom up to the 1th step of the parent class of C.
3. Otherwise, the virtual machine throws a abstractmethoderror error.

Other behaviors, such as creating new stack frames, parameters into local variable tables, parsing exceptions, run-time exceptions, and so on, are similar to invokeinterface directives.

Invokestatic (call class, static method)


Unsigned number indexbyte1 and Indexbyte2 common component A current class constant Pool index (index), which is (indexbyte1<<8) |indexbyte2, which is the two-byte unsigned value in front of the high. A data item indexed as index in a constant pool must be a symbolic reference to a method that indicates the simple name and descriptor of the method, and indicates the class in which the method resides. The parsed method cannot be an instance initialization method "<init> ()" or a class or interface initialization method "<clinit> ()", and it must be static, so it cannot be abstract. When the method is successfully parsed, the initialization of the class that declares the method is also completed (if not initialized, the initialization phase of the triggering class contains the invokestatic directive). The maximum difference between the operands of this instruction and the other three directives is that there is no objectref, because static methods do not have receivers.

Other behaviors, such as creating new stack frames, parameters into local variable tables, parsing exceptions, run-time exceptions, and so on, are similar to invokeinterface directives.

Invokevitual (call virtual method)


see About virtual method invocation steps in method calls

Method invocation Directives

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: [email protected] 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.