| OLD | NEW |
| 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 'mirrors.dart'; |
| 5 | 5 |
| 6 /** | 6 /** |
| 7 * Dynamic implementation of [Injector] that uses mirrors. | 7 * Dynamic implementation of [Injector] that uses mirrors. |
| 8 */ | 8 */ |
| 9 class DynamicInjector extends Injector { | 9 class DynamicInjector extends Injector { |
| 10 | 10 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 Object newInstanceOf(Type type, ObjectFactory getInstanceByType, | 23 Object newInstanceOf(Type type, ObjectFactory getInstanceByType, |
| 24 Injector requestor, error) { | 24 Injector requestor, error) { |
| 25 var classMirror = reflectType(type); | 25 var classMirror = reflectType(type); |
| 26 if (classMirror is TypedefMirror) { | 26 if (classMirror is TypedefMirror) { |
| 27 throw new NoProviderError(error('No implementation provided ' | 27 throw new NoProviderError(error('No implementation provided ' |
| 28 'for ${getSymbolName(classMirror.qualifiedName)} typedef!')); | 28 'for ${getSymbolName(classMirror.qualifiedName)} typedef!')); |
| 29 } | 29 } |
| 30 | 30 |
| 31 MethodMirror ctor = classMirror.declarations[classMirror.simpleName]; | 31 MethodMirror ctor = classMirror.declarations[classMirror.simpleName]; |
| 32 | 32 |
| 33 if (ctor == null) { | |
| 34 throw new NoProviderError('Unable to find default constructor for $type. ' | |
| 35 'Make sure class has a default constructor.' + | |
| 36 (1.0 is int ? | |
| 37 ' Make sure you have correctly configured @MirrorsUsed.' : '')); | |
| 38 } | |
| 39 | |
| 40 resolveArgument(int pos) { | 33 resolveArgument(int pos) { |
| 41 ParameterMirror p = ctor.parameters[pos]; | 34 ParameterMirror p = ctor.parameters[pos]; |
| 42 if (MirrorSystem.getName(p.type.qualifiedName) == 'dynamic') { | |
| 43 var name = MirrorSystem.getName(p.simpleName); | |
| 44 throw new NoProviderError(error("The '$name' parameter must be typed")); | |
| 45 } | |
| 46 if (p.type is TypedefMirror) { | |
| 47 throw new NoProviderError( | |
| 48 error('Cannot create new instance of a typedef ${p.type}')); | |
| 49 } | |
| 50 return getInstanceByType(getReflectedTypeWorkaround(p.type), requestor); | 35 return getInstanceByType(getReflectedTypeWorkaround(p.type), requestor); |
| 51 } | 36 } |
| 52 | 37 |
| 53 var args = new List.generate(ctor.parameters.length, resolveArgument, | 38 var args = new List.generate(ctor.parameters.length, resolveArgument, |
| 54 growable: false); | 39 growable: false); |
| 55 return classMirror.newInstance(ctor.constructorName, args).reflectee; | 40 return classMirror.newInstance(ctor.constructorName, args).reflectee; |
| 56 } | 41 } |
| 57 | 42 |
| 58 /** | 43 /** |
| 59 * Invoke given function and inject all its arguments. | 44 * Invoke given function and inject all its arguments. |
| (...skipping 10 matching lines...) Expand all Loading... |
| 70 } on NoProviderError catch (e) { | 55 } on NoProviderError catch (e) { |
| 71 throw new NoProviderError(e.message); | 56 throw new NoProviderError(e.message); |
| 72 } finally { | 57 } finally { |
| 73 position++; | 58 position++; |
| 74 } | 59 } |
| 75 }).toList(); | 60 }).toList(); |
| 76 | 61 |
| 77 return cm.apply(args).reflectee; | 62 return cm.apply(args).reflectee; |
| 78 } | 63 } |
| 79 } | 64 } |
| OLD | NEW |