4

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.

Azodious
  • 13,752
  • 1
  • 36
  • 71
  • 3
    1. has been answered. To answer the others literally: 2. By letting the bootstrap method return the same `CallSite` instance multiple times. 3. Every `CallSite` object is initially free-floating until it has been returned by a bootstrap method during an `invokedynamic` linking process. If you never return it in such a process, it will stay free-floating. – Holger Sep 01 '20 at 08:04
  • @Holger: it is clear now. Can you please also answer my question in last comment of below answer? – Azodious Sep 01 '20 at 09:09
  • 1
    It’s not clear what you are asking in that comment. – Holger Sep 01 '20 at 09:28
  • @Holger: I want to know that when single abstract method of FunctionalInterface is invoked, how does call go to desugared method? – Azodious Sep 01 '20 at 11:00
  • 2
    As other comments already told, the `LambdaMetaFactory` *generates a class*. That class implements the interface with a method that will invoke the target method. – Holger Sep 01 '20 at 11:02

1 Answers1

3
  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.

Deadbeef
  • 1,499
  • 8
  • 20
  • My questions are more towards how can we do it in code? As you also said that CallSite shouldn't be created by developer so i want to know that how to write code so that compilter performs those 3 points. – Azodious Aug 31 '20 at 10:15
  • 1
    The JSR contributors created CallSites and InvokeDynamic "intending to enhance the JVM support for Dynamic Type Languages." Since `LambdaMetafactory` only creates `ConstantCallSite`s, there is no compiling examples, I suggest you to take a look at [this answer](https://stackoverflow.com/a/61043989/13854774). – Deadbeef Aug 31 '20 at 10:50
  • `Since LambdaMetafactory only creates ConstantCallSites, there is no compiling examples`. Can you please share some link to prove that? – Azodious Sep 01 '20 at 03:52
  • 1
    LambdaMetaFactory [uses the InnerClassLambdaMetafactory](https://github.com/openjdk/jdk/blob/2914064cf4c856454367d4ab6ea914409b57129d/src/java.base/share/classes/java/lang/invoke/LambdaMetafactory.java#L502), which [only creates ConstantCallSites](https://github.com/openjdk/jdk/blob/2914064cf4c856454367d4ab6ea914409b57129d/src/java.base/share/classes/java/lang/invoke/InnerClassLambdaMetafactory.java#L232). – Deadbeef Sep 01 '20 at 04:08
  • Awesome. One mind tingling question: When it is said that `CallSite will return` or `CallSite is invoked` so what does it mean? After getting CallSIte object how is lambda desugared method is invoked when FunctionalInterface method is invoked? – Azodious Sep 01 '20 at 06:38