Compare commits

..

No commits in common. "b1bafb418e4fce4ad1f0689253612620d0bf7ed2" and "7b5290e9d90ec8031c75369cea132d5d4856d35a" have entirely different histories.

View file

@ -1,57 +1,7 @@
import 'dart:core'; import 'dart:core';
import '../metadata.dart'; import '../metadata.dart';
import 'reflector.dart'; import 'reflector.dart';
import '../mirrors.dart'; import '../mirrors/special_types.dart';
import '../mirrors/mirror_system_impl.dart';
import '../exceptions.dart';
/// A wrapper for constructor factory functions that provides scanner-style toString()
class _FactoryWrapper {
final Type type;
final String constructorName;
final MirrorSystemImpl mirrorSystem;
final ClassMirror classMirror;
_FactoryWrapper(this.type, this.constructorName)
: mirrorSystem = MirrorSystemImpl.current(),
classMirror = MirrorSystemImpl.current().reflectClass(type);
dynamic noSuchMethod(Invocation invocation) {
if (invocation.isMethod && invocation.memberName == #call) {
List<dynamic> positionalArgs;
Map<Symbol, dynamic> namedArgs;
// Handle scanner-style call: (List args, [Map namedArgs])
if (invocation.positionalArguments.length <= 2 &&
invocation.positionalArguments.first is List) {
positionalArgs = invocation.positionalArguments[0] as List;
namedArgs = invocation.positionalArguments.length > 1
? invocation.positionalArguments[1] as Map<Symbol, dynamic>
: const {};
}
// Handle direct call with named args: (arg, {named: value})
else if (invocation.namedArguments.isNotEmpty) {
positionalArgs = invocation.positionalArguments;
namedArgs = invocation.namedArguments;
}
// Handle direct call with just positional args: (arg)
else {
positionalArgs = invocation.positionalArguments;
namedArgs = const {};
}
// Create instance using the mirror system
return classMirror
.newInstance(Symbol(constructorName), positionalArgs, namedArgs)
.reflectee;
}
return super.noSuchMethod(invocation);
}
@override
String toString() =>
'Closure: (List<dynamic>, [Map<Symbol, dynamic>?]) => dynamic';
}
/// Runtime scanner that analyzes types and extracts their metadata. /// Runtime scanner that analyzes types and extracts their metadata.
class Scanner { class Scanner {
@ -60,13 +10,13 @@ class Scanner {
/// Scans a type and extracts its metadata. /// Scans a type and extracts its metadata.
static void scanType(Type type) { static void scanType(Type type) {
// First register the type with Reflector // Get type name and analyze it
Reflector.register(type); final typeName = type.toString();
// Get mirror system and analyze type
final mirrorSystem = MirrorSystemImpl.current();
final typeInfo = TypeAnalyzer.analyze(type); final typeInfo = TypeAnalyzer.analyze(type);
// Register type for reflection
Reflector.registerType(type);
// Register properties // Register properties
for (var property in typeInfo.properties) { for (var property in typeInfo.properties) {
Reflector.registerPropertyMetadata( Reflector.registerPropertyMetadata(
@ -126,11 +76,23 @@ class Scanner {
/// Creates a constructor factory function for a given type and constructor. /// Creates a constructor factory function for a given type and constructor.
static Function? _createConstructorFactory( static Function? _createConstructorFactory(
Type type, ConstructorInfo constructor) { Type type, ConstructorInfo constructor) {
final wrapper = _FactoryWrapper(type, constructor.name); final typeObj = type as dynamic;
return (List<dynamic> args, [Map<Symbol, dynamic>? namedArgs]) {
return wrapper.noSuchMethod( // Create a factory function that takes a list of positional args and optional named args
Invocation.method(#call, args, namedArgs ?? {}), return (List positionalArgs, [Map<Symbol, dynamic>? namedArgs]) {
); try {
if (constructor.name.isEmpty) {
// For default constructor
return Function.apply(typeObj, positionalArgs, namedArgs);
} else {
// For named constructor
final namedConstructor = typeObj[constructor.name];
return Function.apply(namedConstructor, positionalArgs, namedArgs);
}
} catch (e) {
print('Warning: Failed to create instance: $e');
return null;
}
}; };
} }
} }