ResolvableType class
A comprehensive type resolution system for Dart that provides advanced type introspection, generic type handling, and assignability checking capabilities.
ResolvableType wraps Dart's Type system to provide enhanced functionality for:
- Generic type resolution and manipulation
- Type assignability checking with inheritance support
- Array and collection type handling
- Type variable resolution and bounds checking
- Integration with reflection and dependency injection systems
This class is particularly useful for frameworks that need to perform complex type operations at runtime, such as serialization libraries, dependency injection containers, and validation frameworks.
Basic Usage:
// Create ResolvableType for simple types
final stringType = ResolvableType.forClass(String);
final intType = ResolvableType.forClass(int);
// Work with generic types
final listType = ResolvableType.forClass(List<String>);
print(listType.hasGenerics()); // true
print(listType.getGeneric().resolve()?.getType()); // String
// Check type assignability
final objectType = ResolvableType.forClass(Object);
print(objectType.isAssignableFromType(String)); // true
// Handle arrays and collections
final arrayType = ResolvableType.forClass(List<int>);
print(arrayType.isArray()); // true
print(arrayType.getComponentType().resolve()?.getType()); // int
Advanced Usage:
// Create complex generic types
final mapType = ResolvableType.forClassWithGenerics(
Map,
[String, List<int>]
);
// Resolve nested generics
final nestedType = mapType.getGeneric([1]); // List<int>
final componentType = nestedType.getComponentType(); // int
// Type conversion and casting
final collectionType = listType.asCollection();
final mapAsCollection = mapType.asCollection();
// Instance checking
final myList = <String>['hello', 'world'];
print(listType.isInstance(myList)); // true
Properties
- hashCode → int
-
The hash code for this object.
no setteroverride
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
as(
Type type) → ResolvableType - Returns this type as a ResolvableType of the specified target type.
-
asCollection(
) → ResolvableType - Returns this type as a resolvable Iterable type.
-
asMap(
) → ResolvableType - Returns this type as a resolvable Map type.
-
asVariableResolver(
) → VariableResolver? - Adapts this ResolvableType to a VariableResolver for type variable resolution.
-
getComponentType(
) → ResolvableType - Returns the component type of an array or list type.
-
getGeneric(
[List< int> ? indexes]) → ResolvableType - Returns a ResolvableType representing the generic parameter for the given indexes.
-
getGenerics(
) → List< ResolvableType> - Returns an array of ResolvableTypes representing the generic parameters of this type.
-
getInterfaces(
) → List< ResolvableType> - Returns an array of ResolvableTypes representing the direct interfaces implemented by this type.
-
getKeyType(
) → ResolvableType - Returns the key type for Map-like types.
-
getNested(
int nestingLevel, [Map< int, int> ? typeIndexesPerLevel]) → ResolvableType - Returns a ResolvableType for the specified nesting level.
-
getRawClass(
) → Class? -
getSource(
) → Object -
getSuperType(
) → ResolvableType - Returns a ResolvableType representing the direct supertype of this type.
-
getType(
) → Type -
hasGenerics(
) → bool - Returns true if this type contains generic parameters.
-
hasResolvableGenerics(
) → bool - Returns true if this type contains at least one generic type that can be resolved.
-
hasUnresolvableGenerics(
) → bool - Determines whether the underlying type has any unresolvable generics.
-
isArray(
) → bool - Determines whether this ResolvableType represents an array or list type.
-
isAssignableFrom(
ResolvableType other, bool strict, Map< Type, Type> ? matchedBefore, bool upUntilUnresolvable) → bool -
isAssignableFromResolvable(
ResolvableType other) → bool - Determines whether this ResolvableType is assignable from another ResolvableType.
-
isAssignableFromResolvedPart(
ResolvableType other) → bool - Determines assignability from another ResolvableType up to unresolvable parts.
-
isAssignableFromType(
Type other) → bool - Determines whether this ResolvableType is assignable from the specified Type.
-
isInstance(
Object? obj) → bool -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
resolve(
[Class? fallback]) → Class? - Resolves this ResolvableType to a Class, returning null if the type cannot be resolved.
-
resolveGeneric(
[List< int> ? indexes]) → Class? - Convenience method that resolves a specific generic parameter to a Class.
-
resolveGenerics(
) → List< Class?> - Convenience method that resolves all generic parameters to Class instances.
-
resolveGenericsWithFallback(
Class fallback) → List< Class> - Convenience method that resolves generic parameters with a fallback for unresolvable types.
-
resolveType(
) → ResolvableType - Resolves this type by a single level, returning the resolved value or NONE.
-
toClass(
) → Class? -
toString(
) → String -
A string representation of this object.
override
Operators
-
operator ==(
Object other) → bool -
The equality operator.
override
Static Properties
- NONE → ResolvableType
-
A special ResolvableType instance representing an unresolvable or empty type.
final
Static Methods
-
clearCache(
) → void - Clears the internal ResolvableType cache.
-
forArrayComponent(
ResolvableType componentType) → ResolvableType - Returns a ResolvableType representing an array of the specified component type.
-
forClass(
Type type) → ResolvableType -
forClassWithGenerics(
Type clazz, List< Type> generics) → ResolvableType -
forClassWithImplementation(
Type baseType, Type implementationClass) → ResolvableType -
forClassWithResolvableGenerics(
Type clazz, List< ResolvableType> generics) → ResolvableType -
forConstructorParameter(
ConstructorDeclaration constructor, int parameterIndex) → ResolvableType -
forConstructorParameterWithImplementation(
ConstructorDeclaration constructor, int parameterIndex, Type implementationClass) → ResolvableType - Creates a ResolvableType for a constructor parameter with implementation context.
-
forField(
FieldDeclaration field) → ResolvableType -
forFieldWithImplementation(
FieldDeclaration field, Type implementationClass) → ResolvableType -
forFieldWithResolvableImplementation(
FieldDeclaration field, ResolvableType? implementationType) → ResolvableType -
forInstance(
Object? instance) → ResolvableType -
forMethodParameter(
MethodDeclaration method, int parameterIndex) → ResolvableType - Creates a ResolvableType for a method parameter.
-
forMethodParameterWithImplementation(
MethodDeclaration method, int parameterIndex, Type implementationClass) → ResolvableType - Creates a ResolvableType for a method parameter with implementation context.
-
forMethodReturnType(
MethodDeclaration method) → ResolvableType - Creates a ResolvableType for a method's return type.
-
forMethodReturnTypeWithImplementation(
MethodDeclaration method, Type implementationClass) → ResolvableType - Creates a ResolvableType for a method's return type with implementation context.
-
forRawClass(
Type clazz) → ResolvableType - Returns a ResolvableType for the specified Class doing assignability checks against the raw class only.
-
forType(
Type type, [ResolvableType? owner]) → ResolvableType - Returns a ResolvableType for the specified Type with optional owner context.
-
forTypeWithProviderAndResolver(
Type type, TypeProvider? typeProvider, VariableResolver? variableResolver) → ResolvableType - Returns a ResolvableType with both TypeProvider and VariableResolver.
-
forTypeWithVariableResolver(
Type type, VariableResolver? variableResolver) → ResolvableType - Returns a ResolvableType for the specified Type backed by a VariableResolver.