java.lang.Object
io.github.sugarcubes.cloner.Toolsets
Profiles of
Toolset: ready combinations of the low-level mechanisms.-
Method Summary
Modifier and TypeMethodDescriptionstatic ToolsetBuilderbuilder()Creates a builder for a custom toolset.static ToolsetConservative toolset, the same for every JVM: reflection, withUnsafeonly for fields which reflection cannot access (packages not opened to the cloner); instances are created without constructors bysun.reflect.ReflectionFactory, or byUnsafefor classes which it refuses (classes which are not serializable on JDK 28+) and if the runtime has nojdk.unsupportedmodule, constructors are the last fallback; packages are not opened.static Toolsetdefaults()Default toolset: the fastest mechanisms which the running JVM allows, chosen once per JVM.static booleanisAvailable(Allocation allocation) Tells whether the instance allocation mechanism is available in the running JVM.static booleanisAvailable(FieldAccess fieldAccess) Tells whether the field access mechanism is available in the running JVM.static booleanisAvailable(ModuleOpening moduleOpening) Tells whether the module opening mechanism is available in the running JVM.static ToolsetNarcissus library for accessing fields: works for any package.static ToolsetReflection for fields, neverUnsafe: fields of packages not opened to the cloner cannot be copied; on JDK 26+ final fields need--enable-final-field-mutationif the JVM denies their mutation.static Toolsetunsafe()Unsafefor creating instances and accessing fields: works for any package.
-
Method Details
-
defaults
Default toolset: the fastest mechanisms which the running JVM allows, chosen once per JVM. Fields are accessed byUnsafeunless its memory access is denied, by reflection otherwise; instances are created byUnsafeorsun.reflect.ReflectionFactorywithout constructors, constructors are the last fallback; packages are opened by the cloner agent,Unsafeor Narcissus, whichever is available. Since JDK 24 the JVM warns whenUnsafeaccesses 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
Conservative toolset, the same for every JVM: reflection, withUnsafeonly for fields which reflection cannot access (packages not opened to the cloner); instances are created without constructors bysun.reflect.ReflectionFactory, or byUnsafefor classes which it refuses (classes which are not serializable on JDK 28+) and if the runtime has nojdk.unsupportedmodule, constructors are the last fallback; packages are not opened. The same asToolsets.builder().build().- Returns:
- conservative toolset
-
reflection
Reflection for fields, neverUnsafe: fields of packages not opened to the cloner cannot be copied; on JDK 26+ final fields need--enable-final-field-mutationif the JVM denies their mutation. Instances are created as byconservative().- Returns:
- reflection toolset
-
unsafe
Unsafefor creating instances and accessing fields: works for any package. Since JDK 24 the JVM warns whenUnsafeis used for the first time.- Returns:
- Unsafe toolset
-
narcissus
Narcissus library for accessing fields: works for any package. Narcissus must be on the classpath.- Returns:
- Narcissus toolset
-
builder
Creates a builder for a custom toolset.- Returns:
- toolset builder
-
isAvailable
Tells whether the instance allocation mechanism is available in the running JVM.- Parameters:
allocation- mechanism- Returns:
trueif the mechanism can be used
-
isAvailable
Tells whether the field access mechanism is available in the running JVM.- Parameters:
fieldAccess- mechanism- Returns:
trueif the mechanism can be used
-
isAvailable
Tells whether the module opening mechanism is available in the running JVM.- Parameters:
moduleOpening- mechanism- Returns:
trueif the mechanism can be used
-