| 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 |
| 33 resolveArgument(int pos) { | 40 resolveArgument(int pos) { |
| 34 ParameterMirror p = ctor.parameters[pos]; | 41 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 } |
| 35 return getInstanceByType(getReflectedTypeWorkaround(p.type), requestor); | 50 return getInstanceByType(getReflectedTypeWorkaround(p.type), requestor); |
| 36 } | 51 } |
| 37 | 52 |
| 38 var args = new List.generate(ctor.parameters.length, resolveArgument, | 53 var args = new List.generate(ctor.parameters.length, resolveArgument, |
| 39 growable: false); | 54 growable: false); |
| 40 return classMirror.newInstance(ctor.constructorName, args).reflectee; | 55 return classMirror.newInstance(ctor.constructorName, args).reflectee; |
| 41 } | 56 } |
| 42 | 57 |
| 43 /** | 58 /** |
| 44 * Invoke given function and inject all its arguments. | 59 * Invoke given function and inject all its arguments. |
| (...skipping 10 matching lines...) Expand all Loading... |
| 55 } on NoProviderError catch (e) { | 70 } on NoProviderError catch (e) { |
| 56 throw new NoProviderError(e.message); | 71 throw new NoProviderError(e.message); |
| 57 } finally { | 72 } finally { |
| 58 position++; | 73 position++; |
| 59 } | 74 } |
| 60 }).toList(); | 75 }).toList(); |
| 61 | 76 |
| 62 return cm.apply(args).reflectee; | 77 return cm.apply(args).reflectee; |
| 63 } | 78 } |
| 64 } | 79 } |
| OLD | NEW |