DynamicLinker (Java SE 26 & JDK 26)
Description: declaration: module: jdk.dynalink, package: jdk.dynalink, class: DynamicLinker
Keywords: jdk.dynalink.DynamicLinker class

# Class DynamicLinker

java.lang.Object
jdk.dynalink.DynamicLinker

public final class DynamicLinker extends Object
The linker for `RelinkableCallSite` objects. A dynamic linker is a main objects when using Dynalink, it coordinates linking of call sites with linkers of available language runtimes that are represented by `GuardingDynamicLinker` objects (you only need to deal with these if you are yourself implementing a language runtime with its own object model and/or type conversions). To use Dynalink, you have to create one or more dynamic linkers using a `DynamicLinkerFactory`. Subsequently, you need to invoke its `link(RelinkableCallSite)` method from `invokedynamic` bootstrap methods to let it manage all the call sites they create. Usual usage would be to create at least one class per language runtime to contain one linker instance as:

    class MyLanguageRuntime {
        private static final GuardingDynamicLinker myLanguageLinker = new MyLanguageLinker();
        private static final DynamicLinker dynamicLinker = createDynamicLinker();

        private static DynamicLinker createDynamicLinker() {
            final DynamicLinkerFactory factory = new DynamicLinkerFactory();
            factory.setPrioritizedLinker(myLanguageLinker);
            return factory.createLinker();
        }

        public static CallSite bootstrap(MethodHandles.Lookup lookup, String name, MethodType type) {
            return dynamicLinker.link(
                new SimpleRelinkableCallSite(
                    new CallSiteDescriptor(lookup, parseOperation(name), type)));
        }

        private static Operation parseOperation(String name) {
            ...
        }
    }

The above setup of one static linker instance is often too simple. You will often have your language runtime have a concept of some kind of "context class loader" and you will want to create one dynamic linker per such class loader, to ensure it incorporates linkers for all other language runtimes visible to that class loader (see `DynamicLinkerFactory.setClassLoader(ClassLoader)`).

There are three components you need to provide in the above example:

- You are expected to provide a `GuardingDynamicLinker` for your own language. If your runtime doesn't have its own object model or type conversions, you don't need to implement a `GuardingDynamicLinker`; you would simply not invoke the `setPrioritizedLinker` method on the factory.
- The performance of the programs can depend on your choice of the class to represent call sites. The above example used `SimpleRelinkableCallSite`, but you might want to use `ChainedCallSite` instead. You'll need to experiment and decide what fits your runtime the best. You can further subclass either of these or implement your own.
- You also need to provide `CallSiteDescriptor`s to your call sites. They are immutable objects that contain all the information about the call site: the class performing the lookups, the operation being invoked, and the method signature. You will have to supply your own scheme to encode and decode operations in the call site name or static parameters, that is why in the above example the `parseOperation` method is left unimplemented.

Since:
  9

- ## Method Summary

  Modifier and Type
  Method
  Description
  `static StackTraceElement`
  `getLinkedCallSiteLocation()`
  Returns a stack trace element describing the location of the `invokedynamic` call site currently being linked on the current thread.
  `LinkerServices`
  `getLinkerServices()`
  Returns the object representing the linker services of this class that are normally exposed to individual `language-specific linkers`.
  `<T extends RelinkableCallSite>
  T`
  `link(T callSite)`
  Links an invokedynamic call site.

  ### Methods declared in class Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`
  Modifier and Type
  Method
  Description
  `protected Object`
  `clone()`
  Creates and returns a copy of this object.
  `boolean`
  `equals(Object obj)`
  Indicates whether some other object is "equal to" this one.
  `protected void`
  `finalize()`
  Deprecated, for removal: This API element is subject to removal in a future version.
  Finalization is deprecated and subject to removal in a future release.
  `final Class<?>`
  `getClass()`
  Returns the runtime class of this `Object`.
  `int`
  `hashCode()`
  Returns a hash code value for this object.
  `final void`
  `notify()`
  Wakes up a single thread that is waiting on this object's monitor.
  `final void`
  `notifyAll()`
  Wakes up all threads that are waiting on this object's monitor.
  `String`
  `toString()`
  Returns a string representation of the object.
  `final void`
  `wait()`
  Causes the current thread to wait until it is awakened, typically by being notified or interrupted.
  `final void`
  `wait(long timeoutMillis)`
  Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.
  `final void`
  `wait(long timeoutMillis, int nanos)`
  Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.

- ## Method Details

  - ### link

    public <T extends RelinkableCallSite> T link(T callSite)
    Links an invokedynamic call site. It will install a method handle into the call site that invokes the relinking mechanism of this linker. Next time the call site is invoked, it will be linked for the actual arguments it was invoked with.
    Type Parameters:
      `T` - the particular subclass of `RelinkableCallSite` for which to create a link.
    Parameters:
      `callSite` - the call site to link.
    Returns:
      the callSite, for easy call chaining.

  - ### getLinkerServices

    public LinkerServices getLinkerServices()
    Returns the object representing the linker services of this class that are normally exposed to individual `language-specific linkers`. While as a user of this class you normally only care about the `link(RelinkableCallSite)` method, in certain circumstances you might want to use the lower level services directly; either to lookup specific method handles, to access the type converters, and so on.
    Returns:
      the object representing the linker services of this class.

  - ### getLinkedCallSiteLocation

    public static StackTraceElement getLinkedCallSiteLocation()
    Returns a stack trace element describing the location of the `invokedynamic` call site currently being linked on the current thread. The operation is potentially expensive as it needs to generate a stack trace to inspect it and is intended for use in diagnostics code. For "free-floating" call sites (not associated with an `invokedynamic` instruction), the result is not well-defined.
    Returns:
      a stack trace element describing the location of the call site currently being linked, or null if it is not invoked while a call site is being linked.