invokespecial <method-spec><method-spec> is a method specification. It is a single token made up of three parts: a classname, a methodname and a descriptor. e.g.
java/lang/StringBuffer/charAt(I)Cis the method called "charAt" in the class called "java.lang.StringBuffer", and it has the descriptor "(I)C" (i.e. it takes an integer argument and returns a char result). In Jasmin, the characters up to the '(' character in <method-spec> form the classname and methodname (the classname is all the characters up to the last '/' character, and the methodname is all the characters between the last '/' and the '(' character). The characters from '(' to the end of the string are the descriptor. This is illustrated in the following diagram:
foo/baz/Myclass/myMethod(Ljava/lang/String;)V --------------- --------------------- | -------- | | | | classname methodname descriptorStack
Before | After |
arg1 | [result] |
arg2 | ... |
... | |
argN | |
objectref | |
... |
invokevirtual dispatches a Java method. It is used in Java to invoke all methods except interface methods (which use invokeinterface), static methods (which use invokestatic), and the few special cases handled by invokespecial.
For example, when you write in Java:
Object x; ... x.equals("hello");this is compiled into something like:
aload_1 ; push local variable 1 (i.e. 'x') onto stack ldc "hello" ; push the string "hello" onto stack ; invoke the equals method invokevirtual java/lang/Object/equals(Ljava/lang/Object;)Z ; the boolean result is now on the stackNote that the actual method run depends on the runtime type of the object invokevirtual is used with. So in the example above, if x is an instance of a class that overrides Object's equal method, then the subclasses' overridden version of the equals method will be used.
Before performing the method invokation, the class and the method identified by <method-spec> are resolved. See Chapter 9 for a description of how methods are resolved.
invokevirtual looks at the descriptor given in <method-spec>, and determines how many arguments the method takes (this may be zero). It pops these arguments off the operand stack. Next it pops objectref off the stack. objectref is a reference to the object whose method is being called. invokevirtual retrieves the Java class for objectref, and searches the list of methods defined by that class and then its superclasses, looking for a method called methodname, whose descriptor is descriptor.
Once a method has been located, invokevirtual calls the method. First, if the method is marked as synchronized, the monitor associated with objectref is entered. Next, a new stack frame structure is established on the call stack. Then the arguments for the method (which were popped off the current method's operand stack) are placed in local variables of the new stack frame structure. arg1 is stored in local variable 1, arg2 is stored in local variable 2 and so on. objectref is stored in local variable 0 (the local variable used for the special Java variable this). Finally, execution continues at the first instruction in the bytecode of the new method.
When the method called by invokevirtual returns, any single (or double) word return result is placed on the operand stack of the current method and execution continues at the instruction that follows invokevirtual in the bytecode.
Exceptions
NullPointerException - objectref is null
StackOverflowError - no more space in callstack for a new stack frame
Bytecode
In bytecode, after the invokevirtual opcode is a 16-bit unsigned integer index. This is the index of an entry in the constant pool. The entry is tagged as a CONSTANT_Methodref entry. This entry has two fields. One field points to a CONSTANT_Class entry in the constant pool whose name is the classname from <method-spec>, and the other points to a CONSTANT_NameAndType entry in the constant pool whose name is the methodname from <method-spec> and whose descriptor is the type descriptor from <method-spec>.
Type | Description |
u1 | invokevirtual opcode = 0xB6 (182) |
u2 | index |
invokespecial, invokestatic, invokeinterface