Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(95)

Unified Diff: third_party/pkg/di/lib/dynamic_injector.dart

Issue 107003007: Adding Angular's DI package & Dart unittests which test it. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « third_party/pkg/di/lib/di.dart ('k') | third_party/pkg/di/lib/errors.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: third_party/pkg/di/lib/dynamic_injector.dart
diff --git a/third_party/pkg/di/lib/dynamic_injector.dart b/third_party/pkg/di/lib/dynamic_injector.dart
new file mode 100644
index 0000000000000000000000000000000000000000..53ec52eec24922f63e40abf464ef30920fbbc416
--- /dev/null
+++ b/third_party/pkg/di/lib/dynamic_injector.dart
@@ -0,0 +1,64 @@
+library di.dynamic_injector;
+
+import 'di.dart';
+import 'mirrors.dart';
+
+/**
+ * Dynamic implementation of [Injector] that uses mirrors.
+ */
+class DynamicInjector extends Injector {
+
+ DynamicInjector({List<Module> modules, String name,
+ bool allowImplicitInjection: false})
+ : super(modules: modules, name: name,
+ allowImplicitInjection: allowImplicitInjection);
+
+ DynamicInjector._fromParent(List<Module> modules, Injector parent, {name})
+ : super.fromParent(modules, parent, name: name);
+
+ newFromParent(List<Module> modules, String name) {
+ return new DynamicInjector._fromParent(modules, this, name: name);
+ }
+
+ Object newInstanceOf(Type type, ObjectFactory getInstanceByType,
+ Injector requestor, error) {
+ var classMirror = reflectType(type);
+ if (classMirror is TypedefMirror) {
+ throw new NoProviderError(error('No implementation provided '
+ 'for ${getSymbolName(classMirror.qualifiedName)} typedef!'));
+ }
+
+ MethodMirror ctor = classMirror.declarations[classMirror.simpleName];
+
+ resolveArgument(int pos) {
+ ParameterMirror p = ctor.parameters[pos];
+ return getInstanceByType(getReflectedTypeWorkaround(p.type), requestor);
+ }
+
+ var args = new List.generate(ctor.parameters.length, resolveArgument,
+ growable: false);
+ return classMirror.newInstance(ctor.constructorName, args).reflectee;
+ }
+
+ /**
+ * Invoke given function and inject all its arguments.
+ *
+ * Returns whatever the function returns.
+ */
+ dynamic invoke(Function fn) {
+ ClosureMirror cm = reflect(fn);
+ MethodMirror mm = cm.function;
+ int position = 0;
+ List args = mm.parameters.map((ParameterMirror parameter) {
+ try {
+ return get(getReflectedTypeWorkaround(parameter.type));
+ } on NoProviderError catch (e) {
+ throw new NoProviderError(e.message);
+ } finally {
+ position++;
+ }
+ }).toList();
+
+ return cm.apply(args).reflectee;
+ }
+}
« no previous file with comments | « third_party/pkg/di/lib/di.dart ('k') | third_party/pkg/di/lib/errors.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698