APIdia Launch Partner
Top Description Fields Constructors Methods
jdk.dynalink

public final Class DynamicLinker

extends Object
Class Inheritance
Imports
java.lang.StackWalker.StackFrame, java.lang.invoke.MethodHandle, .MethodHandles, .MethodType, .MutableCallSite, java.util.Objects, jdk.dynalink.linker.GuardedInvocation, .GuardedInvocationTransformer, .GuardingDynamicLinker, .LinkRequest, .LinkerServices, jdk.dynalink.linker.support.Lookup, .SimpleLinkRequest, jdk.dynalink.support.ChainedCallSite, .SimpleRelinkableCallSite

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:

Since
9

Field Summary

Modifier and TypeField and Description
private static final String
private static final String
private final LinkerServices
private final GuardedInvocationTransformer
private static final MethodHandle
private static final StackWalker
private final boolean
private final int

Constructor Summary

AccessConstructor and Description
pack-priv
DynamicLinker(final LinkerServices
the linkerServices used by the linker, created by the factory.
linkerServices
,
final GuardedInvocationTransformer prelinkTransformer, final boolean syncOnRelink, final int unstableRelinkThreshold)

Creates a new dynamic linker.

Method Summary

Modifier and TypeMethod and Description
private MethodHandle
createRelinkAndInvokeMethod(final RelinkableCallSite callSite, final int relinkCount)

public static StackTraceElement

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.
getLinkedCallSiteLocation
()

Returns a stack trace element describing the location of the invokedynamic call site currently being linked on the current thread.

public LinkerServices

Returns:

the object representing the linker services of this class.
getLinkerServices
()

Returns the object representing the linker services of this class that are normally exposed to individual language-specific linkers.

private static boolean

Returns:

true if this frame represents MethodHandleNatives.linkCallSite().
isInitialLinkFrame
(final StackWalker.StackFrame
the frame
frame
)

Returns true if the frame represents MethodHandleNatives.linkCallSite(), the frame immediately on top of the call site frame when the call site is being linked for the first time.

private static boolean

Returns:

true if this frame represents DynamicLinker.relink().
isRelinkFrame
(final StackWalker.StackFrame
the frame
frame
)

Returns true if the frame represents DynamicLinker.relink(), the frame immediately on top of the call site frame when the call site is being relinked (linked for second and subsequent times).

private static boolean
testFrame(final StackWalker.StackFrame frame, final String methodName, final String className)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

CLASS_NAMEback to summary
private static final String CLASS_NAME
INVOKE_PACKAGE_PREFIXback to summary
private static final String INVOKE_PACKAGE_PREFIX
linkerServicesback to summary
private final LinkerServices linkerServices
prelinkTransformerback to summary
private final GuardedInvocationTransformer prelinkTransformer
stackWalkerback to summary
private static final StackWalker stackWalker
unstableRelinkThresholdback to summary
private final int unstableRelinkThreshold

Constructor Detail

DynamicLinkerback to summary
pack-priv DynamicLinker(final LinkerServices linkerServices, final GuardedInvocationTransformer prelinkTransformer, final boolean syncOnRelink, final int unstableRelinkThreshold)

Creates a new dynamic linker.

Parameters
linkerServices:LinkerServices

the linkerServices used by the linker, created by the factory.

prelinkTransformer:GuardedInvocationTransformer

see DynamicLinkerFactory#setPrelinkTransformer(GuardedInvocationTransformer)

syncOnRelink:boolean

see DynamicLinkerFactory#setSyncOnRelink(boolean)

unstableRelinkThreshold:int

see DynamicLinkerFactory#setUnstableRelinkThreshold(int)

Method Detail

createRelinkAndInvokeMethodback to summary
private MethodHandle createRelinkAndInvokeMethod(final RelinkableCallSite callSite, final int relinkCount)
getLinkedCallSiteLocationback to summary
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:StackTraceElement

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.

getLinkerServicesback to summary
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:LinkerServices

the object representing the linker services of this class.

isInitialLinkFrameback to summary
private static boolean isInitialLinkFrame(final StackWalker.StackFrame frame)

Returns true if the frame represents MethodHandleNatives.linkCallSite(), the frame immediately on top of the call site frame when the call site is being linked for the first time.

Parameters
frame:StackWalker.StackFrame

the frame

Returns:boolean

true if this frame represents MethodHandleNatives.linkCallSite().

isRelinkFrameback to summary
private static boolean isRelinkFrame(final StackWalker.StackFrame frame)

Returns true if the frame represents DynamicLinker.relink(), the frame immediately on top of the call site frame when the call site is being relinked (linked for second and subsequent times).

Parameters
frame:StackWalker.StackFrame

the frame

Returns:boolean

true if this frame represents DynamicLinker.relink().

testFrameback to summary
private static boolean testFrame(final StackWalker.StackFrame frame, final String methodName, final String className)