Module jdk.dynalink
Package jdk.dynalink

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 CallSiteDescriptors 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