refactor: refactoring mirrors further

This commit is contained in:
Patrick Stewart 2024-12-25 15:15:51 -07:00
parent a79809f46b
commit d0ff4db4a0
6 changed files with 35 additions and 33 deletions

View file

@ -13,8 +13,9 @@ export 'src/core/mirror_system.dart';
/// Discovery /// Discovery
export 'src/discovery/type_analyzer.dart'; export 'src/discovery/type_analyzer.dart';
export 'src/discovery/library_scanner.dart'; export 'src/discovery/library_analyzer.dart';
export 'src/discovery/scanner.dart'; export 'src/discovery/runtime_library_discoverer.dart';
export 'src/discovery/runtime_type_discoverer.dart';
/// Discovery Models /// Discovery Models
export 'src/discovery/models/models.dart'; export 'src/discovery/models/models.dart';

View file

@ -1,27 +1,6 @@
import 'dart:core';
import 'package:platform_contracts/contracts.dart'; import 'package:platform_contracts/contracts.dart';
import 'package:platform_mirrors/mirrors.dart'; import 'package:platform_mirrors/mirrors.dart';
/// Runtime scanner that analyzes libraries and extracts their metadata.
class LibraryScanner {
// Private constructor to prevent instantiation
LibraryScanner._();
// Cache for library metadata
static final Map<Uri, LibraryInfo> _libraryCache = {};
/// Scans a library and extracts its metadata.
static LibraryInfo scanLibrary(Uri uri) {
if (_libraryCache.containsKey(uri)) {
return _libraryCache[uri]!;
}
final libraryInfo = LibraryAnalyzer.analyze(uri);
_libraryCache[uri] = libraryInfo;
return libraryInfo;
}
}
/// Analyzes libraries at runtime to extract their metadata. /// Analyzes libraries at runtime to extract their metadata.
class LibraryAnalyzer { class LibraryAnalyzer {
// Private constructor to prevent instantiation // Private constructor to prevent instantiation

View file

@ -0,0 +1,22 @@
import 'dart:core';
import 'package:platform_mirrors/mirrors.dart';
/// Runtime scanner that analyzes libraries and extracts their metadata.
class RuntimeLibraryDiscoverer {
// Private constructor to prevent instantiation
RuntimeLibraryDiscoverer._();
// Cache for library metadata
static final Map<Uri, LibraryInfo> _libraryCache = {};
/// Scans a library and extracts its metadata.
static LibraryInfo scanLibrary(Uri uri) {
if (_libraryCache.containsKey(uri)) {
return _libraryCache[uri]!;
}
final libraryInfo = LibraryAnalyzer.analyze(uri);
_libraryCache[uri] = libraryInfo;
return libraryInfo;
}
}

View file

@ -2,9 +2,9 @@ import 'dart:core';
import 'package:platform_mirrors/mirrors.dart'; import 'package:platform_mirrors/mirrors.dart';
/// Runtime scanner that analyzes types and extracts their metadata. /// Runtime scanner that analyzes types and extracts their metadata.
class Scanner { class RuntimeTypeDiscoverer {
// Private constructor to prevent instantiation // Private constructor to prevent instantiation
Scanner._(); RuntimeTypeDiscoverer._();
// Cache for type metadata // Cache for type metadata
static final Map<Type, TypeMetadata> _typeCache = {}; static final Map<Type, TypeMetadata> _typeCache = {};

View file

@ -37,7 +37,7 @@ class LibraryMirror extends TypedMirror implements LibraryMirrorContract {
List<InstanceMirrorContract> metadata = const [], List<InstanceMirrorContract> metadata = const [],
}) { }) {
// Scan library to get declarations // Scan library to get declarations
final libraryInfo = LibraryScanner.scanLibrary(uri); final libraryInfo = RuntimeLibraryDiscoverer.scanLibrary(uri);
final declarations = <Symbol, DeclarationMirrorContract>{}; final declarations = <Symbol, DeclarationMirrorContract>{};
final topLevelValues = <Symbol, dynamic>{}; final topLevelValues = <Symbol, dynamic>{};

View file

@ -78,7 +78,7 @@ void main() {
isWritable: false); isWritable: false);
// Scan type // Scan type
Scanner.scanType(TestClass); RuntimeTypeDiscoverer.scanType(TestClass);
final metadata = ReflectionRegistry.getPropertyMetadata(TestClass); final metadata = ReflectionRegistry.getPropertyMetadata(TestClass);
expect(metadata, isNotNull); expect(metadata, isNotNull);
@ -130,7 +130,7 @@ void main() {
); );
// Scan type // Scan type
Scanner.scanType(TestClass); RuntimeTypeDiscoverer.scanType(TestClass);
final metadata = ReflectionRegistry.getMethodMetadata(TestClass); final metadata = ReflectionRegistry.getMethodMetadata(TestClass);
expect(metadata, isNotNull); expect(metadata, isNotNull);
@ -187,7 +187,7 @@ void main() {
); );
// Scan type // Scan type
Scanner.scanType(TestClass); RuntimeTypeDiscoverer.scanType(TestClass);
final metadata = ReflectionRegistry.getConstructorMetadata(TestClass); final metadata = ReflectionRegistry.getConstructorMetadata(TestClass);
expect(metadata, isNotNull); expect(metadata, isNotNull);
@ -255,7 +255,7 @@ void main() {
); );
// Scan type // Scan type
Scanner.scanType(TestClass); RuntimeTypeDiscoverer.scanType(TestClass);
final reflector = RuntimeReflector.instance; final reflector = RuntimeReflector.instance;
@ -320,7 +320,7 @@ void main() {
); );
// Scan type // Scan type
Scanner.scanType(GenericTestClass); RuntimeTypeDiscoverer.scanType(GenericTestClass);
final metadata = ReflectionRegistry.getPropertyMetadata(GenericTestClass); final metadata = ReflectionRegistry.getPropertyMetadata(GenericTestClass);
expect(metadata, isNotNull); expect(metadata, isNotNull);
@ -364,8 +364,8 @@ void main() {
); );
// Scan types // Scan types
Scanner.scanType(ParentTestClass); RuntimeTypeDiscoverer.scanType(ParentTestClass);
Scanner.scanType(ChildTestClass); RuntimeTypeDiscoverer.scanType(ChildTestClass);
final parentMeta = final parentMeta =
ReflectionRegistry.getPropertyMetadata(ParentTestClass); ReflectionRegistry.getPropertyMetadata(ParentTestClass);