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
-
Converters and Converter Factories
- Add custom converters using addConverter or addPairedConverter.
- Support for generic and parameterized types via type inference.
- Add converter factories using addConverterFactory.
- Caching of converters for repeated conversions.
-
Null Source Handling
- Converts
nullto target types using registered GenericNullConverters. - Defaults to
_DefaultNullConverterif no custom converter exists.
- Converts
-
Conversion Caching
- Converter lookup results are cached to improve performance.
- Cache is invalidated whenever a new converter or factory is added.
-
URI Validation
- Maintains a list of
UriValidatorinstances for validating URI inputs.
- Maintains a list of
-
Type-Safety and Primitive Handling
- Automatically checks that source objects are instances of the expected type.
- Prevents assigning
nullto 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.
canConvertandcanBypassConvertallow pre-checking conversion capabilities._NoOpConverterand_NoMatchConverterare 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
UriValidatorto 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
sourceTypecan be converted totargetType.override -
convert<
T> (Object? source, Class< T> targetType, [String? qualifiedName]) → T? -
Converts the given
sourceobject to the specifiedtargetType.override -
convertTo<
T> (Object? source, Class targetType, [Class? sourceType]) → Object? -
Converts the
sourceobject from a knownsourceTypeto atargetTypeusing full type metadata viaClass.override -
getUriValidators(
) → List< UriValidator> -
Returns a list of registered
UriValidatorinstances.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