OLD | NEW |
| (Empty) |
1 part of angular.core; | |
2 | |
3 abstract class AnnotationMap<K> { | |
4 final Map<K, Type> _map = {}; | |
5 | |
6 AnnotationMap(Injector injector, MetadataExtractor extractMetadata) { | |
7 injector.types.forEach((type) { | |
8 var meta = extractMetadata(type) | |
9 .where((annotation) => annotation is K) | |
10 .forEach((annotation) { | |
11 _map[annotation] = type; | |
12 }); | |
13 }); | |
14 } | |
15 | |
16 Type operator[](K annotation) { | |
17 var value = _map[annotation]; | |
18 if (value == null) throw 'No $annotation found!'; | |
19 return value; | |
20 } | |
21 | |
22 forEach(fn(K, Type)) => _map.forEach(fn); | |
23 | |
24 List<K> annotationsFor(Type type) { | |
25 var res = <K>[]; | |
26 forEach((ann, annType) { | |
27 if (annType == type) res.add(ann); | |
28 }); | |
29 return res; | |
30 } | |
31 } | |
32 | |
33 abstract class AnnotationsMap<K> { | |
34 final Map<K, List<Type>> map = {}; | |
35 | |
36 AnnotationsMap(Injector injector, MetadataExtractor extractMetadata) { | |
37 injector.types.forEach((type) { | |
38 var meta = extractMetadata(type) | |
39 .where((annotation) => annotation is K) | |
40 .forEach((annotation) { | |
41 map.putIfAbsent(annotation, () => []).add(type); | |
42 }); | |
43 }); | |
44 } | |
45 | |
46 List operator[](K annotation) { | |
47 var value = map[annotation]; | |
48 if (value == null) throw 'No $annotation found!'; | |
49 return value; | |
50 } | |
51 | |
52 forEach(fn(K, Type)) { | |
53 map.forEach((annotation, types) { | |
54 types.forEach((type) { | |
55 fn(annotation, type); | |
56 }); | |
57 }); | |
58 } | |
59 | |
60 List<K> annotationsFor(Type type) { | |
61 var res = <K>[]; | |
62 forEach((ann, annType) { | |
63 if (annType == type) res.add(ann); | |
64 }); | |
65 return res; | |
66 } | |
67 } | |
68 | |
69 | |
70 @NgInjectableService() | |
71 class MetadataExtractor { | |
72 Iterable call(Type type) { | |
73 if (reflectType(type) is TypedefMirror) return []; | |
74 var metadata = reflectClass(type).metadata; | |
75 if (metadata == null) return []; | |
76 return metadata.map((InstanceMirror im) => im.reflectee); | |
77 } | |
78 } | |
OLD | NEW |