ClassLoader class abstract

An abstract class loader that manages the loading and caching of class metadata.

The ClassLoader serves as a centralized cache and management system for class reflection data, providing efficient access to class hierarchies, interfaces, mixins, and other type information. It implements both Closeable and Flushable interfaces to support proper resource management and cache invalidation.

Key Features

  • Hierarchical Caching: Maintains separate caches for classes, subclasses, interfaces, mixins, and their relationships
  • Memory Management: Provides flush and close operations for cache cleanup
  • Thread Safety: Ensures safe concurrent access to cached data
  • Performance Optimization: Reduces redundant reflection operations through intelligent caching strategies

Cache Categories

The ClassLoader maintains several specialized caches:

  • Class Cache: Primary class instances indexed by qualified name
  • Subclass Cache: Direct subclass relationships for inheritance traversal
  • Interface Cache: Implemented interfaces for each class
  • Declared Interface Cache: Directly declared interfaces (non-transitive)
  • Mixin Cache: Applied mixins for composition analysis
  • Declared Mixin Cache: Directly declared mixins (non-transitive)

Usage Patterns

// Get or create a class loader
final loader = SystemClassLoader();

// Load a class with caching
final userClass = await loader.loadClass<User>('com.example.User');

// Find related classes efficiently
final subclasses = await loader.findSubclasses(userClass);
final interfaces = await loader.findInterfaces(userClass);

// Clean up resources
await loader.flush(); // Clear caches
await loader.close(); // Release all resources

Implementation Requirements

Concrete implementations must provide:

  • Class loading strategy (loadClass, findClass)
  • Cache management policies (flush, close)
  • Resource cleanup procedures
  • Thread safety mechanisms

Memory Considerations

The ClassLoader maintains strong references to loaded classes and their metadata. Regular flushing is recommended for long-running applications to prevent memory leaks, especially when dealing with dynamic class loading scenarios.

Implemented types
Implementers

Constructors

ClassLoader()
An abstract class loader that manages the loading and caching of class metadata.

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
extractKeyType(Class parentClass) Type?
Extract key type for maps
findAllConstraintArguments(Class parentClass, [bool declared = true]) List<Class>
Retrieves all type arguments from all constraints applied to a class.
findAllConstraints(Class parentClass, [bool declared = true]) List<Class>
Retrieves all constraints applied to a class.
findAllInterfaceArguments(Class parentClass, [bool declared = true]) List<Class>
Finds all interface arguments of the specified class with caching.
findAllInterfaces(Class parentClass, [bool declared = true]) List<Class>
Finds all interfaces implemented by the specified class with caching.
findAllMixinArguments(Class parentClass, [bool declared = true]) List<Class>
Retrieves all type arguments from all mixins applied to a class.
findAllMixins(Class parentClass, [bool declared = true]) List<Class>
Finds all mixins applied to the specified class with caching.
findClass<T>(String className, [ProtectionDomain? domain]) Class<T>?
Finds and loads a class without caching (used internally by loadClass).
findComponentType<C>(Class parentClass, Type? component) Class<C>?
Finds the component type of a collection-like class.
findConstraintArguments<I>(Class parentClass, [bool declared = true]) List<Class>
Retrieves the type arguments for a specific constraint applied to a class.
findConstraints<I>(Class parentClass, [bool declared = true]) List<Class<I>>
Retrieves the constraints applied to a class.
findInterfaceArguments<I>(Class parentClass, [bool declared = true]) List<Class>
Finds all interface arguments of the specified class with caching.
findInterfaces<I>(Class parentClass, [bool declared = true]) List<Class<I>>
Finds all interfaces implemented by the specified class with caching.
findKeyType<K>(Class parentClass, Type? key) Class<K>?
Finds the key type of a map-like class.
findMixinArguments<I>(Class parentClass, [bool declared = true]) List<Class>
Retrieves the type arguments for a specific mixin applied to a class.
findMixins<I>(Class parentClass, [bool declared = true]) List<Class<I>>
Finds mixins applied to a class, returning them as typed Class instances.
findSubclasses(Class parentClass) List<Class>
Finds all direct subclasses of the specified class with caching.
findSuperClass(Class parentClass, [bool declared = true]) Class?
Finds the superclass of a given parentClass.
findSuperClassArguments(Class parentClass, [bool declared = true]) List<Class>
Finds the type arguments of the superclass of a given parentClass.
findSuperClassAs<S>(Class parentClass, [bool declared = true]) Class<S>?
Finds and casts the superclass of a given parentClass to a specific type.
findTypeParameters(Class parentClass) List<Class>
Finds the type parameters of a generic class.
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.
isLoaded(String className) bool
Checks if a class is already loaded in the cache.
loadClass<T>(String className, [ProtectionDomain? domain]) Class<T>?
Loads a class by its fully qualified name with caching support.
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