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

Side by Side Diff: third_party/pkg/di/lib/dynamic_injector.dart

Issue 257423008: Update all Angular libs (run update_all.sh). (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 8 months 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 unified diff | Download patch | Annotate | Revision Log
« 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 »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 library di.dynamic_injector; 1 library di.dynamic_injector;
2 2
3 import 'di.dart'; 3 import 'di.dart';
4 import 'mirrors.dart'; 4 import 'src/mirrors.dart';
5 import 'src/base_injector.dart';
6 import 'src/error_helper.dart';
7 import 'src/provider.dart';
8
9 export 'di.dart';
5 10
6 /** 11 /**
7 * Dynamic implementation of [Injector] that uses mirrors. 12 * Dynamic implementation of [Injector] that uses mirrors.
8 */ 13 */
9 class DynamicInjector extends Injector { 14 class DynamicInjector extends BaseInjector {
10 15
11 DynamicInjector({List<Module> modules, String name, 16 DynamicInjector({List<Module> modules, String name,
12 bool allowImplicitInjection: false}) 17 bool allowImplicitInjection: false})
13 : super(modules: modules, name: name, 18 : super(modules: modules, name: name,
14 allowImplicitInjection: allowImplicitInjection); 19 allowImplicitInjection: allowImplicitInjection);
15 20
16 DynamicInjector._fromParent(List<Module> modules, Injector parent, {name}) 21 DynamicInjector._fromParent(List<Module> modules, Injector parent, {name})
17 : super.fromParent(modules, parent, name: name); 22 : super.fromParent(modules, parent, name: name);
18 23
19 newFromParent(List<Module> modules, String name) { 24 newFromParent(List<Module> modules, String name) =>
20 return new DynamicInjector._fromParent(modules, this, name: name); 25 new DynamicInjector._fromParent(modules, this, name: name);
21 }
22 26
23 Object newInstanceOf(Type type, ObjectFactory getInstanceByType, 27 Object newInstanceOf(Type type, ObjectFactory objFactory, Injector requestor,
24 Injector requestor, error) { 28 resolving) {
25 var classMirror = reflectType(type); 29 var classMirror = reflectType(type);
26 if (classMirror is TypedefMirror) { 30 if (classMirror is TypedefMirror) {
27 throw new NoProviderError(error('No implementation provided ' 31 throw new NoProviderError(error(resolving, 'No implementation provided '
28 'for ${getSymbolName(classMirror.qualifiedName)} typedef!')); 32 'for ${getSymbolName(classMirror.qualifiedName)} typedef!'));
29 } 33 }
30 34
31 MethodMirror ctor = classMirror.declarations[classMirror.simpleName]; 35 MethodMirror ctor = classMirror.declarations[classMirror.simpleName];
32 36
33 if (ctor == null) { 37 if (ctor == null) {
34 throw new NoProviderError('Unable to find default constructor for $type. ' 38 throw new NoProviderError('Unable to find default constructor for $type. '
35 'Make sure class has a default constructor.' + 39 'Make sure class has a default constructor.' + (1.0 is int ?
36 (1.0 is int ? 40 'Make sure you have correctly configured @MirrorsUsed.' : ''));
37 ' Make sure you have correctly configured @MirrorsUsed.' : ''));
38 } 41 }
39 42
40 resolveArgument(int pos) { 43 resolveArgument(int pos) {
41 ParameterMirror p = ctor.parameters[pos]; 44 ParameterMirror p = ctor.parameters[pos];
42 if (MirrorSystem.getName(p.type.qualifiedName) == 'dynamic') { 45 if (p.type.qualifiedName == #dynamic) {
43 var name = MirrorSystem.getName(p.simpleName); 46 var name = MirrorSystem.getName(p.simpleName);
44 throw new NoProviderError(error("The '$name' parameter must be typed")); 47 throw new NoProviderError(
48 error(resolving, "The '$name' parameter must be typed"));
45 } 49 }
46 if (p.type is TypedefMirror) { 50 if (p.type is TypedefMirror) {
47 throw new NoProviderError( 51 throw new NoProviderError(
48 error('Cannot create new instance of a typedef ${p.type}')); 52 error(resolving,
53 'Cannot create new instance of a typedef ${p.type}'));
49 } 54 }
50 return getInstanceByType(getReflectedTypeWorkaround(p.type), requestor); 55 if (p.metadata.isNotEmpty) {
56 assert(p.metadata.length == 1);
57 var type = p.metadata.first.type.reflectedType;
58 return objFactory.getInstanceByKey(
59 new Key((p.type as ClassMirror).reflectedType, type),
60 requestor, resolving);
61 } else {
62 return objFactory.getInstanceByKey(
63 new Key((p.type as ClassMirror).reflectedType),
64 requestor, resolving);
65 }
51 } 66 }
52 67
53 var args = new List.generate(ctor.parameters.length, resolveArgument, 68 var args = new List.generate(ctor.parameters.length, resolveArgument,
54 growable: false); 69 growable: false);
55 return classMirror.newInstance(ctor.constructorName, args).reflectee; 70 return classMirror.newInstance(ctor.constructorName, args).reflectee;
56 } 71 }
57 72
58 /** 73 /**
59 * Invoke given function and inject all its arguments. 74 * Invoke given function and inject all its arguments.
60 * 75 *
61 * Returns whatever the function returns. 76 * Returns whatever the function returns.
62 */ 77 */
63 dynamic invoke(Function fn) { 78 dynamic invoke(Function fn) {
64 ClosureMirror cm = reflect(fn); 79 ClosureMirror cm = reflect(fn);
65 MethodMirror mm = cm.function; 80 MethodMirror mm = cm.function;
66 int position = 0; 81 int position = 0;
67 List args = mm.parameters.map((ParameterMirror parameter) { 82 List args = mm.parameters.map((ParameterMirror parameter) {
68 try { 83 try {
69 return get(getReflectedTypeWorkaround(parameter.type)); 84 if (parameter.metadata.isNotEmpty) {
85 var annotation = parameter.metadata[0].type.reflectedType;
86 return get((parameter.type as ClassMirror).reflectedType, annotation);
87 } else {
88 return get((parameter.type as ClassMirror).reflectedType);
89 }
70 } on NoProviderError catch (e) { 90 } on NoProviderError catch (e) {
71 throw new NoProviderError(e.message); 91 throw new NoProviderError(e.message);
72 } finally { 92 } finally {
73 position++; 93 position++;
74 } 94 }
75 }).toList(); 95 }).toList();
76 96
77 return cm.apply(args).reflectee; 97 return cm.apply(args).reflectee;
78 } 98 }
79 } 99 }
OLDNEW
« 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