diff --git a/angel_container/lib/angel_container.dart b/angel_container/lib/angel_container.dart index 24a3c225..804cff0e 100644 --- a/angel_container/lib/angel_container.dart +++ b/angel_container/lib/angel_container.dart @@ -2,6 +2,7 @@ library angel_container; export 'src/container.dart'; export 'src/empty/empty.dart'; +export 'src/static/static.dart'; export 'src/exception.dart'; export 'src/reflector.dart'; diff --git a/angel_container/lib/src/static/static.dart b/angel_container/lib/src/static/static.dart new file mode 100644 index 00000000..8d66fc4e --- /dev/null +++ b/angel_container/lib/src/static/static.dart @@ -0,0 +1,62 @@ +import 'package:angel_container/angel_container.dart'; +import 'package:meta/meta.dart'; + +/// A [Reflector] implementation that performs simple [Map] lookups. +/// +/// `package:angel_container_generator` uses this to create reflectors from analysis metadata. +class StaticReflector implements Reflector { + final Map names; + final Map types; + final Map functions; + final Map instances; + + const StaticReflector( + {@required this.names, + @required this.types, + @required this.functions, + @required this.instances}); + + @override + String getName(Symbol symbol) { + if (!names.containsKey(symbol)) { + throw new ArgumentError( + 'The value of $symbol is unknown - it was not generated.'); + } + + return names[symbol]; + } + + @override + ReflectedClass reflectClass(Type clazz) => + reflectType(clazz) as ReflectedClass; + + @override + ReflectedFunction reflectFunction(Function function) { + if (!functions.containsKey(function)) { + throw new ArgumentError( + 'There is no reflection information available about $function.'); + } + + return functions[function]; + } + + @override + ReflectedInstance reflectInstance(Object object) { + if (!instances.containsKey(object)) { + throw new ArgumentError( + 'There is no reflection information available about $object.'); + } + + return instances[object]; + } + + @override + ReflectedType reflectType(Type type) { + if (!types.containsKey(type)) { + throw new ArgumentError( + 'There is no reflection information available about $type.'); + } + + return types[type]; + } +} diff --git a/angel_container/pubspec.yaml b/angel_container/pubspec.yaml index 58ee085b..20ec9cca 100644 --- a/angel_container/pubspec.yaml +++ b/angel_container/pubspec.yaml @@ -1,5 +1,5 @@ name: angel_container -version: 1.0.0-alpha.10 +version: 1.0.0-alpha.11 author: Tobe O description: "A better IoC container and dependency injector for Angel, ultimately allowing Angel to be used without dart:mirrors." homepage: https://github.com/angel-dart/container.git @@ -7,6 +7,7 @@ environment: sdk: ">=1.8.0 <3.0.0" dependencies: collection: ^1.0.0 + meta: ^1.0.0 quiver: ^2.0.0 dev_dependencies: test: \ No newline at end of file diff --git a/angel_container_generator/lib/src/library_generator.dart b/angel_container_generator/lib/src/library_generator.dart index 54ad5ee3..43041aef 100644 --- a/angel_container_generator/lib/src/library_generator.dart +++ b/angel_container_generator/lib/src/library_generator.dart @@ -42,7 +42,9 @@ class ReflectorLibraryGenerator { } // implements Reflector - clazz.implements.add(refer('Reflector')); + clazz + ..extend = refer('StaticReflector') + ..implements.add(refer('Reflector')); // Add a const constructor clazz.constructors.add(new Constructor((b) {