Search code examples
javamethodsjvminvokedynamic

What is call site in Java when calling method?


I'm trying to understand what is call site in JVM. Quote from https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-5.html#jvms-5.4.3.6

The result of call site specifier resolution is a tuple consisting of:

• the reference to an instance of java.lang.invoke.MethodHandle,

• the reference to an instance of java.lang.invoke.MethodType,

• the references to instances of Class, java.lang.invoke.MethodHandle, java.lang.invoke.MethodType, and String.

We also have the so called call site object https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-6.html#jvms-6.5.invokedynamic:

The result returned by the bootstrap method must be a reference to an object whose class is java.lang.invoke.CallSite or a subclass of java.lang.invoke.CallSite. This object is known as the call site object

The call site object concept is clear. This just an instance of CallSite. But what about call site specifier? Is that an Java object? Is that a String literal?


Solution

    • Dynamic call site is an each occurence of invokedynamic instruction.

      Before the JVM can execute a dynamic call site (an invokedynamic instruction), the call site must first be linked. Linking is accomplished by calling a bootstrap method which is given the static information content of the call site, and which must produce a method handle that gives the behavior of the call site.

      // from java.lang.invoke package description

    • Call site specifier is an item (obtained from the constant pool) that describes how to link the given call site.

      It is not specified what this item really is.
      JVMS only tells how the symbolic reference to the call site specifier looks like in the constant pool.

      JVM implementation is free to choose an internal representation of the call site specifier. It can be an object in the heap or a piece of metadata in native memory. For example, HotSpot JVM caches the call site specifier as an object array where the first element is an instance of MethodHandle representing the bootstrap method, and the rest elements are arguments for calling this bootstrap method.

      No matter how the call site specifier is implemented inside JVM, when it is resolved, it should produce a call site object (an instance of java.lang.invoke.CallSite) which is permanently bound to the dynamic call site.