add static reflector

This commit is contained in:
Tobe O 2018-11-08 11:39:20 -05:00
parent 5ca37494cf
commit 34224504e2
4 changed files with 68 additions and 2 deletions

View file

@ -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';

View file

@ -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<Symbol, String> names;
final Map<Type, ReflectedType> types;
final Map<Function, ReflectedFunction> functions;
final Map<Object, ReflectedInstance> 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];
}
}

View file

@ -1,5 +1,5 @@
name: angel_container
version: 1.0.0-alpha.10
version: 1.0.0-alpha.11
author: Tobe O <thosakwe@gmail.com>
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:

View file

@ -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) {