SimpleConversionService class

A configurable and extensible conversion service that provides a central mechanism for converting between types in JetLeaf applications.

SimpleConversionService supports:

  • Generic type conversion with caching
  • Paired converters and converter factories
  • Null source handling
  • URI validation
  • Optional protection domains for security/validation contexts

This service is intended to be subclassed (e.g., DefaultConversionService) or used directly for applications that require custom conversion behavior.

Features

  1. Converters and Converter Factories

  2. Null Source Handling

    • Converts null to target types using registered GenericNullConverters.
    • Defaults to _DefaultNullConverter if no custom converter exists.
  3. Conversion Caching

    • Converter lookup results are cached to improve performance.
    • Cache is invalidated whenever a new converter or factory is added.
  4. URI Validation

    • Maintains a list of UriValidator instances for validating URI inputs.
  5. Type-Safety and Primitive Handling

    • Automatically checks that source objects are instances of the expected type.
    • Prevents assigning null to primitive target types.

Example

final service = SimpleConversionService();

// Add a custom converter
service.addConverter<String, int>(MyStringToIntConverter(), sourceType: Class.forType(String), targetType: Class.forType(int));

// Convert a value
int result = service.convert('123', Class.forType(int))!;

// Convert a nullable source
int? nullableResult = service.convert<int>(null, Class.forType(int)); // throws ConversionFailedException if int is primitive

Notes

  • Internal caching ensures repeated conversions are fast.
  • canConvert and canBypassConvert allow pre-checking conversion capabilities.
  • _NoOpConverter and _NoMatchConverter are used internally for cache markers and bypassing conversions when source and target types are compatible.
Implemented types
Implementers

Constructors

SimpleConversionService([ProtectionDomain? pd])
Creates a new SimpleConversionService.

Properties

hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

addConverter<S, T>(Converter<S, T> converter, {Class<S>? sourceType, Class<T>? targetType}) → void
Registers a simple Converter from one specific source type to one target type.
override
addConverterFactory(ConverterFactory factory) → void
Registers a ConverterFactory that can produce Converter instances for a target type.
override
addNullSourceConverter(GenericNullConverter converter) → void
Adds a new GenericNullConverter to the list of registered converters.
override
addPairedConverter(PairedConverter converter) → void
Registers a PairedConverter capable of converting between multiple types.
override
addUriValidator(UriValidator validator) → void
Adds a new UriValidator to the list of registered validators.
override
canBypassConvert(Class? sourceType, Class targetType) bool
Return whether conversion between the source type and the target type can be bypassed. More precisely, this method will return true if objects of sourceType can be converted to the target type by returning the source object unchanged.
canConvert(Class? sourceType, Class targetType) bool
Determines whether objects of sourceType can be converted to targetType.
override
convert<T>(Object? source, Class<T> targetType, [String? qualifiedName]) → T?
Converts the given source object to the specified targetType.
override
convertTo<T>(Object? source, Class targetType, [Class? sourceType]) Object?
Converts the source object from a known sourceType to a targetType using full type metadata via Class.
override
getUriValidators() List<UriValidator>
Returns a list of registered UriValidator instances.
override
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
remove(Class sourceType, Class targetType) → void
Removes a registered converter between the specified source and target types.
override
removeNullSourceConverter(GenericNullConverter converter) → void
Removes a registered GenericNullConverter.
override
removeUriValidator(UriValidator validator) → void
Removes a registered UriValidator.
override
toString() String
A string representation of this object.
override

Operators

operator ==(Object other) bool
The equality operator.
inherited