DynamicLinker (Java SE 15 & JDK 15) (original) (raw)


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 byGuardingDynamicLinker 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 frominvokedynamic 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 (seeDynamicLinkerFactory.setClassLoader(ClassLoader)).

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

Modifier and Type Method Description
static StackTraceElement getLinkedCallSiteLocation() Returns a stack trace element describing the location of theinvokedynamic 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 java.lang.Object

[clone](../../../java.base/java/lang/Object.html#clone%28%29), [equals](../../../java.base/java/lang/Object.html#equals%28java.lang.Object%29), [finalize](../../../java.base/java/lang/Object.html#finalize%28%29), [getClass](../../../java.base/java/lang/Object.html#getClass%28%29), [hashCode](../../../java.base/java/lang/Object.html#hashCode%28%29), [notify](../../../java.base/java/lang/Object.html#notify%28%29), [notifyAll](../../../java.base/java/lang/Object.html#notifyAll%28%29), [toString](../../../java.base/java/lang/Object.html#toString%28%29), [wait](../../../java.base/java/lang/Object.html#wait%28%29), [wait](../../../java.base/java/lang/Object.html#wait%28long%29), [wait](../../../java.base/java/lang/Object.html#wait%28long,int%29)