DefaultClassLoader class

The default system implementation of ClassLoader with comprehensive caching.

Provides efficient class loading and caching for the JetLeaf reflection system. This implementation uses multiple specialized caches to optimize different types of reflection queries and maintains detailed statistics for monitoring.

Cache Architecture

The DefaultClassLoader employs a multi-tier caching strategy:

Primary Caches

  • Class Cache: Maps qualified names to Class instances
  • Subclass Cache: Maps parent classes to their direct classes
  • Interface Cache: Maps classes to their implemented interfaces
  • Mixin Cache: Maps classes to their applied mixins

Declared Caches (Non-transitive)

  • Declared Interface Cache: Direct interface implementations only
  • Declared Mixin Cache: Direct mixin applications only

Performance Characteristics

  • Class Loading: O(1) for cached, O(log n) for new classes
  • Hierarchy Queries: O(1) for cached relationships
  • Memory Usage: Configurable with automatic cleanup
  • Thread Safety: Full concurrent access support

Example Usage

// Create system class loader
final loader = DefaultClassLoader();

// Load classes with caching
final stringClass = await loader.loadClass<String>('dart:core/string.dart.String');
final listClass = await loader.loadClass<List>('dart:core/list.dart.List');

// Query relationships (cached)
final classes = await loader.findSubclasses(stringClass);
final interfaces = await loader.findInterfaces(listClass);

// Monitor performance
final stats = loader.getCacheStats();
print('Hit rate: ${stats.hitRate}');

// Cleanup
await loader.flush(); // Clear caches
await loader.close(); // Release resources

Configuration Options

The loader supports various configuration parameters:

  • Max Cache Size: Prevents unbounded memory growth
  • Auto-flush Threshold: Automatic cache cleanup triggers
  • Statistics Collection: Detailed performance monitoring
  • Security Domains: Fine-grained access control

Thread Safety

All operations are thread-safe and support concurrent access:

  • Multiple threads can load classes simultaneously
  • Cache updates are atomic and consistent
  • Statistics are accurately maintained under concurrency
  • Resource cleanup is coordinated across threads
Inheritance

Constructors

DefaultClassLoader()
The default system implementation of ClassLoader with comprehensive caching.

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

close() Future<void>
Closes the class loader and releases all resources.
override
extractComponentType(Class parentClass) Type?
Extract component type for arrays/lists
override
extractKeyType(Class parentClass) Type?
Extract key type for maps
override
findAllConstraintArguments(Class parentClass, [bool declared = true]) List<Class>
Retrieves all type arguments from all constraints applied to a class.
override
findAllConstraints(Class parentClass, [bool declared = true]) List<Class>
Retrieves all constraints applied to a class.
override
findAllInterfaceArguments(Class parentClass, [bool declared = true]) List<Class>
Finds all interface arguments of the specified class with caching.
override
findAllInterfaces(Class parentClass, [bool declared = true]) List<Class>
Finds all interfaces implemented by the specified class with caching.
override
findAllMixinArguments(Class parentClass, [bool declared = true]) List<Class>
Retrieves all type arguments from all mixins applied to a class.
override
findAllMixins(Class parentClass, [bool declared = true]) List<Class>
Finds all mixins applied to the specified class with caching.
override
findClass<T>(String className, [ProtectionDomain? domain]) Class<T>?
Finds and loads a class without caching (used internally by loadClass).
override
findComponentType<C>(Class parentClass, Type? component) Class<C>?
Finds the component type of a collection-like class.
override
findConstraintArguments<I>(Class parentClass, [bool declared = true]) List<Class>
Retrieves the type arguments for a specific constraint applied to a class.
override
findConstraints<I>(Class parentClass, [bool declared = true]) List<Class<I>>
Retrieves the constraints applied to a class.
override
findInterfaceArguments<I>(Class parentClass, [bool declared = true]) List<Class>
Finds all interface arguments of the specified class with caching.
override
findInterfaces<I>(Class parentClass, [bool declared = true]) List<Class<I>>
Finds all interfaces implemented by the specified class with caching.
override
findKeyType<K>(Class parentClass, Type? key) Class<K>?
Finds the key type of a map-like class.
override
findMixinArguments<I>(Class parentClass, [bool declared = true]) List<Class>
Retrieves the type arguments for a specific mixin applied to a class.
override
findMixins<I>(Class parentClass, [bool declared = true]) List<Class<I>>
Finds mixins applied to a class, returning them as typed Class instances.
override
findSubclasses(Class parentClass) List<Class>
Finds all direct subclasses of the specified class with caching.
override
findSuperClass(Class parentClass, [bool declared = true]) Class?
Finds the superclass of a given parentClass.
override
findSuperClassArguments(Class parentClass, [bool declared = true]) List<Class>
Finds the type arguments of the superclass of a given parentClass.
override
findSuperClassAs<S>(Class parentClass, [bool declared = true]) Class<S>?
Finds and casts the superclass of a given parentClass to a specific type.
override
findTypeParameters(Class parentClass) List<Class>
Finds the type parameters of a generic class.
override
flush() Future<void>
Flushes all caches, clearing cached class data while keeping the loader active.
override
getCacheStats() ClassLoaderStats
Gets cache statistics for monitoring and optimization.
override
isLoaded(String className) bool
Checks if a class is already loaded in the cache.
override
loadClass<T>(String className, [ProtectionDomain? domain]) Class<T>?
Loads a class by its fully qualified name with caching support.
override
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited

Operators

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