JVM method dispatch:修订间差异

来自WHY42
第77行: 第77行:


== invokespecial ==
== invokespecial ==
Invoke instance method; special handling for superclass, private, and instance initialization method invocations
<pre>
  public HelloWorld();
    descriptor: ()V
    flags: (0x0001) 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
</pre>
== invokeinterface ==
== invokeinterface ==
== invokedynamic ==
== invokedynamic ==

2021年4月19日 (一) 09:29的版本

Method invocation

invokestatic

invoke a static method:

invokestatic
indexbyte1
indexbyte2

invoke a static method, the method is from the constant pool by

index= (indexbyte1 << 8) | indexbyte2
public class Hello {
    public static void sayHello(String msg) {
        System.out.println(msg);
    }

    public static void main(String[] args) {
        sayHello("Riguz!");
    }
}

compiled:

// Riguz!
#4 = String             #23
// com/riguz/Hello.sayHello:(Ljava/lang/String;)V
#5 = Methodref          #6.#24 
...
  public static void main(java.lang.String[]);
    descriptor: ([Ljava/lang/String;)V
    flags: ACC_PUBLIC, ACC_STATIC
    Code:
      stack=1, locals=1, args_size=1
         0: ldc           #4
         2: invokestatic  #5
         5: return
      LineNumberTable:
        line 9: 0
        line 10: 5
}

invokevirtual

Invoke instance method; dispatch based on class

invokevirtual
indexbyte1
indexbyte2

Operand stack:

..., objectref, [arg1, [arg2 ...]] →
// java/lang/System.out:Ljava/io/PrintStream;
#2 = Fieldref           #19.#20
// java/io/PrintStream.println:(Ljava/lang/String;)V   
#3 = Methodref          #21.#22   
...       
  public static void sayHello(java.lang.String);
    descriptor: (Ljava/lang/String;)V
    flags: ACC_PUBLIC, ACC_STATIC
    Code:
      stack=2, locals=1, args_size=1
         0: getstatic     #2
         3: aload_0
         4: invokevirtual #3
         7: return

invokespecial

Invoke instance method; special handling for superclass, private, and instance initialization method invocations

  public HelloWorld();
    descriptor: ()V
    flags: (0x0001) 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

invokeinterface

invokedynamic