java.lang.Object
io.github.sugarcubes.cloner.Toolsets

public final class Toolsets extends Object
Profiles of Toolset: ready combinations of the low-level mechanisms.
  • Method Summary

    Modifier and Type
    Method
    Description
    Creates a builder for a custom toolset.
    static Toolset
    Conservative toolset, the same for every JVM: reflection, with Unsafe only for fields which reflection cannot access (packages not opened to the cloner); instances are created without constructors by sun.reflect.ReflectionFactory, or by Unsafe for classes which it refuses (classes which are not serializable on JDK 28+) and if the runtime has no jdk.unsupported module, constructors are the last fallback; packages are not opened.
    static Toolset
    Default toolset: the fastest mechanisms which the running JVM allows, chosen once per JVM.
    static boolean
    isAvailable(Allocation allocation)
    Tells whether the instance allocation mechanism is available in the running JVM.
    static boolean
    isAvailable(FieldAccess fieldAccess)
    Tells whether the field access mechanism is available in the running JVM.
    static boolean
    isAvailable(ModuleOpening moduleOpening)
    Tells whether the module opening mechanism is available in the running JVM.
    static Toolset
    Narcissus library for accessing fields: works for any package.
    static Toolset
    Reflection for fields, never Unsafe: fields of packages not opened to the cloner cannot be copied; on JDK 26+ final fields need --enable-final-field-mutation if the JVM denies their mutation.
    static Toolset
    Unsafe for creating instances and accessing fields: works for any package.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • defaults

      public static Toolset defaults()
      Default toolset: the fastest mechanisms which the running JVM allows, chosen once per JVM. Fields are accessed by Unsafe unless its memory access is denied, by reflection otherwise; instances are created by Unsafe or sun.reflect.ReflectionFactory without constructors, constructors are the last fallback; packages are opened by the cloner agent, Unsafe or Narcissus, whichever is available. Since JDK 24 the JVM warns when Unsafe accesses a field for the first time. Toolset.toString() tells the choice.

      conservative() is the fixed choice which does not depend on the JVM.

      Returns:
      default toolset
    • conservative

      public static Toolset conservative()
      Conservative toolset, the same for every JVM: reflection, with Unsafe only for fields which reflection cannot access (packages not opened to the cloner); instances are created without constructors by sun.reflect.ReflectionFactory, or by Unsafe for classes which it refuses (classes which are not serializable on JDK 28+) and if the runtime has no jdk.unsupported module, constructors are the last fallback; packages are not opened. The same as Toolsets.builder().build().
      Returns:
      conservative toolset
    • reflection

      public static Toolset reflection()
      Reflection for fields, never Unsafe: fields of packages not opened to the cloner cannot be copied; on JDK 26+ final fields need --enable-final-field-mutation if the JVM denies their mutation. Instances are created as by conservative().
      Returns:
      reflection toolset
    • unsafe

      public static Toolset unsafe()
      Unsafe for creating instances and accessing fields: works for any package. Since JDK 24 the JVM warns when Unsafe is used for the first time.
      Returns:
      Unsafe toolset
    • narcissus

      public static Toolset narcissus()
      Narcissus library for accessing fields: works for any package. Narcissus must be on the classpath.
      Returns:
      Narcissus toolset
    • builder

      public static ToolsetBuilder builder()
      Creates a builder for a custom toolset.
      Returns:
      toolset builder
    • isAvailable

      public static boolean isAvailable(Allocation allocation)
      Tells whether the instance allocation mechanism is available in the running JVM.
      Parameters:
      allocation - mechanism
      Returns:
      true if the mechanism can be used
    • isAvailable

      public static boolean isAvailable(FieldAccess fieldAccess)
      Tells whether the field access mechanism is available in the running JVM.
      Parameters:
      fieldAccess - mechanism
      Returns:
      true if the mechanism can be used
    • isAvailable

      public static boolean isAvailable(ModuleOpening moduleOpening)
      Tells whether the module opening mechanism is available in the running JVM.
      Parameters:
      moduleOpening - mechanism
      Returns:
      true if the mechanism can be used