loadClass<T> method

  1. @override
Class<T>? loadClass<T>(
  1. String className, [
  2. ProtectionDomain? domain
])
override

Loads a class by its fully qualified name with caching support.

Type Parameters:

  • T: The expected class type for type safety

Parameters:

  • className: The fully qualified class name (e.g., 'dart:core/string.dart.String')
  • domain: Optional protection domain for security context

Returns:

  • A cached or newly loaded Class<T> instance
  • null if the class cannot be found or loaded

Caching Behavior

  • First checks the primary class cache for existing instances
  • If not cached, delegates to findClass for loading
  • Caches successful results for future requests
  • Updates related caches (subclasses, interfaces, mixins)

Example

// Load with type safety
final stringClass = await loader.loadClass<String>('dart:core/string.dart.String');

// Load with custom domain
final userClass = await loader.loadClass<User>(
  'com.example.User',
  ProtectionDomain.application()
);

Performance Notes

  • Subsequent calls for the same class return cached instances
  • Cache lookups are O(1) for qualified name access
  • Related type information is pre-computed and cached

Throws:

Implementation

@override
Class<T>? loadClass<T>(String className, [ProtectionDomain? domain]) {
  _checkClosed();

  // Check cache first
  final cached = _classCache[className];
  if (cached != null) {
    _hitCount++;
    return cached as Class<T>;
  }

  _missCount++;

  // Load class and measure time
  final stopwatch = Stopwatch()..start();
  try {
    final clazz = findClass<T>(className, domain);
    stopwatch.stop();
    _loadTimes.add(stopwatch.elapsedMilliseconds);

    if (clazz != null) {
      // Cache the result
      _classCache[className] = clazz;
      _successfulLoads++;

      return clazz;
    } else {
      _failedLoads++;
      return null;
    }
  } catch (e) {
    stopwatch.stop();
    _failedLoads++;
    rethrow;
  }
}