public final class DynamicLinker extends Object
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:
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.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.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.Modifier and Type | Method and 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> |
link(T callSite)
Links an invokedynamic call site.
|
public <T extends RelinkableCallSite> T link(T callSite)
T
- the particular subclass of RelinkableCallSite
for
which to create a link.callSite
- the call site to link.public LinkerServices getLinkerServices()
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.public static StackTraceElement getLinkedCallSiteLocation()
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.
Copyright © 2015, 2016, Oracle and/or its affiliates. All rights reserved.
DRAFT 9-internal+0-2016-04-29-133312.buildd.src