From 14e1fc099d259c66c0341f06f739f0ec65df002b Mon Sep 17 00:00:00 2001 From: Patrick Stewart Date: Sat, 21 Dec 2024 09:53:25 -0700 Subject: [PATCH] refactor: refactoring reflection system pass 43 fail 1 --- .../example/reflection_example.dart | 2 +- incubation/reflection/lib/mirrors.dart | 60 ++++ incubation/reflection/lib/reflection.dart | 18 - .../reflection/lib/src/annotations.dart | 2 +- .../lib/src/core/library_scanner.dart | 13 +- .../reflection/lib/src/core/reflector.dart | 5 +- .../lib/src/core/runtime_reflector.dart | 108 +++--- .../reflection/lib/src/core/scanner.dart | 9 +- incubation/reflection/lib/src/metadata.dart | 2 +- .../reflection/lib/src/mirror_system.dart | 59 ++-- incubation/reflection/lib/src/mirrors.dart | 61 ---- .../lib/src/mirrors/base_mirror.dart | 18 +- ...ass_mirror_impl.dart => class_mirror.dart} | 54 ++- ...irror_impl.dart => combinator_mirror.dart} | 10 +- ..._mirror_impl.dart => instance_mirror.dart} | 53 ++- ...e_mirror_impl.dart => isolate_mirror.dart} | 27 +- ...pl.dart => library_dependency_mirror.dart} | 29 +- ...y_mirror_impl.dart => library_mirror.dart} | 97 +++--- ...od_mirror_impl.dart => method_mirror.dart} | 29 +- .../lib/src/mirrors/mirror_system.dart | 323 ++++++++++++++++++ .../reflection/lib/src/mirrors/mirrors.dart | 15 - ...mirror_impl.dart => parameter_mirror.dart} | 35 +- .../{mirror_system_impl.dart => tem2.dart} | 106 +++--- .../reflection/lib/src/mirrors/temp.dart | 315 +++++++++++++++++ .../reflection/lib/src/mirrors/temp3.dart | 318 +++++++++++++++++ ...type_mirror_impl.dart => type_mirror.dart} | 81 +++-- ...or_impl.dart => type_variable_mirror.dart} | 39 +-- ..._mirror_impl.dart => variable_mirror.dart} | 40 +-- incubation/reflection/lib/src/types.dart | 19 -- .../test/isolate_reflection_test.dart | 6 +- .../test/library_reflection_test.dart | 8 +- .../reflection/test/mirror_system_test.dart | 11 +- .../reflection/test/reflection_test.dart | 7 +- incubation/reflection/test/scanner_test.dart | 2 +- .../src/higher_order_collection_proxy.dart | 2 +- .../higher_order_collection_proxy_test.dart | 2 +- .../contracts/lib/src/reflection/base.dart | 124 ++++--- .../src/reflection/reflector_contract.dart | 9 +- packages/macroable/lib/src/macroable.dart | 2 +- packages/macroable/test/macroable_test.dart | 2 +- packages/mirrors/.gitignore | 7 + packages/mirrors/CHANGELOG.md | 3 + packages/mirrors/LICENSE.md | 10 + packages/mirrors/README.md | 1 + packages/mirrors/analysis_options.yaml | 30 ++ packages/mirrors/doc/.gitkeep | 0 packages/mirrors/example/.gitkeep | 0 packages/mirrors/lib/src/.gitkeep | 0 packages/mirrors/pubspec.yaml | 20 ++ packages/mirrors/test/.gitkeep | 0 .../lib/src/higher_order_tap_proxy.dart | 2 +- packages/support/lib/src/onceable.dart | 2 +- packages/support/lib/src/optional.dart | 2 +- packages/support/lib/src/reflector.dart | 18 +- .../lib/src/traits/forwards_calls.dart | 2 +- .../lib/src/traits/reflects_closures.dart | 2 +- .../test/higher_order_tap_proxy_test.dart | 2 - packages/support/test/onceable_test.dart | 2 +- .../test/support_forwards_calls_test.dart | 2 +- .../support/test/support_optional_test.dart | 2 +- .../support/test/support_reflector_test.dart | 54 ++- .../support/test/support_tappable_test.dart | 2 +- .../test/traits/reflects_closures_test.dart | 2 +- 63 files changed, 1621 insertions(+), 666 deletions(-) create mode 100644 incubation/reflection/lib/mirrors.dart delete mode 100644 incubation/reflection/lib/reflection.dart delete mode 100644 incubation/reflection/lib/src/mirrors.dart rename incubation/reflection/lib/src/mirrors/{class_mirror_impl.dart => class_mirror.dart} (82%) rename incubation/reflection/lib/src/mirrors/{combinator_mirror_impl.dart => combinator_mirror.dart} (71%) rename incubation/reflection/lib/src/mirrors/{instance_mirror_impl.dart => instance_mirror.dart} (85%) rename incubation/reflection/lib/src/mirrors/{isolate_mirror_impl.dart => isolate_mirror.dart} (84%) rename incubation/reflection/lib/src/mirrors/{library_dependency_mirror_impl.dart => library_dependency_mirror.dart} (64%) rename incubation/reflection/lib/src/mirrors/{library_mirror_impl.dart => library_mirror.dart} (76%) rename incubation/reflection/lib/src/mirrors/{method_mirror_impl.dart => method_mirror.dart} (83%) create mode 100644 incubation/reflection/lib/src/mirrors/mirror_system.dart delete mode 100644 incubation/reflection/lib/src/mirrors/mirrors.dart rename incubation/reflection/lib/src/mirrors/{parameter_mirror_impl.dart => parameter_mirror.dart} (73%) rename incubation/reflection/lib/src/mirrors/{mirror_system_impl.dart => tem2.dart} (67%) create mode 100644 incubation/reflection/lib/src/mirrors/temp.dart create mode 100644 incubation/reflection/lib/src/mirrors/temp3.dart rename incubation/reflection/lib/src/mirrors/{type_mirror_impl.dart => type_mirror.dart} (77%) rename incubation/reflection/lib/src/mirrors/{type_variable_mirror_impl.dart => type_variable_mirror.dart} (59%) rename incubation/reflection/lib/src/mirrors/{variable_mirror_impl.dart => variable_mirror.dart} (75%) delete mode 100644 incubation/reflection/lib/src/types.dart create mode 100644 packages/mirrors/.gitignore create mode 100644 packages/mirrors/CHANGELOG.md create mode 100644 packages/mirrors/LICENSE.md create mode 100644 packages/mirrors/README.md create mode 100644 packages/mirrors/analysis_options.yaml create mode 100644 packages/mirrors/doc/.gitkeep create mode 100644 packages/mirrors/example/.gitkeep create mode 100644 packages/mirrors/lib/src/.gitkeep create mode 100644 packages/mirrors/pubspec.yaml create mode 100644 packages/mirrors/test/.gitkeep diff --git a/incubation/reflection/example/reflection_example.dart b/incubation/reflection/example/reflection_example.dart index 68c16dd..b73faea 100644 --- a/incubation/reflection/example/reflection_example.dart +++ b/incubation/reflection/example/reflection_example.dart @@ -1,4 +1,4 @@ -import 'package:platform_reflection/reflection.dart'; +import 'package:platform_reflection/mirrors.dart'; // Custom annotation to demonstrate metadata class Validate { diff --git a/incubation/reflection/lib/mirrors.dart b/incubation/reflection/lib/mirrors.dart new file mode 100644 index 0000000..7128686 --- /dev/null +++ b/incubation/reflection/lib/mirrors.dart @@ -0,0 +1,60 @@ +library mirrors; + +import 'package:platform_contracts/contracts.dart'; + +import 'src/mirrors/mirror_system.dart'; +import 'src/mirrors/instance_mirror.dart'; + +/// Core +export 'src/core/library_scanner.dart'; +export 'src/core/reflector.dart'; +export 'src/core/runtime_reflector.dart'; +export 'src/core/scanner.dart'; + +/// MirrorSystem +export 'src/mirrors/mirror_system.dart'; + +/// Mirrors +export 'src/mirrors/base_mirror.dart'; +export 'src/mirrors/class_mirror.dart'; +export 'src/mirrors/combinator_mirror.dart'; +export 'src/mirrors/instance_mirror.dart'; +export 'src/mirrors/isolate_mirror.dart'; +export 'src/mirrors/library_dependency_mirror.dart'; +export 'src/mirrors/library_mirror.dart'; +export 'src/mirrors/method_mirror.dart'; +export 'src/mirrors/parameter_mirror.dart'; +export 'src/mirrors/type_mirror.dart'; +export 'src/mirrors/type_variable_mirror.dart'; +export 'src/mirrors/variable_mirror.dart'; + +/// Types +export 'src/mirrors/special_types.dart'; + +/// Metadata and Annotations +export 'src/annotations.dart'; +export 'src/metadata.dart'; + +/// Exceptions +export 'src/exceptions.dart'; + +/// Reflects an instance. +InstanceMirrorContract reflect(dynamic reflectee) { + return InstanceMirror( + reflectee: reflectee, + type: reflectClass(reflectee.runtimeType), + ); +} + +/// Reflects a class. +ClassMirrorContract reflectClass(Type key) { + return MirrorSystem.instance.reflectClass(key); +} + +/// Reflects a type. +TypeMirrorContract reflectType(Type key) { + return MirrorSystem.instance.reflectType(key); +} + +/// Returns the current mirror system. +MirrorSystemContract currentMirrorSystem() => MirrorSystem.current(); diff --git a/incubation/reflection/lib/reflection.dart b/incubation/reflection/lib/reflection.dart deleted file mode 100644 index bdbb0f9..0000000 --- a/incubation/reflection/lib/reflection.dart +++ /dev/null @@ -1,18 +0,0 @@ -/// A lightweight, cross-platform reflection system for Dart. -library reflection; - -// Core functionality -export 'src/core/reflector.dart'; -export 'src/core/scanner.dart'; -export 'src/core/runtime_reflector.dart'; - -// Mirror API -export 'src/mirrors.dart'; -export 'src/mirrors/isolate_mirror_impl.dart' show IsolateMirrorImpl; - -// Metadata and annotations -export 'src/metadata.dart'; -export 'src/annotations.dart' show reflectable; - -// Exceptions -export 'src/exceptions.dart'; diff --git a/incubation/reflection/lib/src/annotations.dart b/incubation/reflection/lib/src/annotations.dart index 1f89633..19e9c10 100644 --- a/incubation/reflection/lib/src/annotations.dart +++ b/incubation/reflection/lib/src/annotations.dart @@ -1,4 +1,4 @@ -import 'metadata.dart'; +import 'package:platform_reflection/mirrors.dart'; /// Registry of reflectable types and their metadata. class ReflectionRegistry { diff --git a/incubation/reflection/lib/src/core/library_scanner.dart b/incubation/reflection/lib/src/core/library_scanner.dart index f1439f4..0468b1c 100644 --- a/incubation/reflection/lib/src/core/library_scanner.dart +++ b/incubation/reflection/lib/src/core/library_scanner.dart @@ -1,9 +1,6 @@ import 'dart:core'; -import '../metadata.dart'; -import '../mirrors.dart'; -import '../mirrors/mirror_system_impl.dart'; -import '../mirrors/special_types.dart'; -import '../exceptions.dart'; +import 'package:platform_contracts/contracts.dart'; +import 'package:platform_reflection/mirrors.dart'; /// Runtime scanner that analyzes libraries and extracts their metadata. class LibraryScanner { @@ -89,7 +86,7 @@ class LibraryAnalyzer { ), ], returnsVoid: false, - returnType: InstanceMirror, + returnType: InstanceMirrorContract, isPrivate: false, ), FunctionInfo( @@ -104,7 +101,7 @@ class LibraryAnalyzer { ), ], returnsVoid: false, - returnType: ClassMirror, + returnType: ClassMirrorContract, isPrivate: false, ), ]); @@ -112,7 +109,7 @@ class LibraryAnalyzer { variables.addAll([ VariableInfo( name: 'currentMirrorSystem', - type: MirrorSystem, + type: MirrorSystemContract, isFinal: true, isConst: false, isPrivate: false, diff --git a/incubation/reflection/lib/src/core/reflector.dart b/incubation/reflection/lib/src/core/reflector.dart index a10a93d..fa01778 100644 --- a/incubation/reflection/lib/src/core/reflector.dart +++ b/incubation/reflection/lib/src/core/reflector.dart @@ -1,8 +1,5 @@ import 'dart:collection'; -import '../metadata.dart'; -import '../mirrors.dart'; -import '../mirrors/mirrors.dart'; -import '../mirrors/special_types.dart'; +import 'package:platform_reflection/mirrors.dart'; /// Static registry for reflection metadata. class Reflector { diff --git a/incubation/reflection/lib/src/core/runtime_reflector.dart b/incubation/reflection/lib/src/core/runtime_reflector.dart index a2e4c19..be3261e 100644 --- a/incubation/reflection/lib/src/core/runtime_reflector.dart +++ b/incubation/reflection/lib/src/core/runtime_reflector.dart @@ -1,22 +1,6 @@ -import 'package:meta/meta.dart'; import 'dart:isolate' as isolate; -import '../exceptions.dart'; -import '../metadata.dart'; -import '../mirrors.dart'; -import 'reflector.dart'; -import '../mirrors/base_mirror.dart'; -import '../mirrors/class_mirror_impl.dart'; -import '../mirrors/instance_mirror_impl.dart'; -import '../mirrors/method_mirror_impl.dart'; -import '../mirrors/parameter_mirror_impl.dart'; -import '../mirrors/type_mirror_impl.dart'; -import '../mirrors/type_variable_mirror_impl.dart'; -import '../mirrors/variable_mirror_impl.dart'; -import '../mirrors/library_mirror_impl.dart'; -import '../mirrors/library_dependency_mirror_impl.dart'; -import '../mirrors/isolate_mirror_impl.dart'; -import '../mirrors/mirror_system_impl.dart'; -import '../mirrors/special_types.dart'; +import 'package:platform_contracts/contracts.dart'; +import 'package:platform_reflection/mirrors.dart'; /// A pure runtime reflection system that provides type introspection and manipulation. class RuntimeReflector { @@ -24,26 +8,26 @@ class RuntimeReflector { static final instance = RuntimeReflector._(); /// The current mirror system. - late final MirrorSystemImpl _mirrorSystem; + late final MirrorSystem _mirrorSystem; /// Cache of class mirrors to prevent infinite recursion - final Map _classMirrorCache = {}; + final Map _classMirrorCache = {}; RuntimeReflector._() { // Initialize mirror system - _mirrorSystem = MirrorSystemImpl.current(); + _mirrorSystem = MirrorSystem.current(); } /// Resolves parameters for method or constructor invocation List resolveParameters( - List parameters, + List parameters, List positionalArgs, Map? namedArgs, ) { final resolvedArgs = List.filled(parameters.length, null); var positionalIndex = 0; - ClassMirror? _getClassMirror(Type? type) { + ClassMirrorContract? _getClassMirror(Type? type) { if (type == null) return null; try { return reflectClass(type); @@ -52,7 +36,7 @@ class RuntimeReflector { } } - bool _isTypeCompatible(dynamic value, TypeMirror expectedType) { + bool _isTypeCompatible(dynamic value, TypeMirrorContract expectedType) { // Handle null values if (value == null) { // For now, accept null for any type as we don't have nullability information @@ -81,7 +65,7 @@ class RuntimeReflector { } // Handle generic type parameters - if (expectedType is TypeVariableMirrorImpl) { + if (expectedType is TypeVariableMirror) { return _isTypeCompatible(value, expectedType.upperBound); } @@ -198,9 +182,9 @@ class RuntimeReflector { // Resolve parameters using constructor metadata final resolvedArgs = resolveParameters( constructor.parameters - .map((param) => ParameterMirrorImpl( + .map((param) => ParameterMirror( name: param.name, - type: TypeMirrorImpl( + type: TypeMirror( type: param.type, name: param.type.toString(), owner: mirror, @@ -242,14 +226,15 @@ class RuntimeReflector { } /// Creates a TypeMirror for a given type. - TypeMirror _createTypeMirror(Type type, String name, [ClassMirror? owner]) { + TypeMirrorContract _createTypeMirror(Type type, String name, + [ClassMirrorContract? owner]) { if (type == voidType) { - return TypeMirrorImpl.voidType(owner); + return TypeMirror.voidType(owner); } if (type == dynamicType) { - return TypeMirrorImpl.dynamicType(owner); + return TypeMirror.dynamicType(owner); } - return TypeMirrorImpl( + return TypeMirror( type: type, name: name, owner: owner, @@ -258,7 +243,7 @@ class RuntimeReflector { } /// Reflects on a type, returning its class mirror. - ClassMirror reflectClass(Type type) { + ClassMirrorContract reflectClass(Type type) { // Check cache first if (_classMirrorCache.containsKey(type)) { return _classMirrorCache[type]!; @@ -270,7 +255,7 @@ class RuntimeReflector { } // Create empty mirror and add to cache to break recursion - final emptyMirror = ClassMirrorImpl( + final emptyMirror = ClassMirror( type: type, name: type.toString(), owner: null, @@ -288,11 +273,11 @@ class RuntimeReflector { final typeMetadata = Reflector.getTypeMetadata(type); // Create declarations map - final declarations = {}; + final declarations = {}; // Add properties as variable declarations properties.forEach((name, prop) { - declarations[Symbol(name)] = VariableMirrorImpl( + declarations[Symbol(name)] = VariableMirror( name: name, type: _createTypeMirror(prop.type, prop.type.toString(), emptyMirror), owner: emptyMirror, @@ -305,15 +290,15 @@ class RuntimeReflector { // Add methods as method declarations methods.forEach((name, method) { - declarations[Symbol(name)] = MethodMirrorImpl( + declarations[Symbol(name)] = MethodMirror( name: name, owner: emptyMirror, returnType: method.returnsVoid - ? TypeMirrorImpl.voidType(emptyMirror) + ? TypeMirror.voidType(emptyMirror) : _createTypeMirror( method.returnType, method.returnType.toString(), emptyMirror), parameters: method.parameters - .map((param) => ParameterMirrorImpl( + .map((param) => ParameterMirror( name: param.name, type: _createTypeMirror( param.type, param.type.toString(), emptyMirror), @@ -334,12 +319,12 @@ class RuntimeReflector { // Add constructors as method declarations for (final ctor in constructors) { - declarations[Symbol(ctor.name)] = MethodMirrorImpl( + declarations[Symbol(ctor.name)] = MethodMirror( name: ctor.name, owner: emptyMirror, returnType: emptyMirror, parameters: ctor.parameters - .map((param) => ParameterMirrorImpl( + .map((param) => ParameterMirror( name: param.name, type: _createTypeMirror( param.type, param.type.toString(), emptyMirror), @@ -360,11 +345,11 @@ class RuntimeReflector { } // Create instance and static member maps - final instanceMembers = {}; - final staticMembers = {}; + final instanceMembers = {}; + final staticMembers = {}; methods.forEach((name, method) { - final methodMirror = declarations[Symbol(name)] as MethodMirror; + final methodMirror = declarations[Symbol(name)] as MethodMirrorContract; if (method.isStatic) { staticMembers[Symbol(name)] = methodMirror; } else { @@ -373,7 +358,7 @@ class RuntimeReflector { }); // Create class mirror - final mirror = ClassMirrorImpl( + final mirror = ClassMirror( type: type, name: type.toString(), owner: null, @@ -403,7 +388,7 @@ class RuntimeReflector { } /// Reflects on a type, returning its type mirror. - TypeMirror reflectType(Type type) { + TypeMirrorContract reflectType(Type type) { // Check if type is reflectable if (!Reflector.isReflectable(type)) { throw NotReflectableException(type); @@ -413,22 +398,22 @@ class RuntimeReflector { } /// Creates a new instance reflector for the given object. - InstanceMirror reflect(Object instance) { + InstanceMirrorContract reflect(Object instance) { // Check if type is reflectable if (!Reflector.isReflectable(instance.runtimeType)) { throw NotReflectableException(instance.runtimeType); } - return InstanceMirrorImpl( + return InstanceMirror( reflectee: instance, type: reflectClass(instance.runtimeType), ); } /// Reflects on a library, returning its library mirror. - LibraryMirror reflectLibrary(Uri uri) { + LibraryMirrorContract reflectLibrary(Uri uri) { // Create library mirror with declarations - final library = LibraryMirrorImpl.withDeclarations( + final library = LibraryMirror.withDeclarations( name: uri.toString(), uri: uri, owner: null, @@ -443,30 +428,30 @@ class RuntimeReflector { } /// Gets library dependencies for a given URI. - List _getLibraryDependencies(Uri uri) { + List _getLibraryDependencies(Uri uri) { // Create source library - final sourceLibrary = LibraryMirrorImpl.withDeclarations( + final sourceLibrary = LibraryMirror.withDeclarations( name: uri.toString(), uri: uri, owner: null, ); // Create core library as target - final coreLibrary = LibraryMirrorImpl.withDeclarations( + final coreLibrary = LibraryMirror.withDeclarations( name: 'dart:core', uri: Uri.parse('dart:core'), owner: null, ); // Create test library as target - final testLibrary = LibraryMirrorImpl.withDeclarations( + final testLibrary = LibraryMirror.withDeclarations( name: 'package:test/test.dart', uri: Uri.parse('package:test/test.dart'), owner: null, ); // Create reflection library as target - final reflectionLibrary = LibraryMirrorImpl.withDeclarations( + final reflectionLibrary = LibraryMirror.withDeclarations( name: 'package:platform_reflection/reflection.dart', uri: Uri.parse('package:platform_reflection/reflection.dart'), owner: null, @@ -474,7 +459,7 @@ class RuntimeReflector { return [ // Import dependencies - LibraryDependencyMirrorImpl( + LibraryDependencyMirror( isImport: true, isDeferred: false, sourceLibrary: sourceLibrary, @@ -482,7 +467,7 @@ class RuntimeReflector { prefix: null, combinators: const [], ), - LibraryDependencyMirrorImpl( + LibraryDependencyMirror( isImport: true, isDeferred: false, sourceLibrary: sourceLibrary, @@ -490,7 +475,7 @@ class RuntimeReflector { prefix: null, combinators: const [], ), - LibraryDependencyMirrorImpl( + LibraryDependencyMirror( isImport: true, isDeferred: false, sourceLibrary: sourceLibrary, @@ -499,7 +484,7 @@ class RuntimeReflector { combinators: const [], ), // Export dependencies - LibraryDependencyMirrorImpl( + LibraryDependencyMirror( isImport: false, isDeferred: false, sourceLibrary: sourceLibrary, @@ -511,11 +496,12 @@ class RuntimeReflector { } /// Returns a mirror on the current isolate. - IsolateMirror get currentIsolate => _mirrorSystem.isolate; + IsolateMirrorContract get currentIsolate => _mirrorSystem.isolate; /// Creates a mirror for another isolate. - IsolateMirror reflectIsolate(isolate.Isolate isolate, String debugName) { - return IsolateMirrorImpl.other( + IsolateMirrorContract reflectIsolate( + isolate.Isolate isolate, String debugName) { + return IsolateMirror.other( isolate, debugName, reflectLibrary(Uri.parse('dart:core')), diff --git a/incubation/reflection/lib/src/core/scanner.dart b/incubation/reflection/lib/src/core/scanner.dart index 0f50edf..d0b4559 100644 --- a/incubation/reflection/lib/src/core/scanner.dart +++ b/incubation/reflection/lib/src/core/scanner.dart @@ -1,10 +1,5 @@ import 'dart:core'; -import '../metadata.dart'; -import 'reflector.dart'; -import '../mirrors.dart'; -import '../mirrors/mirror_system_impl.dart'; -import '../mirrors/special_types.dart'; -import '../exceptions.dart'; +import 'package:platform_reflection/mirrors.dart'; /// Runtime scanner that analyzes types and extracts their metadata. class Scanner { @@ -22,7 +17,7 @@ class Scanner { Reflector.register(type); // Get mirror system and analyze type - final mirrorSystem = MirrorSystemImpl.current(); + //final mirrorSystem = MirrorSystem.current(); final typeInfo = TypeAnalyzer.analyze(type); // Convert properties, methods, and constructors to metadata diff --git a/incubation/reflection/lib/src/metadata.dart b/incubation/reflection/lib/src/metadata.dart index 7c137be..66c1715 100644 --- a/incubation/reflection/lib/src/metadata.dart +++ b/incubation/reflection/lib/src/metadata.dart @@ -1,4 +1,4 @@ -import 'exceptions.dart'; +import 'package:platform_reflection/mirrors.dart'; /// Represents metadata about a type parameter. class TypeParameterMetadata { diff --git a/incubation/reflection/lib/src/mirror_system.dart b/incubation/reflection/lib/src/mirror_system.dart index aaac661..99f8637 100644 --- a/incubation/reflection/lib/src/mirror_system.dart +++ b/incubation/reflection/lib/src/mirror_system.dart @@ -1,14 +1,9 @@ import 'dart:core'; -import 'mirrors.dart'; -import 'mirrors/class_mirror_impl.dart'; -import 'mirrors/instance_mirror_impl.dart'; -import 'mirrors/library_mirror_impl.dart'; -import 'mirrors/type_mirror_impl.dart'; -import 'mirrors/isolate_mirror_impl.dart'; -import 'mirrors/special_types.dart'; +import 'package:platform_contracts/contracts.dart'; +import 'package:platform_reflection/mirrors.dart'; -/// The default implementation of [MirrorSystem]. -class RuntimeMirrorSystem implements MirrorSystem { +/// The default implementation of [MirrorSystemContract]. +class RuntimeMirrorSystem implements MirrorSystemContract { /// The singleton instance of the mirror system. static final instance = RuntimeMirrorSystem._(); @@ -16,16 +11,16 @@ class RuntimeMirrorSystem implements MirrorSystem { _initializeRootLibrary(); } - final Map _libraries = {}; - final Map _classes = {}; - final Map _types = {}; - late final LibraryMirror _rootLibrary; + final Map _libraries = {}; + final Map _classes = {}; + final Map _types = {}; + late final LibraryMirrorContract _rootLibrary; @override - Map get libraries => Map.unmodifiable(_libraries); + Map get libraries => Map.unmodifiable(_libraries); @override - LibraryMirror findLibrary(Symbol libraryName) { + LibraryMirrorContract findLibrary(Symbol libraryName) { final lib = _libraries.values.firstWhere( (lib) => lib.qualifiedName == libraryName, orElse: () => throw ArgumentError('Library not found: $libraryName'), @@ -34,30 +29,31 @@ class RuntimeMirrorSystem implements MirrorSystem { } @override - IsolateMirror get isolate => IsolateMirrorImpl.current(_rootLibrary); + IsolateMirrorContract get isolate => IsolateMirror.current(_rootLibrary); @override - TypeMirror get dynamicType => _getOrCreateTypeMirror(dynamic); + TypeMirrorContract get dynamicType => _getOrCreateTypeMirror(dynamic); @override - TypeMirror get voidType => _getOrCreateTypeMirror(VoidType); + TypeMirrorContract get voidType => _getOrCreateTypeMirror(VoidType); @override - TypeMirror get neverType => _getOrCreateTypeMirror(NeverType); + TypeMirrorContract get neverType => _getOrCreateTypeMirror(NeverType); /// Creates a mirror reflecting [reflectee]. - InstanceMirror reflect(Object reflectee) { - return InstanceMirrorImpl( + InstanceMirrorContract reflect(Object reflectee) { + return InstanceMirror( reflectee: reflectee, type: reflectClass(reflectee.runtimeType), ); } /// Creates a mirror reflecting the class [key]. - ClassMirror reflectClass(Type key) { + @override + ClassMirrorContract reflectClass(Type key) { return _classes.putIfAbsent( key, - () => ClassMirrorImpl( + () => ClassMirror( type: key, name: key.toString(), owner: _rootLibrary, @@ -70,14 +66,15 @@ class RuntimeMirrorSystem implements MirrorSystem { } /// Creates a mirror reflecting the type [key]. - TypeMirror reflectType(Type key) { + @override + TypeMirrorContract reflectType(Type key) { return _getOrCreateTypeMirror(key); } - TypeMirror _getOrCreateTypeMirror(Type type) { + TypeMirrorContract _getOrCreateTypeMirror(Type type) { return _types.putIfAbsent( type, - () => TypeMirrorImpl( + () => TypeMirror( type: type, name: type.toString(), owner: _rootLibrary, @@ -87,7 +84,7 @@ class RuntimeMirrorSystem implements MirrorSystem { } void _initializeRootLibrary() { - _rootLibrary = LibraryMirrorImpl.withDeclarations( + _rootLibrary = LibraryMirror.withDeclarations( name: 'dart.core', uri: Uri.parse('dart:core'), ); @@ -96,16 +93,16 @@ class RuntimeMirrorSystem implements MirrorSystem { } /// The current mirror system. -MirrorSystem currentMirrorSystem() => RuntimeMirrorSystem.instance; +MirrorSystemContract currentMirrorSystem() => RuntimeMirrorSystem.instance; /// Reflects an instance. -InstanceMirror reflect(Object reflectee) => +InstanceMirrorContract reflect(Object reflectee) => RuntimeMirrorSystem.instance.reflect(reflectee); /// Reflects a class. -ClassMirror reflectClass(Type key) => +ClassMirrorContract reflectClass(Type key) => RuntimeMirrorSystem.instance.reflectClass(key); /// Reflects a type. -TypeMirror reflectType(Type key) => +TypeMirrorContract reflectType(Type key) => RuntimeMirrorSystem.instance.reflectType(key); diff --git a/incubation/reflection/lib/src/mirrors.dart b/incubation/reflection/lib/src/mirrors.dart deleted file mode 100644 index 693c550..0000000 --- a/incubation/reflection/lib/src/mirrors.dart +++ /dev/null @@ -1,61 +0,0 @@ -/// Basic reflection in Dart, with support for introspection and dynamic invocation. -library mirrors; - -import 'package:platform_contracts/contracts.dart'; - -export 'package:platform_contracts/contracts.dart' - show - Mirror, - DeclarationMirror, - ObjectMirror, - InstanceMirror, - TypeMirror, - ClassMirror, - LibraryMirror, - MethodMirror, - VariableMirror, - ParameterMirror, - TypeVariableMirror, - LibraryDependencyMirror, - CombinatorMirror; - -export 'mirrors/mirrors.dart'; - -/// An [IsolateMirror] reflects an isolate. -abstract class IsolateMirror implements Mirror { - /// A unique name used to refer to the isolate in debugging messages. - String get debugName; - - /// Whether this mirror reflects the currently running isolate. - bool get isCurrent; - - /// The root library for the reflected isolate. - LibraryMirror get rootLibrary; -} - -/// A [MirrorSystem] is the main interface used to reflect on a set of libraries. -abstract class MirrorSystem { - /// All libraries known to the mirror system. - Map get libraries; - - /// Returns the unique library with the specified name. - LibraryMirror findLibrary(Symbol libraryName); - - /// Returns a mirror for the specified class. - ClassMirror reflectClass(Type type); - - /// Returns a mirror for the specified type. - TypeMirror reflectType(Type type); - - /// A mirror on the isolate associated with this mirror system. - IsolateMirror get isolate; - - /// A mirror on the dynamic type. - TypeMirror get dynamicType; - - /// A mirror on the void type. - TypeMirror get voidType; - - /// A mirror on the Never type. - TypeMirror get neverType; -} diff --git a/incubation/reflection/lib/src/mirrors/base_mirror.dart b/incubation/reflection/lib/src/mirrors/base_mirror.dart index 5bcb094..4b33b04 100644 --- a/incubation/reflection/lib/src/mirrors/base_mirror.dart +++ b/incubation/reflection/lib/src/mirrors/base_mirror.dart @@ -1,31 +1,31 @@ import 'package:meta/meta.dart'; -import '../mirrors.dart'; +import 'package:platform_contracts/contracts.dart'; /// Base class for mirrors that have an owner. -abstract class MutableOwnerMirror implements DeclarationMirror { - DeclarationMirror? _owner; +abstract class MutableOwnerMirror implements DeclarationMirrorContract { + DeclarationMirrorContract? _owner; /// Sets the owner of this mirror. @protected - void setOwner(DeclarationMirror? owner) { + void setOwner(DeclarationMirrorContract? owner) { _owner = owner; } @override - DeclarationMirror? get owner => _owner; + DeclarationMirrorContract? get owner => _owner; } /// Base class for mirrors that have a type. abstract class TypedMirror extends MutableOwnerMirror { final Type _type; final String _name; - final List _metadata; + final List _metadata; TypedMirror({ required Type type, required String name, - DeclarationMirror? owner, - List metadata = const [], + DeclarationMirrorContract? owner, + List metadata = const [], }) : _type = type, _name = name, _metadata = metadata { @@ -54,5 +54,5 @@ abstract class TypedMirror extends MutableOwnerMirror { bool get isTopLevel => owner == null; @override - List get metadata => List.unmodifiable(_metadata); + List get metadata => List.unmodifiable(_metadata); } diff --git a/incubation/reflection/lib/src/mirrors/class_mirror_impl.dart b/incubation/reflection/lib/src/mirrors/class_mirror.dart similarity index 82% rename from incubation/reflection/lib/src/mirrors/class_mirror_impl.dart rename to incubation/reflection/lib/src/mirrors/class_mirror.dart index d4b1afa..c274ec4 100644 --- a/incubation/reflection/lib/src/mirrors/class_mirror_impl.dart +++ b/incubation/reflection/lib/src/mirrors/class_mirror.dart @@ -1,23 +1,16 @@ -import '../metadata.dart'; -import '../mirrors.dart'; -import '../exceptions.dart'; -import '../core/reflector.dart'; -import 'base_mirror.dart'; -import 'instance_mirror_impl.dart'; -import 'method_mirror_impl.dart'; -import 'mirror_system_impl.dart'; -import 'type_mirror_impl.dart'; +import 'package:platform_contracts/contracts.dart'; +import 'package:platform_reflection/mirrors.dart'; -/// Implementation of [ClassMirror]. -class ClassMirrorImpl extends TypeMirrorImpl implements ClassMirror { +/// Implementation of [ClassMirrorContract]. +class ClassMirror extends TypeMirror implements ClassMirrorContract { @override - final Map declarations; + final Map declarations; @override - final Map instanceMembers; + final Map instanceMembers; @override - final Map staticMembers; + final Map staticMembers; @override final bool isAbstract; @@ -26,19 +19,19 @@ class ClassMirrorImpl extends TypeMirrorImpl implements ClassMirror { final bool isEnum; @override - final ClassMirror? superclass; + final ClassMirrorContract? superclass; @override - final List superinterfaces; + final List superinterfaces; - ClassMirrorImpl({ + ClassMirror({ required Type type, required String name, - required DeclarationMirror? owner, + required DeclarationMirrorContract? owner, required this.declarations, required this.instanceMembers, required this.staticMembers, - required List metadata, + required List metadata, this.isAbstract = false, this.isEnum = false, this.superclass, @@ -57,19 +50,19 @@ class ClassMirrorImpl extends TypeMirrorImpl implements ClassMirror { } @override - bool isSubclassOf(ClassMirror other) { + bool isSubclassOf(ClassMirrorContract other) { var current = this; while (current.superclass != null) { if (current.superclass == other) { return true; } - current = current.superclass as ClassMirrorImpl; + current = current.superclass as ClassMirror; } return false; } @override - InstanceMirror newInstance( + InstanceMirrorContract newInstance( Symbol constructorName, List positionalArguments, [ Map? namedArguments, @@ -126,7 +119,7 @@ class ClassMirrorImpl extends TypeMirrorImpl implements ClassMirror { 'Failed to create instance: creator returned null'); } - return InstanceMirrorImpl( + return InstanceMirror( reflectee: instance, type: this, ); @@ -136,7 +129,8 @@ class ClassMirrorImpl extends TypeMirrorImpl implements ClassMirror { } @override - InstanceMirror invoke(Symbol memberName, List positionalArguments, + InstanceMirrorContract invoke( + Symbol memberName, List positionalArguments, [Map? namedArguments]) { try { // Get method metadata @@ -174,7 +168,7 @@ class ClassMirrorImpl extends TypeMirrorImpl implements ClassMirror { namedArguments, ); - return InstanceMirrorImpl( + return InstanceMirror( reflectee: result, type: this, ); @@ -184,7 +178,7 @@ class ClassMirrorImpl extends TypeMirrorImpl implements ClassMirror { } @override - InstanceMirror getField(Symbol fieldName) { + InstanceMirrorContract getField(Symbol fieldName) { final declaration = declarations[fieldName]; if (declaration == null) { throw NoSuchMethodError.withInvocation( @@ -195,7 +189,7 @@ class ClassMirrorImpl extends TypeMirrorImpl implements ClassMirror { try { final value = (type as dynamic)[_symbolToString(fieldName)]; - return InstanceMirrorImpl( + return InstanceMirror( reflectee: value, type: this, ); @@ -205,7 +199,7 @@ class ClassMirrorImpl extends TypeMirrorImpl implements ClassMirror { } @override - InstanceMirror setField(Symbol fieldName, dynamic value) { + InstanceMirrorContract setField(Symbol fieldName, dynamic value) { final declaration = declarations[fieldName]; if (declaration == null) { throw NoSuchMethodError.withInvocation( @@ -216,7 +210,7 @@ class ClassMirrorImpl extends TypeMirrorImpl implements ClassMirror { try { (type as dynamic)[_symbolToString(fieldName)] = value; - return InstanceMirrorImpl( + return InstanceMirror( reflectee: value, type: this, ); @@ -228,7 +222,7 @@ class ClassMirrorImpl extends TypeMirrorImpl implements ClassMirror { @override bool operator ==(Object other) => identical(this, other) || - other is ClassMirrorImpl && + other is ClassMirror && runtimeType == other.runtimeType && type == other.type; diff --git a/incubation/reflection/lib/src/mirrors/combinator_mirror_impl.dart b/incubation/reflection/lib/src/mirrors/combinator_mirror.dart similarity index 71% rename from incubation/reflection/lib/src/mirrors/combinator_mirror_impl.dart rename to incubation/reflection/lib/src/mirrors/combinator_mirror.dart index 1803087..57ff9f4 100644 --- a/incubation/reflection/lib/src/mirrors/combinator_mirror_impl.dart +++ b/incubation/reflection/lib/src/mirrors/combinator_mirror.dart @@ -1,11 +1,11 @@ -import '../mirrors.dart'; +import 'package:platform_contracts/contracts.dart'; -/// Implementation of [CombinatorMirror] that provides reflection on show/hide combinators. -class CombinatorMirrorImpl implements CombinatorMirror { +/// Implementation of [CombinatorMirrorContract] that provides reflection on show/hide combinators. +class CombinatorMirror implements CombinatorMirrorContract { final List _identifiers; final bool _isShow; - CombinatorMirrorImpl({ + CombinatorMirror({ required List identifiers, required bool isShow, }) : _identifiers = identifiers, @@ -23,7 +23,7 @@ class CombinatorMirrorImpl implements CombinatorMirror { @override bool operator ==(Object other) { if (identical(this, other)) return true; - if (other is! CombinatorMirrorImpl) return false; + if (other is! CombinatorMirror) return false; return _identifiers == other._identifiers && _isShow == other._isShow; } diff --git a/incubation/reflection/lib/src/mirrors/instance_mirror_impl.dart b/incubation/reflection/lib/src/mirrors/instance_mirror.dart similarity index 85% rename from incubation/reflection/lib/src/mirrors/instance_mirror_impl.dart rename to incubation/reflection/lib/src/mirrors/instance_mirror.dart index 4a130a5..cf2f838 100644 --- a/incubation/reflection/lib/src/mirrors/instance_mirror_impl.dart +++ b/incubation/reflection/lib/src/mirrors/instance_mirror.dart @@ -1,21 +1,20 @@ import 'dart:core'; -import '../mirrors.dart'; -import '../exceptions.dart'; -import '../core/reflector.dart'; +import 'package:platform_contracts/contracts.dart'; +import 'package:platform_reflection/mirrors.dart'; -/// Implementation of [InstanceMirror] that provides reflection on instances. -class InstanceMirrorImpl implements InstanceMirror { +/// Implementation of [InstanceMirrorContract] that provides reflection on instances. +class InstanceMirror implements InstanceMirrorContract { final Object _reflectee; - final ClassMirror _type; + final ClassMirrorContract _type; - InstanceMirrorImpl({ + InstanceMirror({ required Object reflectee, - required ClassMirror type, + required ClassMirrorContract type, }) : _reflectee = reflectee, _type = type; @override - ClassMirror get type => _type; + ClassMirrorContract get type => _type; @override bool get hasReflectee => true; @@ -24,7 +23,7 @@ class InstanceMirrorImpl implements InstanceMirror { dynamic get reflectee => _reflectee; @override - InstanceMirror invoke(Symbol memberName, List positionalArguments, + InstanceMirrorContract invoke(Symbol memberName, List positionalArguments, [Map namedArguments = const {}]) { // Get method metadata final methods = Reflector.getMethodMetadata(_reflectee.runtimeType); @@ -79,7 +78,7 @@ class InstanceMirrorImpl implements InstanceMirror { default: throw ReflectionException('Method $methodName not implemented'); } - return InstanceMirrorImpl( + return InstanceMirror( reflectee: result ?? '', type: _type, ); @@ -89,7 +88,7 @@ class InstanceMirrorImpl implements InstanceMirror { } @override - InstanceMirror getField(Symbol fieldName) { + InstanceMirrorContract getField(Symbol fieldName) { // Get property metadata final properties = Reflector.getPropertyMetadata(_reflectee.runtimeType); if (properties == null) { @@ -135,7 +134,7 @@ class InstanceMirrorImpl implements InstanceMirror { default: throw ReflectionException('Property $propertyName not implemented'); } - return InstanceMirrorImpl( + return InstanceMirror( reflectee: value ?? '', type: _type, ); @@ -145,7 +144,7 @@ class InstanceMirrorImpl implements InstanceMirror { } @override - InstanceMirror setField(Symbol fieldName, dynamic value) { + InstanceMirrorContract setField(Symbol fieldName, dynamic value) { // Get property metadata final properties = Reflector.getPropertyMetadata(_reflectee.runtimeType); if (properties == null) { @@ -194,7 +193,7 @@ class InstanceMirrorImpl implements InstanceMirror { default: throw ReflectionException('Property $propertyName not implemented'); } - return InstanceMirrorImpl( + return InstanceMirror( reflectee: value, type: _type, ); @@ -206,7 +205,7 @@ class InstanceMirrorImpl implements InstanceMirror { @override bool operator ==(Object other) { if (identical(this, other)) return true; - if (other is! InstanceMirrorImpl) return false; + if (other is! InstanceMirror) return false; return identical(_reflectee, other._reflectee) && _type == other._type; } @@ -224,22 +223,22 @@ class InstanceMirrorImpl implements InstanceMirror { } } -/// Implementation of [InstanceMirror] for closures. -class ClosureMirrorImpl extends InstanceMirrorImpl { - final MethodMirror _function; +/// Implementation of [InstanceMirrorContract] for closures. +class ClosureMirrorImpl extends InstanceMirror { + final MethodMirrorContract _function; ClosureMirrorImpl({ required Object reflectee, - required ClassMirror type, - required MethodMirror function, + required ClassMirrorContract type, + required MethodMirrorContract function, }) : _function = function, super(reflectee: reflectee, type: type); /// The function this closure represents. - MethodMirror get function => _function; + MethodMirrorContract get function => _function; /// Applies this closure with the given arguments. - InstanceMirror apply(List positionalArguments, + InstanceMirrorContract apply(List positionalArguments, [Map namedArguments = const {}]) { final closure = reflectee as Function; final result = Function.apply( @@ -247,7 +246,7 @@ class ClosureMirrorImpl extends InstanceMirrorImpl { positionalArguments, namedArguments, ); - return InstanceMirrorImpl( + return InstanceMirror( reflectee: result ?? '', type: type, ); @@ -269,11 +268,11 @@ class ClosureMirrorImpl extends InstanceMirrorImpl { String toString() => 'ClosureMirror on ${_reflectee.runtimeType}'; } -/// Implementation of [InstanceMirror] for simple values. -class ValueMirrorImpl extends InstanceMirrorImpl { +/// Implementation of [InstanceMirrorContract] for simple values. +class ValueMirrorImpl extends InstanceMirror { ValueMirrorImpl({ required Object reflectee, - required ClassMirror type, + required ClassMirrorContract type, }) : super(reflectee: reflectee, type: type); @override diff --git a/incubation/reflection/lib/src/mirrors/isolate_mirror_impl.dart b/incubation/reflection/lib/src/mirrors/isolate_mirror.dart similarity index 84% rename from incubation/reflection/lib/src/mirrors/isolate_mirror_impl.dart rename to incubation/reflection/lib/src/mirrors/isolate_mirror.dart index 78abb4a..21d505f 100644 --- a/incubation/reflection/lib/src/mirrors/isolate_mirror_impl.dart +++ b/incubation/reflection/lib/src/mirrors/isolate_mirror.dart @@ -1,19 +1,18 @@ import 'dart:core'; import 'dart:isolate' as isolate; -import '../mirrors.dart'; -import 'library_mirror_impl.dart'; +import 'package:platform_contracts/contracts.dart'; -/// Implementation of [IsolateMirror] that provides reflection on isolates. -class IsolateMirrorImpl implements IsolateMirror { +/// Implementation of [IsolateMirrorContract] that provides reflection on isolates. +class IsolateMirror implements IsolateMirrorContract { final String _debugName; final bool _isCurrent; - final LibraryMirror _rootLibrary; + final LibraryMirrorContract _rootLibrary; final isolate.Isolate? _underlyingIsolate; - IsolateMirrorImpl({ + IsolateMirror({ required String debugName, required bool isCurrent, - required LibraryMirror rootLibrary, + required LibraryMirrorContract rootLibrary, isolate.Isolate? underlyingIsolate, }) : _debugName = debugName, _isCurrent = isCurrent, @@ -21,8 +20,8 @@ class IsolateMirrorImpl implements IsolateMirror { _underlyingIsolate = underlyingIsolate; /// Creates a mirror for the current isolate. - factory IsolateMirrorImpl.current(LibraryMirror rootLibrary) { - return IsolateMirrorImpl( + factory IsolateMirror.current(LibraryMirrorContract rootLibrary) { + return IsolateMirror( debugName: 'main', isCurrent: true, rootLibrary: rootLibrary, @@ -31,12 +30,12 @@ class IsolateMirrorImpl implements IsolateMirror { } /// Creates a mirror for another isolate. - factory IsolateMirrorImpl.other( + factory IsolateMirror.other( isolate.Isolate underlyingIsolate, String debugName, - LibraryMirror rootLibrary, + LibraryMirrorContract rootLibrary, ) { - return IsolateMirrorImpl( + return IsolateMirror( debugName: debugName, isCurrent: false, rootLibrary: rootLibrary, @@ -51,7 +50,7 @@ class IsolateMirrorImpl implements IsolateMirror { bool get isCurrent => _isCurrent; @override - LibraryMirror get rootLibrary => _rootLibrary; + LibraryMirrorContract get rootLibrary => _rootLibrary; /// The underlying isolate, if this mirror reflects a non-current isolate. isolate.Isolate? get underlyingIsolate => _underlyingIsolate; @@ -59,7 +58,7 @@ class IsolateMirrorImpl implements IsolateMirror { @override bool operator ==(Object other) { if (identical(this, other)) return true; - if (other is! IsolateMirrorImpl) return false; + if (other is! IsolateMirror) return false; // Only compare debug name and isCurrent flag // Two mirrors pointing to the same isolate should be equal diff --git a/incubation/reflection/lib/src/mirrors/library_dependency_mirror_impl.dart b/incubation/reflection/lib/src/mirrors/library_dependency_mirror.dart similarity index 64% rename from incubation/reflection/lib/src/mirrors/library_dependency_mirror_impl.dart rename to incubation/reflection/lib/src/mirrors/library_dependency_mirror.dart index 09e426b..9e27208 100644 --- a/incubation/reflection/lib/src/mirrors/library_dependency_mirror_impl.dart +++ b/incubation/reflection/lib/src/mirrors/library_dependency_mirror.dart @@ -1,21 +1,21 @@ -import '../mirrors.dart'; +import 'package:platform_contracts/contracts.dart'; -/// Implementation of [LibraryDependencyMirror] that provides reflection on library dependencies. -class LibraryDependencyMirrorImpl implements LibraryDependencyMirror { +/// Implementation of [LibraryDependencyMirrorContract] that provides reflection on library dependencies. +class LibraryDependencyMirror implements LibraryDependencyMirrorContract { final bool _isImport; final bool _isDeferred; - final LibraryMirror _sourceLibrary; - final LibraryMirror? _targetLibrary; + final LibraryMirrorContract _sourceLibrary; + final LibraryMirrorContract? _targetLibrary; final Symbol? _prefix; - final List _combinators; + final List _combinators; - LibraryDependencyMirrorImpl({ + LibraryDependencyMirror({ required bool isImport, required bool isDeferred, - required LibraryMirror sourceLibrary, - LibraryMirror? targetLibrary, + required LibraryMirrorContract sourceLibrary, + LibraryMirrorContract? targetLibrary, Symbol? prefix, - List combinators = const [], + List combinators = const [], }) : _isImport = isImport, _isDeferred = isDeferred, _sourceLibrary = sourceLibrary, @@ -33,21 +33,22 @@ class LibraryDependencyMirrorImpl implements LibraryDependencyMirror { bool get isDeferred => _isDeferred; @override - LibraryMirror get sourceLibrary => _sourceLibrary; + LibraryMirrorContract get sourceLibrary => _sourceLibrary; @override - LibraryMirror? get targetLibrary => _targetLibrary; + LibraryMirrorContract? get targetLibrary => _targetLibrary; @override Symbol? get prefix => _prefix; @override - List get combinators => List.unmodifiable(_combinators); + List get combinators => + List.unmodifiable(_combinators); @override bool operator ==(Object other) { if (identical(this, other)) return true; - if (other is! LibraryDependencyMirrorImpl) return false; + if (other is! LibraryDependencyMirror) return false; return _isImport == other._isImport && _isDeferred == other._isDeferred && diff --git a/incubation/reflection/lib/src/mirrors/library_mirror_impl.dart b/incubation/reflection/lib/src/mirrors/library_mirror.dart similarity index 76% rename from incubation/reflection/lib/src/mirrors/library_mirror_impl.dart rename to incubation/reflection/lib/src/mirrors/library_mirror.dart index 74ff1d0..71f7dff 100644 --- a/incubation/reflection/lib/src/mirrors/library_mirror_impl.dart +++ b/incubation/reflection/lib/src/mirrors/library_mirror.dart @@ -1,31 +1,21 @@ import 'dart:core'; -import '../mirrors.dart'; -import '../core/library_scanner.dart'; -import 'base_mirror.dart'; -import 'library_dependency_mirror_impl.dart'; -import 'method_mirror_impl.dart'; -import 'variable_mirror_impl.dart'; -import 'type_mirror_impl.dart'; -import 'parameter_mirror_impl.dart'; -import 'instance_mirror_impl.dart'; -import 'class_mirror_impl.dart'; -import '../core/reflector.dart'; -import '../core/runtime_reflector.dart'; +import 'package:platform_contracts/contracts.dart'; +import 'package:platform_reflection/mirrors.dart'; -/// Implementation of [LibraryMirror] that provides reflection on libraries. -class LibraryMirrorImpl extends TypedMirror implements LibraryMirror { +/// Implementation of [LibraryMirrorContract] that provides reflection on libraries. +class LibraryMirror extends TypedMirror implements LibraryMirrorContract { final Uri _uri; - final Map _declarations; - final List _libraryDependencies; + final Map _declarations; + final List _libraryDependencies; final Map _topLevelValues; - LibraryMirrorImpl({ + LibraryMirror({ required String name, required Uri uri, - DeclarationMirror? owner, - Map? declarations, - List libraryDependencies = const [], - List metadata = const [], + DeclarationMirrorContract? owner, + Map? declarations, + List libraryDependencies = const [], + List metadata = const [], Map? topLevelValues, }) : _uri = uri, _declarations = declarations ?? {}, @@ -39,20 +29,20 @@ class LibraryMirrorImpl extends TypedMirror implements LibraryMirror { ); /// Factory constructor that creates a library mirror with declarations from scanning - factory LibraryMirrorImpl.withDeclarations({ + factory LibraryMirror.withDeclarations({ required String name, required Uri uri, - DeclarationMirror? owner, - List libraryDependencies = const [], - List metadata = const [], + DeclarationMirrorContract? owner, + List libraryDependencies = const [], + List metadata = const [], }) { // Scan library to get declarations final libraryInfo = LibraryScanner.scanLibrary(uri); - final declarations = {}; + final declarations = {}; final topLevelValues = {}; // Create temporary library for owner references - final tempLibrary = LibraryMirrorImpl( + final tempLibrary = LibraryMirror( name: name, uri: uri, owner: owner, @@ -63,19 +53,19 @@ class LibraryMirrorImpl extends TypedMirror implements LibraryMirror { // Add top-level function declarations for (final function in libraryInfo.topLevelFunctions) { if (!function.isPrivate || uri == tempLibrary.uri) { - declarations[Symbol(function.name)] = MethodMirrorImpl( + declarations[Symbol(function.name)] = MethodMirror( name: function.name, owner: tempLibrary, - returnType: TypeMirrorImpl( + returnType: TypeMirror( type: function.returnType, name: function.returnType.toString(), owner: tempLibrary, metadata: const [], ), parameters: function.parameters - .map((param) => ParameterMirrorImpl( + .map((param) => ParameterMirror( name: param.name, - type: TypeMirrorImpl( + type: TypeMirror( type: param.type, name: param.type.toString(), owner: tempLibrary, @@ -96,9 +86,9 @@ class LibraryMirrorImpl extends TypedMirror implements LibraryMirror { // Add top-level variable declarations for (final variable in libraryInfo.topLevelVariables) { if (!variable.isPrivate || uri == tempLibrary.uri) { - declarations[Symbol(variable.name)] = VariableMirrorImpl( + declarations[Symbol(variable.name)] = VariableMirror( name: variable.name, - type: TypeMirrorImpl( + type: TypeMirror( type: variable.type, name: variable.type.toString(), owner: tempLibrary, @@ -124,15 +114,15 @@ class LibraryMirrorImpl extends TypedMirror implements LibraryMirror { } // Create library dependencies - final dependencies = []; + final dependencies = []; // Add imports for (final dep in libraryInfo.dependencies) { - dependencies.add(LibraryDependencyMirrorImpl( + dependencies.add(LibraryDependencyMirror( isImport: true, isDeferred: dep.isDeferred, sourceLibrary: tempLibrary, - targetLibrary: LibraryMirrorImpl.withDeclarations( + targetLibrary: LibraryMirror.withDeclarations( name: dep.uri.toString(), uri: dep.uri, owner: tempLibrary, @@ -144,11 +134,11 @@ class LibraryMirrorImpl extends TypedMirror implements LibraryMirror { // Add exports for (final dep in libraryInfo.exports) { - dependencies.add(LibraryDependencyMirrorImpl( + dependencies.add(LibraryDependencyMirror( isImport: false, isDeferred: false, sourceLibrary: tempLibrary, - targetLibrary: LibraryMirrorImpl.withDeclarations( + targetLibrary: LibraryMirror.withDeclarations( name: dep.uri.toString(), uri: dep.uri, owner: tempLibrary, @@ -158,7 +148,7 @@ class LibraryMirrorImpl extends TypedMirror implements LibraryMirror { )); } - return LibraryMirrorImpl( + return LibraryMirror( name: name, uri: uri, owner: owner, @@ -194,15 +184,15 @@ class LibraryMirrorImpl extends TypedMirror implements LibraryMirror { Uri get uri => _uri; @override - Map get declarations => + Map get declarations => Map.unmodifiable(_declarations); @override - List get libraryDependencies => + List get libraryDependencies => List.unmodifiable(_libraryDependencies); @override - InstanceMirror invoke(Symbol memberName, List positionalArguments, + InstanceMirrorContract invoke(Symbol memberName, List positionalArguments, [Map namedArguments = const {}]) { final member = declarations[memberName]; if (member == null) { @@ -212,7 +202,7 @@ class LibraryMirrorImpl extends TypedMirror implements LibraryMirror { ); } - if (member is! MethodMirror) { + if (member is! MethodMirrorContract) { throw NoSuchMethodError.withInvocation( this, Invocation.method(memberName, positionalArguments, namedArguments), @@ -223,7 +213,7 @@ class LibraryMirrorImpl extends TypedMirror implements LibraryMirror { if (memberName == const Symbol('add')) { final a = positionalArguments[0] as int; final b = positionalArguments[1] as int; - return InstanceMirrorImpl( + return InstanceMirror( reflectee: a + b, type: _createPrimitiveClassMirror(int, 'int'), ); @@ -234,7 +224,7 @@ class LibraryMirrorImpl extends TypedMirror implements LibraryMirror { } @override - InstanceMirror getField(Symbol fieldName) { + InstanceMirrorContract getField(Symbol fieldName) { final member = declarations[fieldName]; if (member == null) { throw NoSuchMethodError.withInvocation( @@ -243,7 +233,7 @@ class LibraryMirrorImpl extends TypedMirror implements LibraryMirror { ); } - if (member is! VariableMirror) { + if (member is! VariableMirrorContract) { throw NoSuchMethodError.withInvocation( this, Invocation.getter(fieldName), @@ -257,14 +247,14 @@ class LibraryMirrorImpl extends TypedMirror implements LibraryMirror { 'Top-level variable $fieldName has not been initialized'); } - return InstanceMirrorImpl( + return InstanceMirror( reflectee: value, type: _createPrimitiveClassMirror(member.type.reflectedType, member.name), ); } @override - InstanceMirror setField(Symbol fieldName, dynamic value) { + InstanceMirrorContract setField(Symbol fieldName, dynamic value) { final member = declarations[fieldName]; if (member == null) { throw NoSuchMethodError.withInvocation( @@ -273,7 +263,7 @@ class LibraryMirrorImpl extends TypedMirror implements LibraryMirror { ); } - if (member is! VariableMirror) { + if (member is! VariableMirrorContract) { throw NoSuchMethodError.withInvocation( this, Invocation.setter(fieldName, [value]), @@ -296,15 +286,16 @@ class LibraryMirrorImpl extends TypedMirror implements LibraryMirror { // Update value in top-level values map _topLevelValues[fieldName] = value; - return InstanceMirrorImpl( + return InstanceMirror( reflectee: value, type: _createPrimitiveClassMirror(member.type.reflectedType, member.name), ); } /// Creates a ClassMirror for a primitive type. - static ClassMirror _createPrimitiveClassMirror(Type type, String name) { - return ClassMirrorImpl( + static ClassMirrorContract _createPrimitiveClassMirror( + Type type, String name) { + return ClassMirror( type: type, name: name, owner: null, @@ -318,7 +309,7 @@ class LibraryMirrorImpl extends TypedMirror implements LibraryMirror { @override bool operator ==(Object other) { if (identical(this, other)) return true; - if (other is! LibraryMirrorImpl) return false; + if (other is! LibraryMirror) return false; return _uri == other._uri && name == other.name && diff --git a/incubation/reflection/lib/src/mirrors/method_mirror_impl.dart b/incubation/reflection/lib/src/mirrors/method_mirror.dart similarity index 83% rename from incubation/reflection/lib/src/mirrors/method_mirror_impl.dart rename to incubation/reflection/lib/src/mirrors/method_mirror.dart index fac7432..a8932dc 100644 --- a/incubation/reflection/lib/src/mirrors/method_mirror_impl.dart +++ b/incubation/reflection/lib/src/mirrors/method_mirror.dart @@ -1,10 +1,10 @@ -import '../mirrors.dart'; -import 'base_mirror.dart'; +import 'package:platform_contracts/contracts.dart'; +import 'package:platform_reflection/mirrors.dart'; -/// Implementation of [MethodMirror] that provides reflection on methods. -class MethodMirrorImpl extends TypedMirror implements MethodMirror { - final TypeMirror _returnType; - final List _parameters; +/// Implementation of [MethodMirrorContract] that provides reflection on methods. +class MethodMirror extends TypedMirror implements MethodMirrorContract { + final TypeMirrorContract _returnType; + final List _parameters; final bool _isStatic; final bool _isAbstract; final bool _isSynthetic; @@ -16,11 +16,11 @@ class MethodMirrorImpl extends TypedMirror implements MethodMirror { final bool _isFactoryConstructor; final String? _source; - MethodMirrorImpl({ + MethodMirror({ required String name, - required DeclarationMirror? owner, - required TypeMirror returnType, - required List parameters, + required DeclarationMirrorContract? owner, + required TypeMirrorContract returnType, + required List parameters, bool isStatic = false, bool isAbstract = false, bool isSynthetic = false, @@ -31,7 +31,7 @@ class MethodMirrorImpl extends TypedMirror implements MethodMirror { bool isRedirectingConstructor = false, bool isFactoryConstructor = false, String? source, - List metadata = const [], + List metadata = const [], }) : _returnType = returnType, _parameters = parameters, _isStatic = isStatic, @@ -52,10 +52,11 @@ class MethodMirrorImpl extends TypedMirror implements MethodMirror { ); @override - TypeMirror get returnType => _returnType; + TypeMirrorContract get returnType => _returnType; @override - List get parameters => List.unmodifiable(_parameters); + List get parameters => + List.unmodifiable(_parameters); @override bool get isStatic => _isStatic; @@ -103,7 +104,7 @@ class MethodMirrorImpl extends TypedMirror implements MethodMirror { @override bool operator ==(Object other) { if (identical(this, other)) return true; - if (other is! MethodMirrorImpl) return false; + if (other is! MethodMirror) return false; return name == other.name && owner == other.owner && diff --git a/incubation/reflection/lib/src/mirrors/mirror_system.dart b/incubation/reflection/lib/src/mirrors/mirror_system.dart new file mode 100644 index 0000000..787c789 --- /dev/null +++ b/incubation/reflection/lib/src/mirrors/mirror_system.dart @@ -0,0 +1,323 @@ +import 'dart:core'; +import 'package:platform_contracts/contracts.dart'; +import 'package:platform_reflection/mirrors.dart'; + +/// Implementation of [MirrorSystemContract] that provides reflection on a set of libraries. +class MirrorSystem implements MirrorSystemContract { + static MirrorSystem? _instance; + + static MirrorSystem get instance { + return _instance ??= MirrorSystem._(); + } + + // Add this method back + static MirrorSystem current() { + return instance; + } + + final Map _libraries = {}; + final Map _classes = {}; + final Map _types = {}; + late final LibraryMirrorContract _rootLibrary; + late final IsolateMirrorContract _isolate; + late final TypeMirrorContract _dynamicType; + late final TypeMirrorContract _voidType; + late final TypeMirrorContract _neverType; + + // Private constructor + MirrorSystem._() { + _initializeSystem(); + } + + void _initializeSystem() { + _initializeRootLibrary(); + _initializeCoreDependencies(); + _initializeSpecialTypes(); + _initializeIsolate(); + } + + void _initializeRootLibrary() { + _rootLibrary = LibraryMirror.withDeclarations( + name: 'dart.core', + uri: Uri.parse('dart:core'), + ); + _libraries[_rootLibrary.uri] = _rootLibrary; + } + + void _initializeCoreDependencies() { + // Create core library mirror + final coreLibrary = LibraryMirror.withDeclarations( + name: 'dart:core', + uri: _createDartUri('core'), + owner: null, + ); + + // Create async library mirror + final asyncLibrary = LibraryMirror.withDeclarations( + name: 'dart:async', + uri: _createDartUri('async'), + owner: null, + ); + + // Create test library mirror + final testLibrary = LibraryMirror.withDeclarations( + name: 'package:test/test.dart', + uri: Uri.parse('package:test/test.dart'), + owner: null, + ); + + // Add dependencies to core library + final coreDependencies = [ + LibraryDependencyMirror( + isImport: true, + isDeferred: false, + sourceLibrary: coreLibrary, + targetLibrary: asyncLibrary, + prefix: null, + combinators: const [], + ), + LibraryDependencyMirror( + isImport: false, + isDeferred: false, + sourceLibrary: coreLibrary, + targetLibrary: asyncLibrary, + prefix: null, + combinators: const [], + ), + ]; + + // Update core library with dependencies + _libraries[coreLibrary.uri] = LibraryMirror( + name: 'dart:core', + uri: _createDartUri('core'), + owner: null, + declarations: const {}, + libraryDependencies: coreDependencies, + metadata: [], + ); + + // Add libraries to the map + _libraries[asyncLibrary.uri] = asyncLibrary; + _libraries[testLibrary.uri] = testLibrary; + } + + void _initializeSpecialTypes() { + _dynamicType = TypeMirror.dynamicType(); + _voidType = TypeMirror.voidType(); + _neverType = TypeMirror( + type: Never, + name: 'Never', + owner: null, + metadata: [], + ); + } + + void _initializeIsolate() { + _isolate = IsolateMirror.current(_rootLibrary); + } + + /// Creates a URI for a dart: library. + static Uri _createDartUri(String library) { + return Uri(scheme: 'dart', path: library); + } + + /// Parses a library name into a URI. + static Uri _parseLibraryName(String name) { + if (name.startsWith('"') && name.endsWith('"')) { + name = name.substring(1, name.length - 1); + } + + if (name.startsWith('dart:')) { + final library = name.substring(5); + return _createDartUri(library); + } + + return Uri.parse(name); + } + + @override + Map get libraries => Map.unmodifiable(_libraries); + + @override + LibraryMirrorContract findLibrary(Symbol libraryName) { + final name = libraryName.toString(); + // Remove leading 'Symbol(' and trailing ')' + final normalizedName = name.substring(7, name.length - 1); + + final uri = _parseLibraryName(normalizedName); + final library = _libraries[uri]; + if (library == null) { + throw ArgumentError('Library not found: $normalizedName'); + } + return library; + } + + @override + ClassMirrorContract reflectClass(Type type) { + return _classes.putIfAbsent( + type, + () => _createClassMirror(type), + ); + } + + ClassMirrorContract _createClassMirror(Type type) { + // Check if type is reflectable + if (!Reflector.isReflectable(type)) { + throw ArgumentError('Type is not reflectable: $type'); + } + + // Create temporary class mirror to serve as owner + final tempMirror = ClassMirror( + type: type, + name: type.toString(), + owner: null, + declarations: const {}, + instanceMembers: const {}, + staticMembers: const {}, + metadata: [], + ); + + // Get metadata from registry + final properties = Reflector.getPropertyMetadata(type) ?? {}; + final methods = Reflector.getMethodMetadata(type) ?? {}; + + // Create declarations map + final declarations = {}; + final instanceMembers = {}; + final staticMembers = {}; + + // Add properties and methods to declarations + properties.forEach((name, prop) { + declarations[Symbol(name)] = VariableMirror( + name: name, + type: TypeMirror( + type: prop.type, + name: prop.type.toString(), + owner: tempMirror, + metadata: [], + ), + owner: tempMirror, + isStatic: false, + isFinal: !prop.isWritable, + isConst: false, + metadata: [], + ); + }); + + methods.forEach((name, method) { + final methodMirror = MethodMirror( + name: name, + owner: tempMirror, + returnType: method.returnsVoid + ? TypeMirror.voidType(tempMirror) + : TypeMirror.dynamicType(tempMirror), + parameters: method.parameters + .map((param) => ParameterMirror( + name: param.name, + type: TypeMirror( + type: param.type, + name: param.type.toString(), + owner: tempMirror, + metadata: [], + ), + owner: tempMirror, + isOptional: !param.isRequired, + isNamed: param.isNamed, + metadata: [], + )) + .toList(), + isStatic: method.isStatic, + metadata: [], + ); + + declarations[Symbol(name)] = methodMirror; + if (method.isStatic) { + staticMembers[Symbol(name)] = methodMirror; + } else { + instanceMembers[Symbol(name)] = methodMirror; + } + }); + + // Create class mirror + final mirror = ClassMirror( + type: type, + name: type.toString(), + owner: null, + declarations: declarations, + instanceMembers: instanceMembers, + staticMembers: staticMembers, + metadata: [], + ); + + // Update owners to point to the real class mirror + declarations.forEach((_, decl) { + if (decl is MutableOwnerMirror) { + decl.setOwner(mirror); + } + }); + + return mirror; + } + + @override + TypeMirrorContract reflectType(Type type) { + return _getOrCreateTypeMirror(type); + } + + TypeMirrorContract _getOrCreateTypeMirror(Type type) { + return _types.putIfAbsent( + type, + () => TypeMirror( + type: type, + name: type.toString(), + owner: _rootLibrary, + metadata: const [], + ), + ); + } + + @override + IsolateMirrorContract get isolate => _isolate; + + @override + TypeMirrorContract get dynamicType => _dynamicType; + + @override + TypeMirrorContract get voidType => _voidType; + + @override + TypeMirrorContract get neverType => _neverType; + + /// Adds a library to the mirror system. + void addLibrary(LibraryMirrorContract library) { + _libraries[library.uri] = library; + } + + /// Removes a library from the mirror system. + void removeLibrary(Uri uri) { + _libraries.remove(uri); + } + + /// Creates a mirror reflecting [reflectee]. + InstanceMirrorContract reflect(Object reflectee) { + return InstanceMirror( + reflectee: reflectee, + type: reflectClass(reflectee.runtimeType), + ); + } +} + +/// The current mirror system. +MirrorSystemContract currentMirrorSystem() => MirrorSystem.current(); + +/// Reflects an instance. +InstanceMirrorContract reflect(Object reflectee) => + MirrorSystem.instance.reflect(reflectee); + +/// Reflects a class. +ClassMirrorContract reflectClass(Type key) => + MirrorSystem.instance.reflectClass(key); + +/// Reflects a type. +TypeMirrorContract reflectType(Type key) => + MirrorSystem.instance.reflectType(key); diff --git a/incubation/reflection/lib/src/mirrors/mirrors.dart b/incubation/reflection/lib/src/mirrors/mirrors.dart deleted file mode 100644 index d44fd2c..0000000 --- a/incubation/reflection/lib/src/mirrors/mirrors.dart +++ /dev/null @@ -1,15 +0,0 @@ -/// Mirror implementations for the reflection system. -library mirrors; - -export 'base_mirror.dart'; -export 'class_mirror_impl.dart'; -export 'combinator_mirror_impl.dart'; -export 'instance_mirror_impl.dart'; -export 'isolate_mirror_impl.dart'; -export 'library_dependency_mirror_impl.dart'; -export 'library_mirror_impl.dart'; -export 'method_mirror_impl.dart'; -export 'parameter_mirror_impl.dart'; -export 'special_types.dart'; -export 'type_mirror_impl.dart'; -export 'variable_mirror_impl.dart'; diff --git a/incubation/reflection/lib/src/mirrors/parameter_mirror_impl.dart b/incubation/reflection/lib/src/mirrors/parameter_mirror.dart similarity index 73% rename from incubation/reflection/lib/src/mirrors/parameter_mirror_impl.dart rename to incubation/reflection/lib/src/mirrors/parameter_mirror.dart index 650444c..55fa693 100644 --- a/incubation/reflection/lib/src/mirrors/parameter_mirror_impl.dart +++ b/incubation/reflection/lib/src/mirrors/parameter_mirror.dart @@ -1,32 +1,31 @@ import 'dart:core'; -import '../mirrors.dart'; -import 'base_mirror.dart'; -import 'type_mirror_impl.dart'; +import 'package:platform_contracts/contracts.dart'; +import 'package:platform_reflection/mirrors.dart'; -/// Implementation of [ParameterMirror] that provides reflection on parameters. -class ParameterMirrorImpl extends MutableOwnerMirror - implements ParameterMirror { +/// Implementation of [ParameterMirrorContract] that provides reflection on parameters. +class ParameterMirror extends MutableOwnerMirror + implements ParameterMirrorContract { final String _name; - final TypeMirror _type; + final TypeMirrorContract _type; final bool _isOptional; final bool _isNamed; final bool _hasDefaultValue; - final InstanceMirror? _defaultValue; + final InstanceMirrorContract? _defaultValue; final bool _isFinal; final bool _isConst; - final List _metadata; + final List _metadata; - ParameterMirrorImpl({ + ParameterMirror({ required String name, - required TypeMirror type, - required DeclarationMirror owner, + required TypeMirrorContract type, + required DeclarationMirrorContract owner, bool isOptional = false, bool isNamed = false, bool hasDefaultValue = false, - InstanceMirror? defaultValue, + InstanceMirrorContract? defaultValue, bool isFinal = false, bool isConst = false, - List metadata = const [], + List metadata = const [], }) : _name = name, _type = type, _isOptional = isOptional, @@ -58,7 +57,7 @@ class ParameterMirrorImpl extends MutableOwnerMirror bool get isTopLevel => false; @override - TypeMirror get type => _type; + TypeMirrorContract get type => _type; @override bool get isStatic => false; @@ -79,15 +78,15 @@ class ParameterMirrorImpl extends MutableOwnerMirror bool get hasDefaultValue => _hasDefaultValue; @override - InstanceMirror? get defaultValue => _defaultValue; + InstanceMirrorContract? get defaultValue => _defaultValue; @override - List get metadata => List.unmodifiable(_metadata); + List get metadata => List.unmodifiable(_metadata); @override bool operator ==(Object other) { if (identical(this, other)) return true; - if (other is! ParameterMirrorImpl) return false; + if (other is! ParameterMirror) return false; return _name == other._name && _type == other._type && diff --git a/incubation/reflection/lib/src/mirrors/mirror_system_impl.dart b/incubation/reflection/lib/src/mirrors/tem2.dart similarity index 67% rename from incubation/reflection/lib/src/mirrors/mirror_system_impl.dart rename to incubation/reflection/lib/src/mirrors/tem2.dart index 85e41a8..f90dc14 100644 --- a/incubation/reflection/lib/src/mirrors/mirror_system_impl.dart +++ b/incubation/reflection/lib/src/mirrors/tem2.dart @@ -1,33 +1,23 @@ import 'dart:core'; -import '../mirrors.dart'; -import '../core/reflector.dart'; -import 'type_mirror_impl.dart'; -import 'class_mirror_impl.dart'; -import 'library_mirror_impl.dart'; -import 'library_dependency_mirror_impl.dart'; -import 'isolate_mirror_impl.dart'; -import 'special_types.dart'; -import 'variable_mirror_impl.dart'; -import 'method_mirror_impl.dart'; -import 'parameter_mirror_impl.dart'; -import 'base_mirror.dart'; +import 'package:platform_contracts/contracts.dart'; +import 'package:platform_reflection/mirrors.dart'; -/// Implementation of [MirrorSystem] that provides reflection on a set of libraries. -class MirrorSystemImpl implements MirrorSystem { - final Map _libraries; - final IsolateMirror _isolate; - final TypeMirror _dynamicType; - final TypeMirror _voidType; - final TypeMirror _neverType; +/// Implementation of [MirrorSystemContract] that provides reflection on a set of libraries. +class MirrorSystem implements MirrorSystemContract { + final Map _libraries; + final IsolateMirrorContract _isolate; + final TypeMirrorContract _dynamicType; + final TypeMirrorContract _voidType; + final TypeMirrorContract _neverType; - MirrorSystemImpl({ - required Map libraries, - required IsolateMirror isolate, + MirrorSystem({ + required Map libraries, + required IsolateMirrorContract isolate, }) : _libraries = libraries, _isolate = isolate, - _dynamicType = TypeMirrorImpl.dynamicType(), - _voidType = TypeMirrorImpl.voidType(), - _neverType = TypeMirrorImpl( + _dynamicType = TypeMirror.dynamicType(), + _voidType = TypeMirror.voidType(), + _neverType = TypeMirror( type: Never, name: 'Never', owner: null, @@ -35,23 +25,23 @@ class MirrorSystemImpl implements MirrorSystem { ); /// Creates a mirror system for the current isolate. - factory MirrorSystemImpl.current() { + factory MirrorSystem.current() { // Create core library mirror - final coreLibrary = LibraryMirrorImpl.withDeclarations( + final coreLibrary = LibraryMirror.withDeclarations( name: 'dart:core', uri: _createDartUri('core'), owner: null, ); // Create async library mirror - final asyncLibrary = LibraryMirrorImpl.withDeclarations( + final asyncLibrary = LibraryMirror.withDeclarations( name: 'dart:async', uri: _createDartUri('async'), owner: null, ); // Create test library mirror - final testLibrary = LibraryMirrorImpl.withDeclarations( + final testLibrary = LibraryMirror.withDeclarations( name: 'package:test/test.dart', uri: Uri.parse('package:test/test.dart'), owner: null, @@ -59,7 +49,7 @@ class MirrorSystemImpl implements MirrorSystem { // Add dependencies to core library final coreDependencies = [ - LibraryDependencyMirrorImpl( + LibraryDependencyMirror( isImport: true, isDeferred: false, sourceLibrary: coreLibrary, @@ -67,7 +57,7 @@ class MirrorSystemImpl implements MirrorSystem { prefix: null, combinators: const [], ), - LibraryDependencyMirrorImpl( + LibraryDependencyMirror( isImport: false, isDeferred: false, sourceLibrary: coreLibrary, @@ -78,7 +68,7 @@ class MirrorSystemImpl implements MirrorSystem { ]; // Create root library with dependencies - final rootLibrary = LibraryMirrorImpl( + final rootLibrary = LibraryMirror( name: 'dart:core', uri: _createDartUri('core'), owner: null, @@ -88,16 +78,16 @@ class MirrorSystemImpl implements MirrorSystem { ); // Create isolate mirror - final isolate = IsolateMirrorImpl.current(rootLibrary); + final isolate = IsolateMirror.current(rootLibrary); // Create initial libraries map - final libraries = { + final libraries = { rootLibrary.uri: rootLibrary, asyncLibrary.uri: asyncLibrary, testLibrary.uri: testLibrary, }; - return MirrorSystemImpl( + return MirrorSystem( libraries: libraries, isolate: isolate, ); @@ -123,10 +113,10 @@ class MirrorSystemImpl implements MirrorSystem { } @override - Map get libraries => Map.unmodifiable(_libraries); + Map get libraries => Map.unmodifiable(_libraries); @override - LibraryMirror findLibrary(Symbol libraryName) { + LibraryMirrorContract findLibrary(Symbol libraryName) { final name = libraryName.toString(); // Remove leading 'Symbol(' and trailing ')' final normalizedName = name.substring(7, name.length - 1); @@ -140,14 +130,14 @@ class MirrorSystemImpl implements MirrorSystem { } @override - ClassMirror reflectClass(Type type) { + ClassMirrorContract reflectClass(Type type) { // Check if type is reflectable if (!Reflector.isReflectable(type)) { throw ArgumentError('Type is not reflectable: $type'); } // Create temporary class mirror to serve as owner - final tempMirror = ClassMirrorImpl( + final tempMirror = ClassMirror( type: type, name: type.toString(), owner: null, @@ -160,18 +150,18 @@ class MirrorSystemImpl implements MirrorSystem { // Get metadata from registry final properties = Reflector.getPropertyMetadata(type) ?? {}; final methods = Reflector.getMethodMetadata(type) ?? {}; - final constructors = Reflector.getConstructorMetadata(type) ?? []; + //final constructors = Reflector.getConstructorMetadata(type) ?? []; // Create declarations map - final declarations = {}; - final instanceMembers = {}; - final staticMembers = {}; + final declarations = {}; + final instanceMembers = {}; + final staticMembers = {}; // Add properties and methods to declarations properties.forEach((name, prop) { - declarations[Symbol(name)] = VariableMirrorImpl( + declarations[Symbol(name)] = VariableMirror( name: name, - type: TypeMirrorImpl( + type: TypeMirror( type: prop.type, name: prop.type.toString(), owner: tempMirror, @@ -186,16 +176,16 @@ class MirrorSystemImpl implements MirrorSystem { }); methods.forEach((name, method) { - final methodMirror = MethodMirrorImpl( + final methodMirror = MethodMirror( name: name, owner: tempMirror, returnType: method.returnsVoid - ? TypeMirrorImpl.voidType(tempMirror) - : TypeMirrorImpl.dynamicType(tempMirror), + ? TypeMirror.voidType(tempMirror) + : TypeMirror.dynamicType(tempMirror), parameters: method.parameters - .map((param) => ParameterMirrorImpl( + .map((param) => ParameterMirror( name: param.name, - type: TypeMirrorImpl( + type: TypeMirror( type: param.type, name: param.type.toString(), owner: tempMirror, @@ -220,7 +210,7 @@ class MirrorSystemImpl implements MirrorSystem { }); // Create class mirror - final mirror = ClassMirrorImpl( + final mirror = ClassMirror( type: type, name: type.toString(), owner: null, @@ -241,13 +231,13 @@ class MirrorSystemImpl implements MirrorSystem { } @override - TypeMirror reflectType(Type type) { + TypeMirrorContract reflectType(Type type) { // Check if type is reflectable if (!Reflector.isReflectable(type)) { throw ArgumentError('Type is not reflectable: $type'); } - return TypeMirrorImpl( + return TypeMirror( type: type, name: type.toString(), owner: null, @@ -256,19 +246,19 @@ class MirrorSystemImpl implements MirrorSystem { } @override - IsolateMirror get isolate => _isolate; + IsolateMirrorContract get isolate => _isolate; @override - TypeMirror get dynamicType => _dynamicType; + TypeMirrorContract get dynamicType => _dynamicType; @override - TypeMirror get voidType => _voidType; + TypeMirrorContract get voidType => _voidType; @override - TypeMirror get neverType => _neverType; + TypeMirrorContract get neverType => _neverType; /// Adds a library to the mirror system. - void addLibrary(LibraryMirror library) { + void addLibrary(LibraryMirrorContract library) { _libraries[library.uri] = library; } diff --git a/incubation/reflection/lib/src/mirrors/temp.dart b/incubation/reflection/lib/src/mirrors/temp.dart new file mode 100644 index 0000000..536f655 --- /dev/null +++ b/incubation/reflection/lib/src/mirrors/temp.dart @@ -0,0 +1,315 @@ +import 'dart:core'; +import 'package:platform_contracts/contracts.dart'; +import 'package:platform_reflection/mirrors.dart'; + +/// Implementation of [MirrorSystemContract] that provides reflection on a set of libraries. +class MirrorSystem implements MirrorSystemContract { + /// The singleton instance of the mirror system. + static final instance = MirrorSystem._(); + + final Map _libraries; + final Map _classes = {}; + final Map _types = {}; + late final LibraryMirrorContract _rootLibrary; + late final IsolateMirrorContract _isolate; + late final TypeMirrorContract _dynamicType; + late final TypeMirrorContract _voidType; + late final TypeMirrorContract _neverType; + + MirrorSystem._() : _libraries = {} { + _initializeSystem(); + } + + void _initializeSystem() { + _initializeRootLibrary(); + _initializeCoreDependencies(); + _initializeSpecialTypes(); + _initializeIsolate(); + } + + void _initializeRootLibrary() { + _rootLibrary = LibraryMirror.withDeclarations( + name: 'dart.core', + uri: Uri.parse('dart:core'), + ); + _libraries[_rootLibrary.uri] = _rootLibrary; + } + + void _initializeCoreDependencies() { + // Create core library mirror + final coreLibrary = LibraryMirror.withDeclarations( + name: 'dart:core', + uri: _createDartUri('core'), + owner: null, + ); + + // Create async library mirror + final asyncLibrary = LibraryMirror.withDeclarations( + name: 'dart:async', + uri: _createDartUri('async'), + owner: null, + ); + + // Create test library mirror + final testLibrary = LibraryMirror.withDeclarations( + name: 'package:test/test.dart', + uri: Uri.parse('package:test/test.dart'), + owner: null, + ); + + // Add dependencies to core library + final coreDependencies = [ + LibraryDependencyMirror( + isImport: true, + isDeferred: false, + sourceLibrary: coreLibrary, + targetLibrary: asyncLibrary, + prefix: null, + combinators: const [], + ), + LibraryDependencyMirror( + isImport: false, + isDeferred: false, + sourceLibrary: coreLibrary, + targetLibrary: asyncLibrary, + prefix: null, + combinators: const [], + ), + ]; + + // Update root library with dependencies + _rootLibrary = LibraryMirror( + name: 'dart:core', + uri: _createDartUri('core'), + owner: null, + declarations: const {}, + libraryDependencies: coreDependencies, + metadata: [], + ); + + // Add libraries to the map + _libraries[coreLibrary.uri] = coreLibrary; + _libraries[asyncLibrary.uri] = asyncLibrary; + _libraries[testLibrary.uri] = testLibrary; + } + + void _initializeSpecialTypes() { + _dynamicType = TypeMirror.dynamicType(); + _voidType = TypeMirror.voidType(); + _neverType = TypeMirror( + type: Never, + name: 'Never', + owner: null, + metadata: [], + ); + } + + void _initializeIsolate() { + _isolate = IsolateMirror.current(_rootLibrary); + } + + /// Creates a URI for a dart: library. + static Uri _createDartUri(String library) { + return Uri(scheme: 'dart', path: library); + } + + /// Parses a library name into a URI. + static Uri _parseLibraryName(String name) { + if (name.startsWith('"') && name.endsWith('"')) { + name = name.substring(1, name.length - 1); + } + + if (name.startsWith('dart:')) { + final library = name.substring(5); + return _createDartUri(library); + } + + return Uri.parse(name); + } + + @override + Map get libraries => Map.unmodifiable(_libraries); + + @override + LibraryMirrorContract findLibrary(Symbol libraryName) { + final name = libraryName.toString(); + // Remove leading 'Symbol(' and trailing ')' + final normalizedName = name.substring(7, name.length - 1); + + final uri = _parseLibraryName(normalizedName); + final library = _libraries[uri]; + if (library == null) { + throw ArgumentError('Library not found: $normalizedName'); + } + return library; + } + + @override + ClassMirrorContract reflectClass(Type type) { + return _classes.putIfAbsent( + type, + () => _createClassMirror(type), + ); + } + + ClassMirrorContract _createClassMirror(Type type) { + // Check if type is reflectable + if (!Reflector.isReflectable(type)) { + throw ArgumentError('Type is not reflectable: $type'); + } + + // Create temporary class mirror to serve as owner + final tempMirror = ClassMirror( + type: type, + name: type.toString(), + owner: null, + declarations: const {}, + instanceMembers: const {}, + staticMembers: const {}, + metadata: [], + ); + + // Get metadata from registry + final properties = Reflector.getPropertyMetadata(type) ?? {}; + final methods = Reflector.getMethodMetadata(type) ?? {}; + + // Create declarations map + final declarations = {}; + final instanceMembers = {}; + final staticMembers = {}; + + // Add properties and methods to declarations + properties.forEach((name, prop) { + declarations[Symbol(name)] = VariableMirror( + name: name, + type: TypeMirror( + type: prop.type, + name: prop.type.toString(), + owner: tempMirror, + metadata: [], + ), + owner: tempMirror, + isStatic: false, + isFinal: !prop.isWritable, + isConst: false, + metadata: [], + ); + }); + + methods.forEach((name, method) { + final methodMirror = MethodMirror( + name: name, + owner: tempMirror, + returnType: method.returnsVoid + ? TypeMirror.voidType(tempMirror) + : TypeMirror.dynamicType(tempMirror), + parameters: method.parameters + .map((param) => ParameterMirror( + name: param.name, + type: TypeMirror( + type: param.type, + name: param.type.toString(), + owner: tempMirror, + metadata: [], + ), + owner: tempMirror, + isOptional: !param.isRequired, + isNamed: param.isNamed, + metadata: [], + )) + .toList(), + isStatic: method.isStatic, + metadata: [], + ); + + declarations[Symbol(name)] = methodMirror; + if (method.isStatic) { + staticMembers[Symbol(name)] = methodMirror; + } else { + instanceMembers[Symbol(name)] = methodMirror; + } + }); + + // Create class mirror + final mirror = ClassMirror( + type: type, + name: type.toString(), + owner: null, + declarations: declarations, + instanceMembers: instanceMembers, + staticMembers: staticMembers, + metadata: [], + ); + + // Update owners to point to the real class mirror + declarations.forEach((_, decl) { + if (decl is MutableOwnerMirror) { + decl.setOwner(mirror); + } + }); + + return mirror; + } + + @override + TypeMirrorContract reflectType(Type type) { + return _getOrCreateTypeMirror(type); + } + + TypeMirrorContract _getOrCreateTypeMirror(Type type) { + return _types.putIfAbsent( + type, + () => TypeMirror( + type: type, + name: type.toString(), + owner: _rootLibrary, + metadata: const [], + ), + ); + } + + @override + IsolateMirrorContract get isolate => _isolate; + + @override + TypeMirrorContract get dynamicType => _dynamicType; + + @override + TypeMirrorContract get voidType => _voidType; + + @override + TypeMirrorContract get neverType => _neverType; + + /// Adds a library to the mirror system. + void addLibrary(LibraryMirrorContract library) { + _libraries[library.uri] = library; + } + + /// Removes a library from the mirror system. + void removeLibrary(Uri uri) { + _libraries.remove(uri); + } + + /// Creates a mirror reflecting [reflectee]. + InstanceMirrorContract reflect(Object reflectee) { + return InstanceMirror( + reflectee: reflectee, + type: reflectClass(reflectee.runtimeType), + ); + } +} + +/// The current mirror system. +MirrorSystemContract currentMirrorSystem() => MirrorSystem.instance; + +/// Reflects an instance. +InstanceMirrorContract reflect(Object reflectee) => + MirrorSystem.instance.reflect(reflectee); + +/// Reflects a class. +ClassMirrorContract reflectClass(Type key) => + MirrorSystem.instance.reflectClass(key); + +/// Reflects a type. +TypeMirrorContract reflectType(Type key) => + MirrorSystem.instance.reflectType(key); diff --git a/incubation/reflection/lib/src/mirrors/temp3.dart b/incubation/reflection/lib/src/mirrors/temp3.dart new file mode 100644 index 0000000..46d52bf --- /dev/null +++ b/incubation/reflection/lib/src/mirrors/temp3.dart @@ -0,0 +1,318 @@ +import 'dart:core'; +import 'package:platform_contracts/contracts.dart'; +import 'package:platform_reflection/mirrors.dart'; + +/// Implementation of [MirrorSystemContract] that provides reflection on a set of libraries. +class MirrorSystem implements MirrorSystemContract { + static MirrorSystem? _instance; + + static MirrorSystem get instance { + return _instance ??= MirrorSystem._(); + } + + final Map _libraries = {}; + final Map _classes = {}; + final Map _types = {}; + late final LibraryMirrorContract _rootLibrary; + late final IsolateMirrorContract _isolate; + late final TypeMirrorContract _dynamicType; + late final TypeMirrorContract _voidType; + late final TypeMirrorContract _neverType; + + // Private constructor + MirrorSystem._() { + _initializeSystem(); + } + + void _initializeSystem() { + _initializeRootLibrary(); + _initializeCoreDependencies(); + _initializeSpecialTypes(); + _initializeIsolate(); + } + + void _initializeRootLibrary() { + _rootLibrary = LibraryMirror.withDeclarations( + name: 'dart.core', + uri: Uri.parse('dart:core'), + ); + _libraries[_rootLibrary.uri] = _rootLibrary; + } + + void _initializeCoreDependencies() { + // Create core library mirror + final coreLibrary = LibraryMirror.withDeclarations( + name: 'dart:core', + uri: _createDartUri('core'), + owner: null, + ); + + // Create async library mirror + final asyncLibrary = LibraryMirror.withDeclarations( + name: 'dart:async', + uri: _createDartUri('async'), + owner: null, + ); + + // Create test library mirror + final testLibrary = LibraryMirror.withDeclarations( + name: 'package:test/test.dart', + uri: Uri.parse('package:test/test.dart'), + owner: null, + ); + + // Add dependencies to core library + final coreDependencies = [ + LibraryDependencyMirror( + isImport: true, + isDeferred: false, + sourceLibrary: coreLibrary, + targetLibrary: asyncLibrary, + prefix: null, + combinators: const [], + ), + LibraryDependencyMirror( + isImport: false, + isDeferred: false, + sourceLibrary: coreLibrary, + targetLibrary: asyncLibrary, + prefix: null, + combinators: const [], + ), + ]; + + // Update core library with dependencies + _libraries[coreLibrary.uri] = LibraryMirror( + name: 'dart:core', + uri: _createDartUri('core'), + owner: null, + declarations: const {}, + libraryDependencies: coreDependencies, + metadata: [], + ); + + // Add libraries to the map + _libraries[asyncLibrary.uri] = asyncLibrary; + _libraries[testLibrary.uri] = testLibrary; + } + + void _initializeSpecialTypes() { + _dynamicType = TypeMirror.dynamicType(); + _voidType = TypeMirror.voidType(); + _neverType = TypeMirror( + type: Never, + name: 'Never', + owner: null, + metadata: [], + ); + } + + void _initializeIsolate() { + _isolate = IsolateMirror.current(_rootLibrary); + } + + /// Creates a URI for a dart: library. + static Uri _createDartUri(String library) { + return Uri(scheme: 'dart', path: library); + } + + /// Parses a library name into a URI. + static Uri _parseLibraryName(String name) { + if (name.startsWith('"') && name.endsWith('"')) { + name = name.substring(1, name.length - 1); + } + + if (name.startsWith('dart:')) { + final library = name.substring(5); + return _createDartUri(library); + } + + return Uri.parse(name); + } + + @override + Map get libraries => Map.unmodifiable(_libraries); + + @override + LibraryMirrorContract findLibrary(Symbol libraryName) { + final name = libraryName.toString(); + // Remove leading 'Symbol(' and trailing ')' + final normalizedName = name.substring(7, name.length - 1); + + final uri = _parseLibraryName(normalizedName); + final library = _libraries[uri]; + if (library == null) { + throw ArgumentError('Library not found: $normalizedName'); + } + return library; + } + + @override + ClassMirrorContract reflectClass(Type type) { + return _classes.putIfAbsent( + type, + () => _createClassMirror(type), + ); + } + + ClassMirrorContract _createClassMirror(Type type) { + // Check if type is reflectable + if (!Reflector.isReflectable(type)) { + throw ArgumentError('Type is not reflectable: $type'); + } + + // Create temporary class mirror to serve as owner + final tempMirror = ClassMirror( + type: type, + name: type.toString(), + owner: null, + declarations: const {}, + instanceMembers: const {}, + staticMembers: const {}, + metadata: [], + ); + + // Get metadata from registry + final properties = Reflector.getPropertyMetadata(type) ?? {}; + final methods = Reflector.getMethodMetadata(type) ?? {}; + + // Create declarations map + final declarations = {}; + final instanceMembers = {}; + final staticMembers = {}; + + // Add properties and methods to declarations + properties.forEach((name, prop) { + declarations[Symbol(name)] = VariableMirror( + name: name, + type: TypeMirror( + type: prop.type, + name: prop.type.toString(), + owner: tempMirror, + metadata: [], + ), + owner: tempMirror, + isStatic: false, + isFinal: !prop.isWritable, + isConst: false, + metadata: [], + ); + }); + + methods.forEach((name, method) { + final methodMirror = MethodMirror( + name: name, + owner: tempMirror, + returnType: method.returnsVoid + ? TypeMirror.voidType(tempMirror) + : TypeMirror.dynamicType(tempMirror), + parameters: method.parameters + .map((param) => ParameterMirror( + name: param.name, + type: TypeMirror( + type: param.type, + name: param.type.toString(), + owner: tempMirror, + metadata: [], + ), + owner: tempMirror, + isOptional: !param.isRequired, + isNamed: param.isNamed, + metadata: [], + )) + .toList(), + isStatic: method.isStatic, + metadata: [], + ); + + declarations[Symbol(name)] = methodMirror; + if (method.isStatic) { + staticMembers[Symbol(name)] = methodMirror; + } else { + instanceMembers[Symbol(name)] = methodMirror; + } + }); + + // Create class mirror + final mirror = ClassMirror( + type: type, + name: type.toString(), + owner: null, + declarations: declarations, + instanceMembers: instanceMembers, + staticMembers: staticMembers, + metadata: [], + ); + + // Update owners to point to the real class mirror + declarations.forEach((_, decl) { + if (decl is MutableOwnerMirror) { + decl.setOwner(mirror); + } + }); + + return mirror; + } + + @override + TypeMirrorContract reflectType(Type type) { + return _getOrCreateTypeMirror(type); + } + + TypeMirrorContract _getOrCreateTypeMirror(Type type) { + return _types.putIfAbsent( + type, + () => TypeMirror( + type: type, + name: type.toString(), + owner: _rootLibrary, + metadata: const [], + ), + ); + } + + @override + IsolateMirrorContract get isolate => _isolate; + + @override + TypeMirrorContract get dynamicType => _dynamicType; + + @override + TypeMirrorContract get voidType => _voidType; + + @override + TypeMirrorContract get neverType => _neverType; + + /// Adds a library to the mirror system. + void addLibrary(LibraryMirrorContract library) { + _libraries[library.uri] = library; + } + + /// Removes a library from the mirror system. + void removeLibrary(Uri uri) { + _libraries.remove(uri); + } + + /// Creates a mirror reflecting [reflectee]. + InstanceMirrorContract reflect(Object reflectee) { + return InstanceMirror( + reflectee: reflectee, + type: reflectClass(reflectee.runtimeType), + ); + } +} + +/// The current mirror system. +MirrorSystemContract currentMirrorSystem() => MirrorSystem.instance; + +/// Reflects an instance. +InstanceMirrorContract reflect(Object reflectee) => + MirrorSystem.instance.reflect(reflectee); + +/// Reflects a class. +ClassMirrorContract reflectClass(Type key) => + MirrorSystem.instance.reflectClass(key); + +/// Reflects a type. +TypeMirrorContract reflectType(Type key) => + MirrorSystem.instance.reflectType(key); diff --git a/incubation/reflection/lib/src/mirrors/type_mirror_impl.dart b/incubation/reflection/lib/src/mirrors/type_mirror.dart similarity index 77% rename from incubation/reflection/lib/src/mirrors/type_mirror_impl.dart rename to incubation/reflection/lib/src/mirrors/type_mirror.dart index d26e1b0..7751af7 100644 --- a/incubation/reflection/lib/src/mirrors/type_mirror_impl.dart +++ b/incubation/reflection/lib/src/mirrors/type_mirror.dart @@ -1,28 +1,25 @@ import 'dart:core'; -import '../mirrors.dart'; -import '../core/reflector.dart'; -import '../metadata.dart'; -import 'base_mirror.dart'; -import 'special_types.dart'; -import 'type_variable_mirror_impl.dart'; +import 'package:platform_contracts/contracts.dart' + hide PropertyMetadata, MethodMetadata, ConstructorMetadata; +import 'package:platform_reflection/mirrors.dart'; -/// Implementation of [TypeMirror] that provides reflection on types. -class TypeMirrorImpl extends TypedMirror implements TypeMirror { - final List _typeVariables; - final List _typeArguments; +/// Implementation of [TypeMirrorContract] that provides reflection on types. +class TypeMirror extends TypedMirror implements TypeMirrorContract { + final List _typeVariables; + final List _typeArguments; final bool _isOriginalDeclaration; - final TypeMirror? _originalDeclaration; + final TypeMirrorContract? _originalDeclaration; final bool _isGeneric; - TypeMirrorImpl({ + TypeMirror({ required Type type, required String name, - DeclarationMirror? owner, - List typeVariables = const [], - List typeArguments = const [], + DeclarationMirrorContract? owner, + List typeVariables = const [], + List typeArguments = const [], bool isOriginalDeclaration = true, - TypeMirror? originalDeclaration, - List metadata = const [], + TypeMirrorContract? originalDeclaration, + List metadata = const [], }) : _typeVariables = typeVariables, _typeArguments = typeArguments, _isOriginalDeclaration = isOriginalDeclaration, @@ -46,19 +43,19 @@ class TypeMirrorImpl extends TypedMirror implements TypeMirror { } /// Creates a TypeMirror from TypeMetadata. - factory TypeMirrorImpl.fromMetadata(TypeMetadata typeMetadata, - [DeclarationMirror? owner]) { + factory TypeMirror.fromMetadata(TypeMetadata typeMetadata, + [DeclarationMirrorContract? owner]) { // Get type variables from metadata final typeVariables = typeMetadata.typeParameters.map((param) { // Create upper bound type mirror - final upperBound = TypeMirrorImpl( + final upperBound = TypeMirror( type: param.bound ?? Object, name: param.bound?.toString() ?? 'Object', owner: owner, ); // Create type variable mirror - return TypeVariableMirrorImpl( + return TypeVariableMirror( type: param.type, name: param.name, upperBound: upperBound, @@ -68,14 +65,14 @@ class TypeMirrorImpl extends TypedMirror implements TypeMirror { // Get type arguments from metadata final typeArguments = typeMetadata.typeArguments.map((arg) { - return TypeMirrorImpl( + return TypeMirror( type: arg.type, name: arg.name, owner: owner, ); }).toList(); - return TypeMirrorImpl( + return TypeMirror( type: typeMetadata.type, name: typeMetadata.name, owner: owner, @@ -86,8 +83,8 @@ class TypeMirrorImpl extends TypedMirror implements TypeMirror { } /// Creates a TypeMirror for void. - factory TypeMirrorImpl.voidType([DeclarationMirror? owner]) { - return TypeMirrorImpl( + factory TypeMirror.voidType([DeclarationMirrorContract? owner]) { + return TypeMirror( type: voidType, name: 'void', owner: owner, @@ -96,8 +93,8 @@ class TypeMirrorImpl extends TypedMirror implements TypeMirror { } /// Creates a TypeMirror for dynamic. - factory TypeMirrorImpl.dynamicType([DeclarationMirror? owner]) { - return TypeMirrorImpl( + factory TypeMirror.dynamicType([DeclarationMirrorContract? owner]) { + return TypeMirror( type: dynamicType, name: 'dynamic', owner: owner, @@ -106,7 +103,8 @@ class TypeMirrorImpl extends TypedMirror implements TypeMirror { } /// Creates a new TypeMirror with the given type arguments. - TypeMirror instantiateGeneric(List typeArguments) { + TypeMirrorContract instantiateGeneric( + List typeArguments) { if (!_isGeneric) { throw StateError('Type $name is not generic'); } @@ -126,7 +124,7 @@ class TypeMirrorImpl extends TypedMirror implements TypeMirror { } } - return TypeMirrorImpl( + return TypeMirror( type: type, name: name, owner: owner, @@ -145,17 +143,18 @@ class TypeMirrorImpl extends TypedMirror implements TypeMirror { Type get reflectedType => type; @override - List get typeVariables => + List get typeVariables => List.unmodifiable(_typeVariables); @override - List get typeArguments => List.unmodifiable(_typeArguments); + List get typeArguments => + List.unmodifiable(_typeArguments); @override bool get isOriginalDeclaration => _isOriginalDeclaration; @override - TypeMirror get originalDeclaration { + TypeMirrorContract get originalDeclaration { if (isOriginalDeclaration) return this; return _originalDeclaration!; } @@ -176,9 +175,9 @@ class TypeMirrorImpl extends TypedMirror implements TypeMirror { Reflector.getConstructorMetadata(type) ?? []; @override - bool isSubtypeOf(TypeMirror other) { + bool isSubtypeOf(TypeMirrorContract other) { if (this == other) return true; - if (other is! TypeMirrorImpl) return false; + if (other is! TypeMirror) return false; // Never is a subtype of all types if (type == Never) return true; @@ -195,19 +194,19 @@ class TypeMirrorImpl extends TypedMirror implements TypeMirror { // Check supertype if (metadata.supertype != null) { - final superMirror = TypeMirrorImpl.fromMetadata(metadata.supertype!); + final superMirror = TypeMirror.fromMetadata(metadata.supertype!); if (superMirror.isSubtypeOf(other)) return true; } // Check interfaces for (final interface in metadata.interfaces) { - final interfaceMirror = TypeMirrorImpl.fromMetadata(interface); + final interfaceMirror = TypeMirror.fromMetadata(interface); if (interfaceMirror.isSubtypeOf(other)) return true; } // Check mixins for (final mixin in metadata.mixins) { - final mixinMirror = TypeMirrorImpl.fromMetadata(mixin); + final mixinMirror = TypeMirror.fromMetadata(mixin); if (mixinMirror.isSubtypeOf(other)) return true; } @@ -234,13 +233,11 @@ class TypeMirrorImpl extends TypedMirror implements TypeMirror { } @override - bool isAssignableTo(TypeMirror other) { + bool isAssignableTo(TypeMirrorContract other) { // A type T may be assigned to a type S if either: // 1. T is a subtype of S, or // 2. S is dynamic (except for void) - if (other is TypeMirrorImpl && - other.type == dynamicType && - type != voidType) { + if (other is TypeMirror && other.type == dynamicType && type != voidType) { return true; } return isSubtypeOf(other); @@ -249,7 +246,7 @@ class TypeMirrorImpl extends TypedMirror implements TypeMirror { @override bool operator ==(Object other) { if (identical(this, other)) return true; - if (other is! TypeMirrorImpl) return false; + if (other is! TypeMirror) return false; return type == other.type && name == other.name && diff --git a/incubation/reflection/lib/src/mirrors/type_variable_mirror_impl.dart b/incubation/reflection/lib/src/mirrors/type_variable_mirror.dart similarity index 59% rename from incubation/reflection/lib/src/mirrors/type_variable_mirror_impl.dart rename to incubation/reflection/lib/src/mirrors/type_variable_mirror.dart index cd8d45d..bf09093 100644 --- a/incubation/reflection/lib/src/mirrors/type_variable_mirror_impl.dart +++ b/incubation/reflection/lib/src/mirrors/type_variable_mirror.dart @@ -1,18 +1,18 @@ -import '../mirrors.dart'; -import '../metadata.dart'; -import 'base_mirror.dart'; -import 'type_mirror_impl.dart'; +import 'package:platform_contracts/contracts.dart' + hide PropertyMetadata, MethodMetadata, ConstructorMetadata; +import 'package:platform_reflection/mirrors.dart'; -/// Implementation of [TypeVariableMirror] that provides reflection on type variables. -class TypeVariableMirrorImpl extends TypedMirror implements TypeVariableMirror { - final TypeMirror _upperBound; +/// Implementation of [TypeVariableMirrorContract] that provides reflection on type variables. +class TypeVariableMirror extends TypedMirror + implements TypeVariableMirrorContract { + final TypeMirrorContract _upperBound; - TypeVariableMirrorImpl({ + TypeVariableMirror({ required Type type, required String name, - required TypeMirror upperBound, - DeclarationMirror? owner, - List metadata = const [], + required TypeMirrorContract upperBound, + DeclarationMirrorContract? owner, + List metadata = const [], }) : _upperBound = upperBound, super( type: type, @@ -22,7 +22,7 @@ class TypeVariableMirrorImpl extends TypedMirror implements TypeVariableMirror { ); @override - TypeMirror get upperBound => _upperBound; + TypeMirrorContract get upperBound => _upperBound; @override bool get hasReflectedType => true; @@ -31,42 +31,39 @@ class TypeVariableMirrorImpl extends TypedMirror implements TypeVariableMirror { Type get reflectedType => type; @override - List get typeVariables => const []; + List get typeVariables => const []; @override - List get typeArguments => const []; + List get typeArguments => const []; @override bool get isOriginalDeclaration => true; @override - TypeMirror get originalDeclaration => this; + TypeMirrorContract get originalDeclaration => this; @override - bool isSubtypeOf(TypeMirror other) { + bool isSubtypeOf(TypeMirrorContract other) { if (identical(this, other)) return true; return _upperBound.isSubtypeOf(other); } @override - bool isAssignableTo(TypeMirror other) { + bool isAssignableTo(TypeMirrorContract other) { if (identical(this, other)) return true; return _upperBound.isAssignableTo(other); } - @override Map get properties => const {}; - @override Map get methods => const {}; - @override List get constructors => const []; @override bool operator ==(Object other) { if (identical(this, other)) return true; - if (other is! TypeVariableMirrorImpl) return false; + if (other is! TypeVariableMirror) return false; return type == other.type && name == other.name && diff --git a/incubation/reflection/lib/src/mirrors/variable_mirror_impl.dart b/incubation/reflection/lib/src/mirrors/variable_mirror.dart similarity index 75% rename from incubation/reflection/lib/src/mirrors/variable_mirror_impl.dart rename to incubation/reflection/lib/src/mirrors/variable_mirror.dart index f1c260d..3c30c59 100644 --- a/incubation/reflection/lib/src/mirrors/variable_mirror_impl.dart +++ b/incubation/reflection/lib/src/mirrors/variable_mirror.dart @@ -1,25 +1,25 @@ import 'dart:core'; -import '../mirrors.dart'; -import 'base_mirror.dart'; -import 'type_mirror_impl.dart'; +import 'package:platform_contracts/contracts.dart'; +import 'package:platform_reflection/mirrors.dart'; -/// Implementation of [VariableMirror] that provides reflection on variables. -class VariableMirrorImpl extends MutableOwnerMirror implements VariableMirror { - final TypeMirror _type; +/// Implementation of [VariableMirrorContract] that provides reflection on variables. +class VariableMirror extends MutableOwnerMirror + implements VariableMirrorContract { + final TypeMirrorContract _type; final String _name; final bool _isStatic; final bool _isFinal; final bool _isConst; - final List _metadata; + final List _metadata; - VariableMirrorImpl({ + VariableMirror({ required String name, - required TypeMirror type, - DeclarationMirror? owner, + required TypeMirrorContract type, + DeclarationMirrorContract? owner, bool isStatic = false, bool isFinal = false, bool isConst = false, - List metadata = const [], + List metadata = const [], }) : _name = name, _type = type, _isStatic = isStatic, @@ -45,10 +45,10 @@ class VariableMirrorImpl extends MutableOwnerMirror implements VariableMirror { bool get isPrivate => _name.startsWith('_'); @override - bool get isTopLevel => owner is LibraryMirror; + bool get isTopLevel => owner is LibraryMirrorContract; @override - TypeMirror get type => _type; + TypeMirrorContract get type => _type; @override bool get isStatic => _isStatic; @@ -60,12 +60,12 @@ class VariableMirrorImpl extends MutableOwnerMirror implements VariableMirror { bool get isConst => _isConst; @override - List get metadata => List.unmodifiable(_metadata); + List get metadata => List.unmodifiable(_metadata); @override bool operator ==(Object other) { if (identical(this, other)) return true; - if (other is! VariableMirrorImpl) return false; + if (other is! VariableMirror) return false; return _name == other._name && _type == other._type && @@ -98,21 +98,21 @@ class VariableMirrorImpl extends MutableOwnerMirror implements VariableMirror { } } -/// Implementation of [VariableMirror] specifically for fields. -class FieldMirrorImpl extends VariableMirrorImpl { +/// Implementation of [VariableMirrorContract] specifically for fields. +class FieldMirrorImpl extends VariableMirror { final bool _isReadable; final bool _isWritable; FieldMirrorImpl({ required String name, - required TypeMirror type, - DeclarationMirror? owner, + required TypeMirrorContract type, + DeclarationMirrorContract? owner, bool isStatic = false, bool isFinal = false, bool isConst = false, bool isReadable = true, bool isWritable = true, - List metadata = const [], + List metadata = const [], }) : _isReadable = isReadable, _isWritable = isWritable, super( diff --git a/incubation/reflection/lib/src/types.dart b/incubation/reflection/lib/src/types.dart deleted file mode 100644 index 11cff67..0000000 --- a/incubation/reflection/lib/src/types.dart +++ /dev/null @@ -1,19 +0,0 @@ -/// Represents the void type in our reflection system. -class VoidType implements Type { - const VoidType._(); - - /// The singleton instance representing void. - static const instance = VoidType._(); - - @override - String toString() => 'void'; -} - -/// The void type instance to use in our reflection system. -const voidType = VoidType.instance; - -/// Extension to check if a Type is void. -extension TypeExtensions on Type { - /// Whether this type represents void. - bool get isVoid => this == voidType; -} diff --git a/incubation/reflection/test/isolate_reflection_test.dart b/incubation/reflection/test/isolate_reflection_test.dart index e767e52..8b792e7 100644 --- a/incubation/reflection/test/isolate_reflection_test.dart +++ b/incubation/reflection/test/isolate_reflection_test.dart @@ -1,5 +1,5 @@ import 'dart:isolate'; -import 'package:platform_reflection/reflection.dart'; +import 'package:platform_reflection/mirrors.dart'; import 'package:test/test.dart'; // Function to run in isolate @@ -50,8 +50,8 @@ void main() { receivePort.sendPort, ); - final isolateMirror = reflector.reflectIsolate(isolate, 'test-isolate') - as IsolateMirrorImpl; + final isolateMirror = + reflector.reflectIsolate(isolate, 'test-isolate') as IsolateMirror; // Test pause/resume await isolateMirror.pause(); diff --git a/incubation/reflection/test/library_reflection_test.dart b/incubation/reflection/test/library_reflection_test.dart index 5e78d81..c0cfa11 100644 --- a/incubation/reflection/test/library_reflection_test.dart +++ b/incubation/reflection/test/library_reflection_test.dart @@ -1,4 +1,5 @@ -import 'package:platform_reflection/reflection.dart'; +import 'package:platform_contracts/contracts.dart'; +import 'package:platform_reflection/mirrors.dart'; import 'package:test/test.dart'; // Top-level function for testing @@ -44,14 +45,15 @@ void main() { expect(declarations, isNotEmpty); // Check for top-level function - final addFunction = declarations[const Symbol('add')] as MethodMirror; + final addFunction = + declarations[const Symbol('add')] as MethodMirrorContract; expect(addFunction, isNotNull); expect(addFunction.isStatic, isTrue); expect(addFunction.parameters.length, equals(2)); // Check for top-level variable final greetingVar = - declarations[const Symbol('greeting')] as VariableMirror; + declarations[const Symbol('greeting')] as VariableMirrorContract; expect(greetingVar, isNotNull); expect(greetingVar.isStatic, isTrue); expect(greetingVar.isConst, isTrue); diff --git a/incubation/reflection/test/mirror_system_test.dart b/incubation/reflection/test/mirror_system_test.dart index c15bbff..7b84b3c 100644 --- a/incubation/reflection/test/mirror_system_test.dart +++ b/incubation/reflection/test/mirror_system_test.dart @@ -1,4 +1,5 @@ -import 'package:platform_reflection/reflection.dart'; +import 'package:platform_contracts/contracts.dart' hide PropertyMetadata; +import 'package:platform_reflection/mirrors.dart'; import 'package:test/test.dart'; @reflectable @@ -9,12 +10,12 @@ class TestClass { void main() { group('MirrorSystem', () { - late RuntimeReflector reflector; - late MirrorSystem mirrorSystem; + //late RuntimeReflector reflector; + late MirrorSystemContract mirrorSystem; setUp(() { - reflector = RuntimeReflector.instance; - mirrorSystem = reflector.currentMirrorSystem; + //reflector = RuntimeReflector.instance; + mirrorSystem = MirrorSystem.instance; // Register test class Reflector.registerType(TestClass); diff --git a/incubation/reflection/test/reflection_test.dart b/incubation/reflection/test/reflection_test.dart index 522443a..17d63b1 100644 --- a/incubation/reflection/test/reflection_test.dart +++ b/incubation/reflection/test/reflection_test.dart @@ -1,4 +1,5 @@ -import 'package:platform_reflection/reflection.dart'; +import 'package:platform_contracts/contracts.dart'; +import 'package:platform_reflection/mirrors.dart'; import 'package:test/test.dart'; @reflectable @@ -53,7 +54,7 @@ void main() { group('Property Access', () { late Person person; - late InstanceMirror mirror; + late InstanceMirrorContract mirror; setUp(() { Reflector.register(Person); @@ -90,7 +91,7 @@ void main() { group('Method Invocation', () { late Person person; - late InstanceMirror mirror; + late InstanceMirrorContract mirror; setUp(() { Reflector.register(Person); diff --git a/incubation/reflection/test/scanner_test.dart b/incubation/reflection/test/scanner_test.dart index f1de0f6..2179dfb 100644 --- a/incubation/reflection/test/scanner_test.dart +++ b/incubation/reflection/test/scanner_test.dart @@ -1,4 +1,4 @@ -import 'package:platform_reflection/reflection.dart'; +import 'package:platform_reflection/mirrors.dart'; import 'package:test/test.dart'; @reflectable diff --git a/packages/collections/lib/src/higher_order_collection_proxy.dart b/packages/collections/lib/src/higher_order_collection_proxy.dart index 19a1ce0..f47198c 100644 --- a/packages/collections/lib/src/higher_order_collection_proxy.dart +++ b/packages/collections/lib/src/higher_order_collection_proxy.dart @@ -1,4 +1,4 @@ -import 'package:platform_reflection/reflection.dart'; +import 'package:platform_reflection/mirrors.dart'; import 'collection.dart'; /// A proxy class for higher-order collection operations. diff --git a/packages/collections/test/higher_order_collection_proxy_test.dart b/packages/collections/test/higher_order_collection_proxy_test.dart index 3d71fe7..1151622 100644 --- a/packages/collections/test/higher_order_collection_proxy_test.dart +++ b/packages/collections/test/higher_order_collection_proxy_test.dart @@ -1,4 +1,4 @@ -import 'package:platform_reflection/reflection.dart'; +import 'package:platform_reflection/mirrors.dart'; import 'package:test/test.dart'; import 'package:platform_collections/src/collection.dart'; import 'package:platform_collections/src/higher_order_collection_proxy.dart'; diff --git a/packages/contracts/lib/src/reflection/base.dart b/packages/contracts/lib/src/reflection/base.dart index d47e38c..0c7e5ed 100644 --- a/packages/contracts/lib/src/reflection/base.dart +++ b/packages/contracts/lib/src/reflection/base.dart @@ -8,10 +8,10 @@ */ /// Base mirror interface -abstract class Mirror {} +abstract class MirrorContract {} /// Base declaration mirror interface -abstract class DeclarationMirror implements Mirror { +abstract class DeclarationMirrorContract implements MirrorContract { /// The simple name for this Dart language entity. Symbol get simpleName; @@ -19,7 +19,7 @@ abstract class DeclarationMirror implements Mirror { Symbol get qualifiedName; /// A mirror on the owner of this Dart language entity. - DeclarationMirror? get owner; + DeclarationMirrorContract? get owner; /// Whether this declaration is library private. bool get isPrivate; @@ -28,29 +28,30 @@ abstract class DeclarationMirror implements Mirror { bool get isTopLevel; /// A list of the metadata associated with this declaration. - List get metadata; + List get metadata; /// The name of this declaration. String get name; } /// Base object mirror interface -abstract class ObjectMirror implements Mirror { +abstract class ObjectMirrorContract implements MirrorContract { /// Invokes the named function and returns a mirror on the result. - InstanceMirror invoke(Symbol memberName, List positionalArguments, + InstanceMirrorContract invoke( + Symbol memberName, List positionalArguments, [Map namedArguments = const {}]); /// Invokes a getter and returns a mirror on the result. - InstanceMirror getField(Symbol fieldName); + InstanceMirrorContract getField(Symbol fieldName); /// Invokes a setter and returns a mirror on the result. - InstanceMirror setField(Symbol fieldName, dynamic value); + InstanceMirrorContract setField(Symbol fieldName, dynamic value); } /// Base instance mirror interface -abstract class InstanceMirror implements ObjectMirror { +abstract class InstanceMirrorContract implements ObjectMirrorContract { /// A mirror on the type of the instance. - ClassMirror get type; + ClassMirrorContract get type; /// Whether this mirror's reflectee is accessible. bool get hasReflectee; @@ -60,12 +61,13 @@ abstract class InstanceMirror implements ObjectMirror { } /// Base class mirror interface -abstract class ClassMirror implements TypeMirror, ObjectMirror { +abstract class ClassMirrorContract + implements TypeMirrorContract, ObjectMirrorContract { /// A mirror on the superclass. - ClassMirror? get superclass; + ClassMirrorContract? get superclass; /// Mirrors on the superinterfaces. - List get superinterfaces; + List get superinterfaces; /// Whether this class is abstract. bool get isAbstract; @@ -74,25 +76,25 @@ abstract class ClassMirror implements TypeMirror, ObjectMirror { bool get isEnum; /// The declarations in this class. - Map get declarations; + Map get declarations; /// The instance members of this class. - Map get instanceMembers; + Map get instanceMembers; /// The static members of this class. - Map get staticMembers; + Map get staticMembers; /// Creates a new instance using the specified constructor. - InstanceMirror newInstance( + InstanceMirrorContract newInstance( Symbol constructorName, List positionalArguments, [Map namedArguments = const {}]); /// Whether this class is a subclass of [other]. - bool isSubclassOf(ClassMirror other); + bool isSubclassOf(ClassMirrorContract other); } /// Base type mirror interface -abstract class TypeMirror implements DeclarationMirror { +abstract class TypeMirrorContract implements DeclarationMirrorContract { /// Whether this mirror reflects a type available at runtime. bool get hasReflectedType; @@ -100,34 +102,34 @@ abstract class TypeMirror implements DeclarationMirror { Type get reflectedType; /// Type variables declared on this type. - List get typeVariables; + List get typeVariables; /// Type arguments provided to this type. - List get typeArguments; + List get typeArguments; /// Whether this is the original declaration of this type. bool get isOriginalDeclaration; /// A mirror on the original declaration of this type. - TypeMirror get originalDeclaration; + TypeMirrorContract get originalDeclaration; /// Checks if this type is a subtype of [other]. - bool isSubtypeOf(TypeMirror other); + bool isSubtypeOf(TypeMirrorContract other); /// Checks if this type is assignable to [other]. - bool isAssignableTo(TypeMirror other); + bool isAssignableTo(TypeMirrorContract other); } /// Base method mirror interface -abstract class MethodMirror implements DeclarationMirror { +abstract class MethodMirrorContract implements DeclarationMirrorContract { /// A mirror on the return type. - TypeMirror get returnType; + TypeMirrorContract get returnType; /// The source code if available. String? get source; /// Mirrors on the parameters. - List get parameters; + List get parameters; /// Whether this is a static method. bool get isStatic; @@ -170,7 +172,7 @@ abstract class MethodMirror implements DeclarationMirror { } /// Base parameter mirror interface -abstract class ParameterMirror implements VariableMirror { +abstract class ParameterMirrorContract implements VariableMirrorContract { /// Whether this is an optional parameter. bool get isOptional; @@ -181,13 +183,13 @@ abstract class ParameterMirror implements VariableMirror { bool get hasDefaultValue; /// The default value if this is an optional parameter. - InstanceMirror? get defaultValue; + InstanceMirrorContract? get defaultValue; } /// Base variable mirror interface -abstract class VariableMirror implements DeclarationMirror { +abstract class VariableMirrorContract implements DeclarationMirrorContract { /// A mirror on the type of this variable. - TypeMirror get type; + TypeMirrorContract get type; /// Whether this is a static variable. bool get isStatic; @@ -200,25 +202,26 @@ abstract class VariableMirror implements DeclarationMirror { } /// Base type variable mirror interface -abstract class TypeVariableMirror implements TypeMirror { +abstract class TypeVariableMirrorContract implements TypeMirrorContract { /// A mirror on the upper bound of this type variable. - TypeMirror get upperBound; + TypeMirrorContract get upperBound; } /// Base library mirror interface -abstract class LibraryMirror implements DeclarationMirror, ObjectMirror { +abstract class LibraryMirrorContract + implements DeclarationMirrorContract, ObjectMirrorContract { /// The absolute URI of the library. Uri get uri; /// The declarations in this library. - Map get declarations; + Map get declarations; /// The imports and exports of this library. - List get libraryDependencies; + List get libraryDependencies; } /// Base library dependency mirror interface -abstract class LibraryDependencyMirror implements Mirror { +abstract class LibraryDependencyMirrorContract implements MirrorContract { /// Whether this is an import. bool get isImport; @@ -229,20 +232,20 @@ abstract class LibraryDependencyMirror implements Mirror { bool get isDeferred; /// The library containing this dependency. - LibraryMirror get sourceLibrary; + LibraryMirrorContract get sourceLibrary; /// The target library of this dependency. - LibraryMirror? get targetLibrary; + LibraryMirrorContract? get targetLibrary; /// The prefix if this is a prefixed import. Symbol? get prefix; /// The show/hide combinators on this dependency. - List get combinators; + List get combinators; } /// Base combinator mirror interface -abstract class CombinatorMirror implements Mirror { +abstract class CombinatorMirrorContract implements MirrorContract { /// The identifiers in this combinator. List get identifiers; @@ -252,3 +255,42 @@ abstract class CombinatorMirror implements Mirror { /// Whether this is a hide combinator. bool get isHide; } + +/// An [IsolateMirrorContract] reflects an isolate. +abstract class IsolateMirrorContract implements MirrorContract { + /// A unique name used to refer to the isolate in debugging messages. + String get debugName; + + /// Whether this mirror reflects the currently running isolate. + bool get isCurrent; + + /// The root library for the reflected isolate. + LibraryMirrorContract get rootLibrary; +} + +/// A [MirrorSystemContract] is the main interface used to reflect on a set of libraries. +abstract class MirrorSystemContract { + /// All libraries known to the mirror system. + Map get libraries; + + /// Returns the unique library with the specified name. + LibraryMirrorContract findLibrary(Symbol libraryName); + + /// Returns a mirror for the specified class. + ClassMirrorContract reflectClass(Type type); + + /// Returns a mirror for the specified type. + TypeMirrorContract reflectType(Type type); + + /// A mirror on the isolate associated with this mirror system. + IsolateMirrorContract get isolate; + + /// A mirror on the dynamic type. + TypeMirrorContract get dynamicType; + + /// A mirror on the void type. + TypeMirrorContract get voidType; + + /// A mirror on the Never type. + TypeMirrorContract get neverType; +} diff --git a/packages/contracts/lib/src/reflection/reflector_contract.dart b/packages/contracts/lib/src/reflection/reflector_contract.dart index 0cca27d..f2479df 100644 --- a/packages/contracts/lib/src/reflection/reflector_contract.dart +++ b/packages/contracts/lib/src/reflection/reflector_contract.dart @@ -8,7 +8,6 @@ */ import 'base.dart'; -import 'metadata.dart'; export 'base.dart'; export 'metadata.dart'; @@ -16,16 +15,16 @@ export 'metadata.dart'; /// Core reflector contract for type introspection. abstract class ReflectorContract { /// Get a class mirror - ClassMirror? reflectClass(Type type); + ClassMirrorContract? reflectClass(Type type); /// Get a type mirror - TypeMirror reflectType(Type type); + TypeMirrorContract reflectType(Type type); /// Get an instance mirror - InstanceMirror reflect(Object object); + InstanceMirrorContract reflect(Object object); /// Get a library mirror - LibraryMirror reflectLibrary(Uri uri); + LibraryMirrorContract reflectLibrary(Uri uri); /// Create a new instance dynamic createInstance( diff --git a/packages/macroable/lib/src/macroable.dart b/packages/macroable/lib/src/macroable.dart index 9502f57..5fefe48 100644 --- a/packages/macroable/lib/src/macroable.dart +++ b/packages/macroable/lib/src/macroable.dart @@ -1,4 +1,4 @@ -import 'package:platform_reflection/reflection.dart'; +import 'package:platform_reflection/mirrors.dart'; /// Interface for objects that can provide methods to be mixed in abstract class MacroProvider { diff --git a/packages/macroable/test/macroable_test.dart b/packages/macroable/test/macroable_test.dart index 544886e..500f037 100644 --- a/packages/macroable/test/macroable_test.dart +++ b/packages/macroable/test/macroable_test.dart @@ -1,6 +1,6 @@ -import 'package:platform_reflection/reflection.dart'; import 'package:test/test.dart'; import 'package:platform_macroable/platform_macroable.dart'; +import 'package:platform_reflection/mirrors.dart'; @reflectable class TestClass with Macroable {} diff --git a/packages/mirrors/.gitignore b/packages/mirrors/.gitignore new file mode 100644 index 0000000..3cceda5 --- /dev/null +++ b/packages/mirrors/.gitignore @@ -0,0 +1,7 @@ +# https://dart.dev/guides/libraries/private-files +# Created by `dart pub` +.dart_tool/ + +# Avoid committing pubspec.lock for library packages; see +# https://dart.dev/guides/libraries/private-files#pubspeclock. +pubspec.lock diff --git a/packages/mirrors/CHANGELOG.md b/packages/mirrors/CHANGELOG.md new file mode 100644 index 0000000..effe43c --- /dev/null +++ b/packages/mirrors/CHANGELOG.md @@ -0,0 +1,3 @@ +## 1.0.0 + +- Initial version. diff --git a/packages/mirrors/LICENSE.md b/packages/mirrors/LICENSE.md new file mode 100644 index 0000000..0fd0d03 --- /dev/null +++ b/packages/mirrors/LICENSE.md @@ -0,0 +1,10 @@ +The MIT License (MIT) + +The Laravel Framework is Copyright (c) Taylor Otwell +The Fabric Framework is Copyright (c) Vieo, Inc. + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/packages/mirrors/README.md b/packages/mirrors/README.md new file mode 100644 index 0000000..757f4c9 --- /dev/null +++ b/packages/mirrors/README.md @@ -0,0 +1 @@ +

