ClassLoader constructor
ClassLoader()
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.
Implementation
ClassLoader();