DynamicLinker (Java SE 11 & JDK 11 ) (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:

Report a bug or suggest an enhancement
For further API reference and developer documentation see the Java SE Documentation, which contains more detailed, developer-targeted descriptions with conceptual overviews, definitions of terms, workarounds, and working code examples.
Java is a trademark or registered trademark of Oracle and/or its affiliates in the US and other countries.
Copyright © 1993, 2025, Oracle and/or its affiliates, 500 Oracle Parkway, Redwood Shores, CA 94065 USA.
All rights reserved. Use is subject to license terms and the documentation redistribution policy.