| 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 // TODO(alanknight): Remove this method. It is working around a bug |
| 17 // in the Dart VM which incorrectly returns Object as the superclass |
| 18 // of Object. |
| 19 _getSuperclass(ClassMirror mirror) { |
| 20 var superclass = mirror.superclass; |
| 21 return (superclass == mirror) ? null : superclass; |
| 22 } |
| 23 |
| 16 /** | 24 /** |
| 17 * Return a list of all the public fields of a class, including inherited | 25 * Return a list of all the public fields of a class, including inherited |
| 18 * fields. | 26 * fields. |
| 19 */ | 27 */ |
| 20 Iterable<VariableMirror> publicFields(ClassMirror mirror) { | 28 Iterable<VariableMirror> publicFields(ClassMirror mirror) { |
| 21 var mine = mirror.variables.values.where( | 29 var mine = mirror.variables.values.where( |
| 22 (x) => !(x.isPrivate || x.isStatic)); | 30 (x) => !(x.isPrivate || x.isStatic)); |
| 23 var mySuperclass = mirror.superclass; | 31 var mySuperclass = _getSuperclass(mirror); |
| 24 if (mySuperclass != mirror) { | 32 if (mySuperclass != null) { |
| 25 return append(publicFields(mirror.superclass), mine); | 33 return append(publicFields(mySuperclass), mine); |
| 26 } else { | 34 } else { |
| 27 return mine; | 35 return mine; |
| 28 } | 36 } |
| 29 } | 37 } |
| 30 | 38 |
| 31 /** Return true if the class has a field named [name]. Note that this | 39 /** Return true if the class has a field named [name]. Note that this |
| 32 * includes private fields, but excludes statics. */ | 40 * includes private fields, but excludes statics. */ |
| 33 bool hasField(Symbol name, ClassMirror mirror) { | 41 bool hasField(Symbol name, ClassMirror mirror) { |
| 34 if (name == null) return false; | 42 if (name == null) return false; |
| 35 var field = mirror.variables[name]; | 43 var field = mirror.variables[name]; |
| 36 if (field != null && !field.isStatic) return true; | 44 if (field != null && !field.isStatic) return true; |
| 37 var superclass = mirror.superclass; | 45 var superclass = _getSuperclass(mirror); |
| 38 if (superclass == mirror) return false; | 46 if (superclass == null) return false; |
| 39 return hasField(name, superclass); | 47 return hasField(name, superclass); |
| 40 } | 48 } |
| 41 | 49 |
| 42 /** | 50 /** |
| 43 * Return a list of all the getters of a class, including inherited | 51 * Return a list of all the getters of a class, including inherited |
| 44 * getters. Note that this allows private getters, but excludes statics. | 52 * getters. Note that this allows private getters, but excludes statics. |
| 45 */ | 53 */ |
| 46 Iterable<MethodMirror> publicGetters(ClassMirror mirror) { | 54 Iterable<MethodMirror> publicGetters(ClassMirror mirror) { |
| 47 var mine = mirror.getters.values.where((x) => !(x.isPrivate || x.isStatic)); | 55 var mine = mirror.getters.values.where((x) => !(x.isPrivate || x.isStatic)); |
| 48 var mySuperclass = mirror.superclass; | 56 var mySuperclass = _getSuperclass(mirror); |
| 49 if (mySuperclass != mirror) { | 57 if (mySuperclass != null) { |
| 50 return append(publicGetters(mirror.superclass), mine); | 58 return append(publicGetters(mySuperclass), mine); |
| 51 } else { | 59 } else { |
| 52 return mine.toList(); | 60 return mine.toList(); |
| 53 } | 61 } |
| 54 } | 62 } |
| 55 | 63 |
| 56 /** Return true if the class has a getter named [name] */ | 64 /** Return true if the class has a getter named [name] */ |
| 57 bool hasGetter(Symbol name, ClassMirror mirror) { | 65 bool hasGetter(Symbol name, ClassMirror mirror) { |
| 58 if (name == null) return false; | 66 if (name == null) return false; |
| 59 var getter = mirror.getters[name]; | 67 var getter = mirror.getters[name]; |
| 60 if (getter != null && !getter.isStatic) return true; | 68 if (getter != null && !getter.isStatic) return true; |
| 61 var superclass = mirror.superclass; | 69 var superclass = _getSuperclass(mirror); |
| 62 if (superclass == mirror) return false; | 70 if (superclass == null) return false; |
| 63 return hasField(name, superclass); | 71 return hasField(name, superclass); |
| 64 } | 72 } |
| 65 | 73 |
| 66 /** | 74 /** |
| 67 * Return a list of all the public getters of a class which have corresponding | 75 * Return a list of all the public getters of a class which have corresponding |
| 68 * setters. | 76 * setters. |
| 69 */ | 77 */ |
| 70 Iterable<MethodMirror> publicGettersWithMatchingSetters(ClassMirror mirror) { | 78 Iterable<MethodMirror> publicGettersWithMatchingSetters(ClassMirror mirror) { |
| 71 var setters = mirror.setters; | 79 var setters = mirror.setters; |
| 72 return publicGetters(mirror).where((each) => | 80 return publicGetters(mirror).where((each) => |
| 73 setters["${each.simpleName}="] != null); | 81 setters["${each.simpleName}="] != null); |
| 74 } | 82 } |
| 75 | 83 |
| 76 /** | 84 /** |
| 77 * A particularly bad case of polyfill, because we cannot yet use type names | 85 * A particularly bad case of polyfill, because we cannot yet use type names |
| 78 * as literals, so we have to be passed an instance and then extract a | 86 * as literals, so we have to be passed an instance and then extract a |
| 79 * ClassMirror from that. Given a horrible name as an extra reminder to fix it. | 87 * ClassMirror from that. Given a horrible name as an extra reminder to fix it. |
| 80 */ | 88 */ |
| 81 ClassMirror turnInstanceIntoSomethingWeCanUse(x) => reflect(x).type; | 89 ClassMirror turnInstanceIntoSomethingWeCanUse(x) => reflect(x).type; |
| OLD | NEW |