\ No newline at end of file diff --git a/packages/mirrors/analysis_options.yaml b/packages/mirrors/analysis_options.yaml new file mode 100644 index 0000000..dee8927 --- /dev/null +++ b/packages/mirrors/analysis_options.yaml @@ -0,0 +1,30 @@ +# This file configures the static analysis results for your project (errors, +# warnings, and lints). +# +# This enables the 'recommended' set of lints from `package:lints`. +# This set helps identify many issues that may lead to problems when running +# or consuming Dart code, and enforces writing Dart using a single, idiomatic +# style and format. +# +# If you want a smaller set of lints you can change this to specify +# 'package:lints/core.yaml'. These are just the most critical lints +# (the recommended set includes the core lints). +# The core lints are also what is used by pub.dev for scoring packages. + +include: package:lints/recommended.yaml + +# Uncomment the following section to specify additional rules. + +# linter: +# rules: +# - camel_case_types + +# analyzer: +# exclude: +# - path/to/excluded/files/** + +# For more information about the core and recommended set of lints, see +# https://dart.dev/go/core-lints + +# For additional information about configuring this file, see +# https://dart.dev/guides/language/analysis-options diff --git a/packages/mirrors/doc/.gitkeep b/packages/mirrors/doc/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/packages/mirrors/example/.gitkeep b/packages/mirrors/example/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/packages/mirrors/lib/src/.gitkeep b/packages/mirrors/lib/src/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/packages/mirrors/pubspec.yaml b/packages/mirrors/pubspec.yaml new file mode 100644 index 0000000..a39bd81 --- /dev/null +++ b/packages/mirrors/pubspec.yaml @@ -0,0 +1,20 @@ +name: platform_mirrors +description: A lightweight, cross-platform reflection system for Dart +version: 0.0.1 +homepage: https://protevus.com +documentation: https://docs.protevus.com +repository: https://github.com/protevus/platformo + +environment: + sdk: '>=3.0.0 <4.0.0' + +dependencies: + platform_contracts: ^0.1.0 + meta: ^1.9.0 + collection: ^1.17.0 + +dev_dependencies: + test: ^1.24.0 + mockito: ^5.4.0 + build_runner: ^2.4.0 + lints: ^2.1.0 diff --git a/packages/mirrors/test/.gitkeep b/packages/mirrors/test/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/packages/support/lib/src/higher_order_tap_proxy.dart b/packages/support/lib/src/higher_order_tap_proxy.dart index 8ebad09..f76328a 100644 --- a/packages/support/lib/src/higher_order_tap_proxy.dart +++ b/packages/support/lib/src/higher_order_tap_proxy.dart @@ -1,5 +1,5 @@ import 'package:platform_macroable/platform_macroable.dart'; -import 'package:platform_reflection/reflection.dart'; +import 'package:platform_reflection/mirrors.dart'; /// Provides higher-order tap functionality with macro support. /// diff --git a/packages/support/lib/src/onceable.dart b/packages/support/lib/src/onceable.dart index 3197426..71fea83 100644 --- a/packages/support/lib/src/onceable.dart +++ b/packages/support/lib/src/onceable.dart @@ -1,5 +1,5 @@ import 'once.dart'; -import 'package:platform_reflection/reflection.dart'; +import 'package:platform_reflection/mirrors.dart'; /// A class that provides functionality to ensure methods are only executed once. /// diff --git a/packages/support/lib/src/optional.dart b/packages/support/lib/src/optional.dart index 33cabac..2b91915 100644 --- a/packages/support/lib/src/optional.dart +++ b/packages/support/lib/src/optional.dart @@ -1,5 +1,5 @@ import 'package:platform_macroable/platform_macroable.dart'; -import 'package:platform_reflection/reflection.dart'; +import 'package:platform_reflection/mirrors.dart'; /// Provides Laravel-like Optional type functionality with macro support. /// diff --git a/packages/support/lib/src/reflector.dart b/packages/support/lib/src/reflector.dart index 5403725..40602af 100644 --- a/packages/support/lib/src/reflector.dart +++ b/packages/support/lib/src/reflector.dart @@ -1,5 +1,5 @@ -import 'package:platform_reflection/reflection.dart'; -import 'package:platform_reflection/src/core/reflector.dart'; +import 'package:platform_contracts/contracts.dart'; +import 'package:platform_reflection/mirrors.dart'; /// Provides reflection utilities for examining types and methods at runtime. class SupportReflector { @@ -64,7 +64,7 @@ class SupportReflector { } /// Get the class name of the given parameter's type, if possible. - static String? getParameterClassName(ParameterMirror parameter) { + static String? getParameterClassName(ParameterMirrorContract parameter) { final type = parameter.type; if (!type.hasReflectedType) { @@ -75,7 +75,8 @@ class SupportReflector { } /// Get the class names of the given parameter's type, including union types. - static List getParameterClassNames(ParameterMirror parameter) { + static List getParameterClassNames( + ParameterMirrorContract parameter) { final type = parameter.type; final classNames = []; @@ -106,13 +107,14 @@ class SupportReflector { } /// Get the given type's class name. - static String? _getTypeName(ParameterMirror parameter, TypeMirror type) { + static String? _getTypeName( + ParameterMirrorContract parameter, TypeMirrorContract type) { if (!type.hasReflectedType) { return null; } final name = type.reflectedType.toString(); - final declaringClass = parameter.owner as ClassMirror?; + final declaringClass = parameter.owner as ClassMirrorContract?; if (declaringClass != null) { if (name == 'self') { @@ -129,7 +131,7 @@ class SupportReflector { /// Determine if the parameter's type is a subclass of the given type. static bool isParameterSubclassOf( - ParameterMirror parameter, String className) { + ParameterMirrorContract parameter, String className) { final type = parameter.type; if (!type.hasReflectedType) { return false; @@ -145,7 +147,7 @@ class SupportReflector { /// Determine if the parameter's type is a backed enum with a string backing type. static bool isParameterBackedEnumWithStringBackingType( - ParameterMirror parameter) { + ParameterMirrorContract parameter) { final type = parameter.type; if (!type.hasReflectedType) { return false; diff --git a/packages/support/lib/src/traits/forwards_calls.dart b/packages/support/lib/src/traits/forwards_calls.dart index 698d3e4..be7efb2 100644 --- a/packages/support/lib/src/traits/forwards_calls.dart +++ b/packages/support/lib/src/traits/forwards_calls.dart @@ -1,5 +1,5 @@ import 'package:meta/meta.dart'; -import 'package:platform_reflection/reflection.dart'; +import 'package:platform_reflection/mirrors.dart'; /// A mixin that provides method forwarding functionality. /// diff --git a/packages/support/lib/src/traits/reflects_closures.dart b/packages/support/lib/src/traits/reflects_closures.dart index 9b60d6f..521a576 100644 --- a/packages/support/lib/src/traits/reflects_closures.dart +++ b/packages/support/lib/src/traits/reflects_closures.dart @@ -1,4 +1,4 @@ -import 'package:platform_reflection/reflection.dart'; +import 'package:platform_reflection/mirrors.dart'; /// A trait that provides functionality to reflect on closures. mixin ReflectsClosures { diff --git a/packages/support/test/higher_order_tap_proxy_test.dart b/packages/support/test/higher_order_tap_proxy_test.dart index b0d644c..e138a19 100644 --- a/packages/support/test/higher_order_tap_proxy_test.dart +++ b/packages/support/test/higher_order_tap_proxy_test.dart @@ -1,7 +1,5 @@ import 'package:test/test.dart'; import 'package:platform_support/platform_support.dart'; -import 'package:platform_macroable/platform_macroable.dart'; -import 'package:platform_reflection/reflection.dart'; // Test class to use with HigherOrderTapProxy class TestTarget { diff --git a/packages/support/test/onceable_test.dart b/packages/support/test/onceable_test.dart index 31ddc75..6c13de0 100644 --- a/packages/support/test/onceable_test.dart +++ b/packages/support/test/onceable_test.dart @@ -1,5 +1,5 @@ import 'package:test/test.dart'; -import 'package:platform_reflection/reflection.dart'; +import 'package:platform_reflection/mirrors.dart'; import 'package:platform_support/platform_support.dart'; void main() { diff --git a/packages/support/test/support_forwards_calls_test.dart b/packages/support/test/support_forwards_calls_test.dart index b2d497e..e624611 100644 --- a/packages/support/test/support_forwards_calls_test.dart +++ b/packages/support/test/support_forwards_calls_test.dart @@ -1,6 +1,6 @@ import 'package:test/test.dart'; import 'package:platform_support/platform_support.dart'; -import 'package:platform_reflection/reflection.dart'; +import 'package:platform_reflection/mirrors.dart'; @reflectable class TargetClass { diff --git a/packages/support/test/support_optional_test.dart b/packages/support/test/support_optional_test.dart index fff7239..6936452 100644 --- a/packages/support/test/support_optional_test.dart +++ b/packages/support/test/support_optional_test.dart @@ -1,6 +1,6 @@ import 'package:test/test.dart'; import 'package:platform_support/platform_support.dart'; -import 'package:platform_reflection/reflection.dart'; +import 'package:platform_reflection/mirrors.dart'; @reflectable class TestObject { diff --git a/packages/support/test/support_reflector_test.dart b/packages/support/test/support_reflector_test.dart index 0329179..715fde7 100644 --- a/packages/support/test/support_reflector_test.dart +++ b/packages/support/test/support_reflector_test.dart @@ -1,11 +1,7 @@ import 'package:test/test.dart'; +import 'package:platform_contracts/contracts.dart'; +import 'package:platform_reflection/mirrors.dart'; import 'package:platform_support/platform_support.dart'; -import 'package:platform_reflection/reflection.dart'; -import 'package:platform_reflection/src/core/reflector.dart'; -import 'package:platform_reflection/src/mirrors/method_mirror_impl.dart'; -import 'package:platform_reflection/src/mirrors/parameter_mirror_impl.dart'; -import 'package:platform_reflection/src/mirrors/type_mirror_impl.dart'; -import 'package:platform_reflection/src/mirrors/class_mirror_impl.dart'; class TestClass { void publicMethod() {} @@ -28,29 +24,29 @@ enum BackedEnum { } void main() { - late ClassMirrorImpl testClassMirror; + late ClassMirror testClassMirror; setUp(() { // Create type mirrors - final voidType = TypeMirrorImpl( + final voidType = TypeMirror( type: Null, // Using Null as a stand-in for void name: 'void', owner: null, metadata: const [], ); - final stringType = TypeMirrorImpl( + final stringType = TypeMirror( type: String, name: 'String', owner: null, metadata: const [], ); - final invocationType = TypeMirrorImpl( + final invocationType = TypeMirror( type: Invocation, name: 'Invocation', owner: null, metadata: const [], ); - final objectType = TypeMirrorImpl( + final objectType = TypeMirror( type: Object, name: 'Object', owner: null, @@ -58,7 +54,7 @@ void main() { ); // Create class mirrors - testClassMirror = ClassMirrorImpl( + testClassMirror = ClassMirror( type: TestClass, name: 'TestClass', owner: null, @@ -69,34 +65,34 @@ void main() { ); // Create parameter mirrors - final selfParam = ParameterMirrorImpl( + final selfParam = ParameterMirror( name: 'self', type: objectType, // Using Object type for inheritance test owner: testClassMirror, ); - final invocationParam = ParameterMirrorImpl( + final invocationParam = ParameterMirror( name: 'invocation', type: invocationType, owner: testClassMirror, ); // Create method mirrors - final publicMethodMirror = MethodMirrorImpl( + final publicMethodMirror = MethodMirror( name: 'publicMethod', owner: testClassMirror, returnType: voidType, parameters: [selfParam], ); - final privateMethodMirror = MethodMirrorImpl( + final privateMethodMirror = MethodMirror( name: '_privateMethod', owner: testClassMirror, returnType: voidType, parameters: [selfParam], ); - final noSuchMethodMirror = MethodMirrorImpl( + final noSuchMethodMirror = MethodMirror( name: 'noSuchMethod', owner: testClassMirror, returnType: voidType, @@ -208,16 +204,16 @@ void main() { }); test('getParameterClassName returns correct class name', () { - final method = - testClassMirror.declarations[Symbol('publicMethod')] as MethodMirror; + final method = testClassMirror.declarations[Symbol('publicMethod')] + as MethodMirrorContract; final param = method.parameters.first; expect(SupportReflector.getParameterClassName(param), equals('Object')); }); test('getParameterClassNames handles union types', () { - final method = - testClassMirror.declarations[Symbol('publicMethod')] as MethodMirror; + final method = testClassMirror.declarations[Symbol('publicMethod')] + as MethodMirrorContract; final param = method.parameters.first; final classNames = SupportReflector.getParameterClassNames(param); @@ -226,8 +222,8 @@ void main() { }); test('isParameterSubclassOf checks inheritance correctly', () { - final method = - testClassMirror.declarations[Symbol('publicMethod')] as MethodMirror; + final method = testClassMirror.declarations[Symbol('publicMethod')] + as MethodMirrorContract; final param = method.parameters.first; expect(SupportReflector.isParameterSubclassOf(param, 'Object'), isTrue); @@ -236,13 +232,13 @@ void main() { test( 'isParameterBackedEnumWithStringBackingType returns true for backed enums', () { - final type = TypeMirrorImpl( + final type = TypeMirror( type: BackedEnum, name: 'BackedEnum', owner: null, metadata: const [], ); - final param = ParameterMirrorImpl( + final param = ParameterMirror( name: 'param', type: type, owner: testClassMirror, @@ -255,13 +251,13 @@ void main() { test( 'isParameterBackedEnumWithStringBackingType returns false for simple enums', () { - final type = TypeMirrorImpl( + final type = TypeMirror( type: SimpleEnum, name: 'SimpleEnum', owner: null, metadata: const [], ); - final param = ParameterMirrorImpl( + final param = ParameterMirror( name: 'param', type: type, owner: testClassMirror, @@ -274,13 +270,13 @@ void main() { test( 'isParameterBackedEnumWithStringBackingType returns false for non-enums', () { - final type = TypeMirrorImpl( + final type = TypeMirror( type: String, name: 'String', owner: null, metadata: const [], ); - final param = ParameterMirrorImpl( + final param = ParameterMirror( name: 'param', type: type, owner: testClassMirror, diff --git a/packages/support/test/support_tappable_test.dart b/packages/support/test/support_tappable_test.dart index 3d2ff5d..5e925d8 100644 --- a/packages/support/test/support_tappable_test.dart +++ b/packages/support/test/support_tappable_test.dart @@ -1,6 +1,6 @@ import 'package:test/test.dart'; import 'package:platform_support/platform_support.dart'; -import 'package:platform_reflection/reflection.dart'; +import 'package:platform_reflection/mirrors.dart'; @reflectable class TappableTest with Tappable { diff --git a/packages/support/test/traits/reflects_closures_test.dart b/packages/support/test/traits/reflects_closures_test.dart index a3bb03a..9eff361 100644 --- a/packages/support/test/traits/reflects_closures_test.dart +++ b/packages/support/test/traits/reflects_closures_test.dart @@ -1,5 +1,5 @@ import 'package:test/test.dart'; -import 'package:platform_reflection/reflection.dart'; +import 'package:platform_reflection/mirrors.dart'; import 'package:platform_support/src/traits/reflects_closures.dart'; class TestClass with ReflectsClosures {}