| 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 10 matching lines...) Expand all Loading... |
| 21 var mine = mirror.variables.values.filter( | 21 var mine = mirror.variables.values.filter( |
| 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 |
| 32 * includes private fields, but excludes statics. */ |
| 33 bool hasField(String name, ClassMirror mirror) { |
| 34 var field = mirror.variables[name]; |
| 35 if (field != null && !field.isStatic) return true; |
| 36 var superclass = mirror.superclass; |
| 37 if (superclass == mirror) return false; |
| 38 return hasField(name, superclass); |
| 39 } |
| 40 |
| 31 /** | 41 /** |
| 32 * Return a list of all the public getters of a class, including inherited | 42 * Return a list of all the getters of a class, including inherited |
| 33 * getters. | 43 * getters. Note that this allows private getters, but excludes statics. |
| 34 */ | 44 */ |
| 35 List<MethodMirror> publicGetters(ClassMirror mirror) { | 45 List<MethodMirror> publicGetters(ClassMirror mirror) { |
| 36 var mine = mirror.getters.values.filter((x) => !(x.isPrivate || x.isStatic)); | 46 var mine = mirror.getters.values.filter((x) => !(x.isPrivate || x.isStatic)); |
| 37 var mySuperclass = mirror.superclass; | 47 var mySuperclass = mirror.superclass; |
| 38 if (mySuperclass != mirror) { | 48 if (mySuperclass != mirror) { |
| 39 return append(publicGetters(mirror.superclass), mine); | 49 return append(publicGetters(mirror.superclass), mine); |
| 40 } else { | 50 } else { |
| 41 return mine; | 51 return mine; |
| 42 } | 52 } |
| 43 } | 53 } |
| 44 | 54 |
| 55 /** Return true if the class has a getter named [name] */ |
| 56 bool hasGetter(String name, ClassMirror mirror) { |
| 57 var getter = mirror.getters[name]; |
| 58 if (getter != null && !getter.isStatic) return true; |
| 59 var superclass = mirror.superclass; |
| 60 if (superclass == mirror) return false; |
| 61 return hasField(name, superclass); |
| 62 } |
| 63 |
| 45 /** | 64 /** |
| 46 * 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 |
| 47 * setters. | 66 * setters. |
| 48 */ | 67 */ |
| 49 List<MethodMirror> publicGettersWithMatchingSetters(ClassMirror mirror) { | 68 List<MethodMirror> publicGettersWithMatchingSetters(ClassMirror mirror) { |
| 50 var setters = mirror.setters; | 69 var setters = mirror.setters; |
| 51 return publicGetters(mirror).filter((each) => | 70 return publicGetters(mirror).filter((each) => |
| 52 setters["${each.simpleName}="] != null); | 71 setters["${each.simpleName}="] != null); |
| 53 } | 72 } |
| 54 | 73 |
| 55 /** | 74 /** |
| 56 * 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 |
| 57 * 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 |
| 58 * 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. |
| 59 */ | 78 */ |
| 60 ClassMirror turnInstanceIntoSomethingWeCanUse(x) => reflect(x).type; | 79 ClassMirror turnInstanceIntoSomethingWeCanUse(x) => reflect(x).type; |
| OLD | NEW |