OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * Provides some additional convenience methods on top of the basic mirrors | 6 * Provides some additional convenience methods on top of the basic mirrors |
7 */ | 7 */ |
8 library mirrors_helpers; | 8 library mirrors_helpers; |
9 | 9 |
10 // Import and re-export mirrors here to minimize both dependence on mirrors | 10 // Import and re-export mirrors here to minimize both dependence on mirrors |
11 // and the number of times we have to be told that mirrors aren't finished yet. | 11 // and the number of times we have to be told that mirrors aren't finished yet. |
12 import 'dart:mirrors'; | 12 import 'dart:mirrors'; |
13 export 'dart:mirrors'; | 13 export 'dart:mirrors'; |
14 import 'serialization_helpers.dart'; | 14 import 'serialization_helpers.dart'; |
15 | 15 |
16 /** | 16 /** |
17 * Return a list of all the public fields of a class, including inherited | 17 * Return a list of all the public fields of a class, including inherited |
18 * fields. | 18 * fields. |
19 */ | 19 */ |
20 List<VariableMirror> publicFields(ClassMirror mirror) { | 20 List<VariableMirror> publicFields(ClassMirror mirror) { |
21 var mine = mirror.variables.values.filter( | 21 var mine = mirror.variables.values.where( |
22 (x) => !(x.isPrivate || x.isStatic)); | 22 (x) => !(x.isPrivate || x.isStatic)); |
23 var mySuperclass = mirror.superclass; | 23 var mySuperclass = mirror.superclass; |
24 if (mySuperclass != mirror) { | 24 if (mySuperclass != mirror) { |
25 return append(publicFields(mirror.superclass), mine); | 25 return append(publicFields(mirror.superclass), mine); |
26 } else { | 26 } else { |
27 return mine; | 27 return mine; |
28 } | 28 } |
29 } | 29 } |
30 | 30 |
31 /** Return true if the class has a field named [name]. Note that this | 31 /** Return true if the class has a field named [name]. Note that this |
32 * includes private fields, but excludes statics. */ | 32 * includes private fields, but excludes statics. */ |
33 bool hasField(String name, ClassMirror mirror) { | 33 bool hasField(String name, ClassMirror mirror) { |
34 var field = mirror.variables[name]; | 34 var field = mirror.variables[name]; |
35 if (field != null && !field.isStatic) return true; | 35 if (field != null && !field.isStatic) return true; |
36 var superclass = mirror.superclass; | 36 var superclass = mirror.superclass; |
37 if (superclass == mirror) return false; | 37 if (superclass == mirror) return false; |
38 return hasField(name, superclass); | 38 return hasField(name, superclass); |
39 } | 39 } |
40 | 40 |
41 /** | 41 /** |
42 * Return a list of all the getters of a class, including inherited | 42 * Return a list of all the getters of a class, including inherited |
43 * getters. Note that this allows private getters, but excludes statics. | 43 * getters. Note that this allows private getters, but excludes statics. |
44 */ | 44 */ |
45 List<MethodMirror> publicGetters(ClassMirror mirror) { | 45 Iterable<MethodMirror> publicGetters(ClassMirror mirror) { |
46 var mine = mirror.getters.values.filter((x) => !(x.isPrivate || x.isStatic)); | 46 var mine = mirror.getters.values.where((x) => !(x.isPrivate || x.isStatic)); |
47 var mySuperclass = mirror.superclass; | 47 var mySuperclass = mirror.superclass; |
48 if (mySuperclass != mirror) { | 48 if (mySuperclass != mirror) { |
49 return append(publicGetters(mirror.superclass), mine); | 49 return append(publicGetters(mirror.superclass), mine); |
50 } else { | 50 } else { |
51 return mine; | 51 return mine.toList(); |
52 } | 52 } |
53 } | 53 } |
54 | 54 |
55 /** Return true if the class has a getter named [name] */ | 55 /** Return true if the class has a getter named [name] */ |
56 bool hasGetter(String name, ClassMirror mirror) { | 56 bool hasGetter(String name, ClassMirror mirror) { |
57 var getter = mirror.getters[name]; | 57 var getter = mirror.getters[name]; |
58 if (getter != null && !getter.isStatic) return true; | 58 if (getter != null && !getter.isStatic) return true; |
59 var superclass = mirror.superclass; | 59 var superclass = mirror.superclass; |
60 if (superclass == mirror) return false; | 60 if (superclass == mirror) return false; |
61 return hasField(name, superclass); | 61 return hasField(name, superclass); |
62 } | 62 } |
63 | 63 |
64 /** | 64 /** |
65 * Return a list of all the public getters of a class which have corresponding | 65 * Return a list of all the public getters of a class which have corresponding |
66 * setters. | 66 * setters. |
67 */ | 67 */ |
68 List<MethodMirror> publicGettersWithMatchingSetters(ClassMirror mirror) { | 68 List<MethodMirror> publicGettersWithMatchingSetters(ClassMirror mirror) { |
69 var setters = mirror.setters; | 69 var setters = mirror.setters; |
70 return publicGetters(mirror).filter((each) => | 70 return publicGetters(mirror).where((each) => |
71 setters["${each.simpleName}="] != null); | 71 setters["${each.simpleName}="] != null).toList(); |
72 } | 72 } |
73 | 73 |
74 /** | 74 /** |
75 * A particularly bad case of polyfill, because we cannot yet use type names | 75 * A particularly bad case of polyfill, because we cannot yet use type names |
76 * as literals, so we have to be passed an instance and then extract a | 76 * as literals, so we have to be passed an instance and then extract a |
77 * ClassMirror from that. Given a horrible name as an extra reminder to fix it. | 77 * ClassMirror from that. Given a horrible name as an extra reminder to fix it. |
78 */ | 78 */ |
79 ClassMirror turnInstanceIntoSomethingWeCanUse(x) => reflect(x).type; | 79 ClassMirror turnInstanceIntoSomethingWeCanUse(x) => reflect(x).type; |
OLD | NEW |