# Class DynamicLinkerFactory

java.lang.Object
jdk.dynalink.DynamicLinkerFactory

public final class DynamicLinkerFactory extends Object
A factory class for creating `DynamicLinker` objects. Dynamic linkers are the central objects in Dynalink; these are composed of several `GuardingDynamicLinker` objects and coordinate linking of call sites with them. The usual dynamic linker is a linker composed of all `GuardingDynamicLinker` objects explicitly pre-created by the user of the factory and configured with `setPrioritizedLinkers(List)`, as well as any `automatically discovered` ones, and finally the ones configured with `setFallbackLinkers(List)`; this last category usually includes `BeansLinker`.

Since:
  9

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `DynamicLinkerFactory()`
  Creates a new dynamic linker factory with default configuration.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `DynamicLinker`
  `createLinker()`
  Creates a new dynamic linker based on the current configuration.
  `List<ServiceConfigurationError>`
  `getAutoLoadingErrors()`
  Returns a list of `ServiceConfigurationError`s that were encountered while loading automatically discovered linkers during the last invocation of `createLinker()`.
  `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 of `LinkerServices.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 invoke `MutableCallSite.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.

  ### Methods declared in class Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`
  Modifier and Type
  Method
  Description
  `protected Object`
  `clone()`
  Creates and returns a copy of this object.
  `boolean`
  `equals(Object obj)`
  Indicates whether some other object is "equal to" this one.
  `protected void`
  `finalize()`
  Deprecated, for removal: This API element is subject to removal in a future version.
  Finalization is deprecated and subject to removal in a future release.
  `final Class<?>`
  `getClass()`
  Returns the runtime class of this `Object`.
  `int`
  `hashCode()`
  Returns a hash code value for this object.
  `final void`
  `notify()`
  Wakes up a single thread that is waiting on this object's monitor.
  `final void`
  `notifyAll()`
  Wakes up all threads that are waiting on this object's monitor.
  `String`
  `toString()`
  Returns a string representation of the object.
  `final void`
  `wait()`
  Causes the current thread to wait until it is awakened, typically by being notified or interrupted.
  `final void`
  `wait(long timeoutMillis)`
  Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.
  `final void`
  `wait(long timeoutMillis, int nanos)`
  Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.

- ## Constructor Details

  - ### DynamicLinkerFactory

    public DynamicLinkerFactory()
    Creates a new dynamic linker factory with default configuration. Upon creation, the factory can be configured using various `setXxx()` methods and used to create one or more dynamic linkers according to its current configuration using `createLinker()`.

- ## Method Details

  - ### 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 the `ServiceLoader` mechanism and the linkers they provide will be incorporated into `DynamicLinker`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 invoking `createLinker()` will be used. If this method is invoked explicitly with null then `ServiceLoader.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 calling `setPrioritizedLinkers(List)` with `Arrays.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 calling `setPrioritizedLinkers(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 of `BeansLinker` 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 calling `setFallbackLinkers(List)` with `Arrays.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 of `BeansLinker` 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 invoke `MutableCallSite.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 a `GuardedInvocationTransformer` 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 calls `GuardedInvocation.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. After `LinkerServices.asType(MethodHandle, MethodType)` has applied all custom conversions to a method handle, it still needs to effect `method invocation conversions` that can usually be automatically applied as per `MethodHandle.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 ordinary `MethodHandles.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's `MethodTypeConversionStrategy.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 final `MethodHandle.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 of `LinkerServices.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 invoke `getAutoLoadingErrors()` to retrieve a list of `ServiceConfigurationError`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 of `ServiceConfigurationError`s that were encountered while loading automatically discovered linkers during the last invocation of `createLinker()`. 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:
    - 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.