CallSite explanation needed

Viewed 298

From the CallSite documentation

A CallSite is a holder for a variable MethodHandle, which is called its target. An invokedynamic instruction linked to a CallSite delegates all calls to the site's current target. A CallSite may be associated with several invokedynamic instructions, or it may be "free floating", associated with none. In any case, it may be invoked through an associated method handle called its dynamic invoker.

Questions:

  1. Can the target within CallSite be changed? how? Please provide a code sample.

  2. How can a CallSite be associated with multiple invokedynamic instructions?

  3. What is meant by "free-floating" CallSite? How can it be created?

EDIT: Note: I want to know the different ways to write code/lambda so that these 3 points occur in program.

1 Answers
  1. Yes. The target in a MutableCallSite or a VolatileCallSite can be changed via CallSite#setTarget
  2. This section of the JVM Specification defines how dynamically computed CallSites are initialized. But to answer the question, I think it is by design. i.e. the JVM Spec says it should for JVM Implementations, and compilers may use it for their advantage
  3. You do not create the CallSites directly in your code, as the compiler does it, and it is just there to state that there might be CallSites that are "free-floating".

Edit: I think you misinterpreted the documentation, I think you should never initialize CallSite directly from you code, since there is no use, and the documentation exists to inform what it does, and the JVM uses it to provide a MethodHandle for a invokedynamic instruction.

Related