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

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

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