- java.lang.Object
-
- jdk.dynalink.DynamicLinkerFactory
-
public final class DynamicLinkerFactory extends Object
A factory class for creatingDynamicLinker
objects. Dynamic linkers are the central objects in Dynalink; these are composed of severalGuardingDynamicLinker
objects and coordinate linking of call sites with them. The usual dynamic linker is a linker composed of allGuardingDynamicLinker
objects explicitly pre-created by the user of the factory and configured withsetPrioritizedLinkers(List)
, as well as anyautomatically discovered
ones, and finally the ones configured withsetFallbackLinkers(List)
; this last category usually includesBeansLinker
.
-
-
Constructor Summary
Constructors Constructor Description DynamicLinkerFactory()
Creates a new dynamic linker factory with default configuration.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description DynamicLinker
createLinker()
Creates a new dynamic linker based on the current configuration.List<ServiceConfigurationError>
getAutoLoadingErrors()
Returns a list ofServiceConfigurationError
s that were encountered while loading automatically discovered linkers during the last invocation ofcreateLinker()
.void
setAutoConversionStrategy(MethodTypeConversionStrategy autoConversionStrategy)
Sets an object representing the conversion strategy for automatic type conversions.void
setClassLoader(ClassLoader classLoader)
Sets the class loader for automatic discovery of available guarding dynamic linkers.void
setFallbackLinkers(List<? extends GuardingDynamicLinker> fallbackLinkers)
Sets the fallback guarding dynamic linkers.void
setFallbackLinkers(GuardingDynamicLinker... fallbackLinkers)
Sets the fallback guarding dynamic linkers.void
setInternalObjectsFilter(MethodHandleTransformer internalObjectsFilter)
Sets a method handle transformer that is supposed to act as the implementation ofLinkerServices.filterInternalObjects(MethodHandle)
for linker services of dynamic linkers created by this factory.void
setPrelinkTransformer(GuardedInvocationTransformer prelinkTransformer)
Set the pre-link transformer.void
setPrioritizedLinker(GuardingDynamicLinker prioritizedLinker)
Sets a single prioritized linker.void
setPrioritizedLinkers(List<? extends GuardingDynamicLinker> prioritizedLinkers)
Sets the prioritized guarding dynamic linkers.void
setPrioritizedLinkers(GuardingDynamicLinker... prioritizedLinkers)
Sets the prioritized guarding dynamic linkers.void
setSyncOnRelink(boolean syncOnRelink)
Sets whether the dynamic linker created by this factory will invokeMutableCallSite.syncAll(MutableCallSite[])
after a call site is relinked.void
setUnstableRelinkThreshold(int unstableRelinkThreshold)
Sets the unstable relink threshold; the number of times a call site is relinked after which it will be considered unstable, and subsequent link requests for it will indicate this.
-
-
-
Constructor Detail
-
DynamicLinkerFactory
public DynamicLinkerFactory()
Creates a new dynamic linker factory with default configuration. Upon creation, the factory can be configured using varioussetXxx()
methods and used to create one or more dynamic linkers according to its current configuration usingcreateLinker()
.
-
-
Method Detail
-
setClassLoader
public void setClassLoader(ClassLoader classLoader)
Sets the class loader for automatic discovery of available guarding dynamic linkers.GuardingDynamicLinkerExporter
implementations available through this class loader will be automatically instantiated using theServiceLoader
mechanism and the linkers they provide will be incorporated intoDynamicLinker
s that this factory creates. This allows for cross-language interoperability where call sites belonging to this language runtime can be linked by linkers from these automatically discovered runtimes if their native objects are passed to this runtime. If class loader is not set explicitly by invoking this method, then the thread context class loader of the thread invokingcreateLinker()
will be used. If this method is invoked explicitly with null thenServiceLoader.loadInstalled(Class)
will be used to load the linkers.- Parameters:
classLoader
- the class loader used for the automatic discovery of available linkers.
-
setPrioritizedLinkers
public void setPrioritizedLinkers(List<? extends GuardingDynamicLinker> prioritizedLinkers)
Sets the prioritized guarding dynamic linkers. Language runtimes using Dynalink will usually have at least one linker for their own language. These linkers will be consulted first by the resulting dynamic linker when it is linking call sites, before any autodiscovered and fallback linkers. If the factory also autodiscovers a linker class matching one of the prioritized linkers, the autodiscovered class will be ignored and the explicit prioritized instance will be used.- Parameters:
prioritizedLinkers
- the list of prioritized linkers. Can be null.- Throws:
NullPointerException
- if any of the list elements are null.
-
setPrioritizedLinkers
public void setPrioritizedLinkers(GuardingDynamicLinker... prioritizedLinkers)
Sets the prioritized guarding dynamic linkers. Identical to callingsetPrioritizedLinkers(List)
withArrays.asList(prioritizedLinkers)
.- Parameters:
prioritizedLinkers
- an array of prioritized linkers. Can be null.- Throws:
NullPointerException
- if any of the array elements are null.
-
setPrioritizedLinker
public void setPrioritizedLinker(GuardingDynamicLinker prioritizedLinker)
Sets a single prioritized linker. Identical to callingsetPrioritizedLinkers(List)
with a single-element list.- Parameters:
prioritizedLinker
- the single prioritized linker. Must not be null.- Throws:
NullPointerException
- if null is passed.
-
setFallbackLinkers
public void setFallbackLinkers(List<? extends GuardingDynamicLinker> fallbackLinkers)
Sets the fallback guarding dynamic linkers. These linkers will be consulted last by the resulting dynamic linker when it is linking call sites, after any autodiscovered and prioritized linkers. If the factory also autodiscovers a linker class matching one of the fallback linkers, the autodiscovered class will be ignored and the explicit fallback instance will be used.- Parameters:
fallbackLinkers
- the list of fallback linkers. Can be empty to indicate the caller wishes to set no fallback linkers. Note that if this method is not invoked explicitly or is passed null, then the factory will create an instance ofBeansLinker
to serve as the default fallback linker.- Throws:
NullPointerException
- if any of the list elements are null.
-
setFallbackLinkers
public void setFallbackLinkers(GuardingDynamicLinker... fallbackLinkers)
Sets the fallback guarding dynamic linkers. Identical to callingsetFallbackLinkers(List)
withArrays.asList(fallbackLinkers)
.- Parameters:
fallbackLinkers
- an array of fallback linkers. Can be empty to indicate the caller wishes to set no fallback linkers. Note that if this method is not invoked explicitly or is passed null, then the factory will create an instance ofBeansLinker
to serve as the default fallback linker.- Throws:
NullPointerException
- if any of the array elements are null.
-
setSyncOnRelink
public void setSyncOnRelink(boolean syncOnRelink)
Sets whether the dynamic linker created by this factory will invokeMutableCallSite.syncAll(MutableCallSite[])
after a call site is relinked. Defaults to false. You probably want to set it to true if your runtime supports multithreaded execution of dynamically linked code.- Parameters:
syncOnRelink
- true for invoking sync on relink, false otherwise.
-
setUnstableRelinkThreshold
public void setUnstableRelinkThreshold(int unstableRelinkThreshold)
Sets the unstable relink threshold; the number of times a call site is relinked after which it will be considered unstable, and subsequent link requests for it will indicate this. Defaults to 8 when not set explicitly.- Parameters:
unstableRelinkThreshold
- the new threshold. Must not be less than zero. The value of zero means that call sites will never be considered unstable.- See Also:
LinkRequest.isCallSiteUnstable()
-
setPrelinkTransformer
public void setPrelinkTransformer(GuardedInvocationTransformer prelinkTransformer)
Set the pre-link transformer. This is aGuardedInvocationTransformer
that will get the final chance to modify the guarded invocation after it has been created by a component linker and before the dynamic linker links it into the call site. It is normally used to adapt the return value type of the invocation to the type of the call site. When not set explicitly, a default pre-link transformer will be used that simply callsGuardedInvocation.asType(LinkerServices, MethodType)
. Customized pre-link transformers are rarely needed; they are mostly used as a building block for implementing advanced techniques such as code deoptimization strategies.- Parameters:
prelinkTransformer
- the pre-link transformer for the dynamic linker. Can be null to have the factory use the default transformer.
-
setAutoConversionStrategy
public void setAutoConversionStrategy(MethodTypeConversionStrategy autoConversionStrategy)
Sets an object representing the conversion strategy for automatic type conversions. AfterLinkerServices.asType(MethodHandle, MethodType)
has applied all custom conversions to a method handle, it still needs to effectmethod invocation conversions
that can usually be automatically applied as perMethodHandle.asType(MethodType)
. However, sometimes language runtimes will want to customize even those conversions for their own call sites. A typical example is allowing unboxing of null return values, which is by default prohibited by ordinaryMethodHandles.asType()
. In this case, a language runtime can install its own custom automatic conversion strategy, that can deal with null values. Note that when the strategy'sMethodTypeConversionStrategy.asType(MethodHandle, MethodType)
is invoked, the custom language conversions will already have been applied to the method handle, so by design the difference between the handle's current method type and the desired final type will always only be ones that can be subjected to method invocation conversions. The strategy also doesn't need to invoke a finalMethodHandle.asType()
as that will be done internally as the final step.- Parameters:
autoConversionStrategy
- the strategy for applying method invocation conversions for the linker created by this factory. Can be null for no custom strategy.
-
setInternalObjectsFilter
public void setInternalObjectsFilter(MethodHandleTransformer internalObjectsFilter)
Sets a method handle transformer that is supposed to act as the implementation ofLinkerServices.filterInternalObjects(MethodHandle)
for linker services of dynamic linkers created by this factory. Some language runtimes can have internal objects that should not escape their scope. They can add a transformer here that will modify the method handle so that any parameters that can receive potentially internal language runtime objects will have a filter added on them to prevent them from escaping, potentially by wrapping them. The transformer can also potentially add an unwrapping filter to the return value.DefaultInternalObjectFilter
is provided as a convenience class for easily creating such filtering transformers.- Parameters:
internalObjectsFilter
- a method handle transformer filtering out internal objects, or null.
-
createLinker
public DynamicLinker createLinker()
Creates a new dynamic linker based on the current configuration. This method can be invoked more than once to create multiple dynamic linkers. Automatically discovered linkers are newly instantiated on every invocation of this method. It is allowed to change the factory's configuration between invocations. The method is not thread safe. After invocation, callers can invokegetAutoLoadingErrors()
to retrieve a list ofServiceConfigurationError
s that occurred while trying to load automatically discovered linkers. These are never thrown from the call to this method as it makes every effort to recover from them and ignore the failing linkers.- Returns:
- the new dynamic Linker
-
getAutoLoadingErrors
public List<ServiceConfigurationError> getAutoLoadingErrors()
Returns a list ofServiceConfigurationError
s that were encountered while loading automatically discovered linkers during the last invocation ofcreateLinker()
. They can be any non-Dynalink specific service configuration issues, as well as some Dynalink-specific errors when an exporter that the factory tried to automatically load:- did not have the runtime permission named
GuardingDynamicLinkerExporter.AUTOLOAD_PERMISSION_NAME
in a system with a security manager, or - returned null from
Supplier.get()
, or - the list returned from
Supplier.get()
had a null element.
- Returns:
- an immutable list of encountered
ServiceConfigurationError
s. Can be empty.
- did not have the runtime permission named
-
-