| 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 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 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(Symbol name, ClassMirror mirror) { |
| 34 if (name == null) return false; |
| 34 var field = mirror.variables[name]; | 35 var field = mirror.variables[name]; |
| 35 if (field != null && !field.isStatic) return true; | 36 if (field != null && !field.isStatic) return true; |
| 36 var superclass = mirror.superclass; | 37 var superclass = mirror.superclass; |
| 37 if (superclass == mirror) return false; | 38 if (superclass == mirror) return false; |
| 38 return hasField(name, superclass); | 39 return hasField(name, superclass); |
| 39 } | 40 } |
| 40 | 41 |
| 41 /** | 42 /** |
| 42 * Return a list of all the getters of a class, including inherited | 43 * Return a list of all the getters of a class, including inherited |
| 43 * getters. Note that this allows private getters, but excludes statics. | 44 * getters. Note that this allows private getters, but excludes statics. |
| 44 */ | 45 */ |
| 45 Iterable<MethodMirror> publicGetters(ClassMirror mirror) { | 46 Iterable<MethodMirror> publicGetters(ClassMirror mirror) { |
| 46 var mine = mirror.getters.values.where((x) => !(x.isPrivate || x.isStatic)); | 47 var mine = mirror.getters.values.where((x) => !(x.isPrivate || x.isStatic)); |
| 47 var mySuperclass = mirror.superclass; | 48 var mySuperclass = mirror.superclass; |
| 48 if (mySuperclass != mirror) { | 49 if (mySuperclass != mirror) { |
| 49 return append(publicGetters(mirror.superclass), mine); | 50 return append(publicGetters(mirror.superclass), mine); |
| 50 } else { | 51 } else { |
| 51 return mine.toList(); | 52 return mine.toList(); |
| 52 } | 53 } |
| 53 } | 54 } |
| 54 | 55 |
| 55 /** Return true if the class has a getter named [name] */ | 56 /** Return true if the class has a getter named [name] */ |
| 56 bool hasGetter(String name, ClassMirror mirror) { | 57 bool hasGetter(Symbol name, ClassMirror mirror) { |
| 58 if (name == null) return false; |
| 57 var getter = mirror.getters[name]; | 59 var getter = mirror.getters[name]; |
| 58 if (getter != null && !getter.isStatic) return true; | 60 if (getter != null && !getter.isStatic) return true; |
| 59 var superclass = mirror.superclass; | 61 var superclass = mirror.superclass; |
| 60 if (superclass == mirror) return false; | 62 if (superclass == mirror) return false; |
| 61 return hasField(name, superclass); | 63 return hasField(name, superclass); |
| 62 } | 64 } |
| 63 | 65 |
| 64 /** | 66 /** |
| 65 * Return a list of all the public getters of a class which have corresponding | 67 * Return a list of all the public getters of a class which have corresponding |
| 66 * setters. | 68 * setters. |
| 67 */ | 69 */ |
| 68 Iterable<MethodMirror> publicGettersWithMatchingSetters(ClassMirror mirror) { | 70 Iterable<MethodMirror> publicGettersWithMatchingSetters(ClassMirror mirror) { |
| 69 var setters = mirror.setters; | 71 var setters = mirror.setters; |
| 70 return publicGetters(mirror).where((each) => | 72 return publicGetters(mirror).where((each) => |
| 71 setters["${each.simpleName}="] != null); | 73 setters["${each.simpleName}="] != null); |
| 72 } | 74 } |
| 73 | 75 |
| 74 /** | 76 /** |
| 75 * A particularly bad case of polyfill, because we cannot yet use type names | 77 * 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 | 78 * 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. | 79 * ClassMirror from that. Given a horrible name as an extra reminder to fix it. |
| 78 */ | 80 */ |
| 79 ClassMirror turnInstanceIntoSomethingWeCanUse(x) => reflect(x).type; | 81 ClassMirror turnInstanceIntoSomethingWeCanUse(x) => reflect(x).type; |
| OLD | NEW |