| 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 Iterable<VariableMirror> publicFields(ClassMirror mirror) { |
| 21 var mine = mirror.variables.values.where( | 21 var mine = mirror.variables.values.where( |
| 22 (x) => !(x.isPrivate || x.isStatic)).toList(); | 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. */ |
| (...skipping 25 matching lines...) Expand all Loading... |
| 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 Iterable<MethodMirror> publicGettersWithMatchingSetters(ClassMirror mirror) { |
| 69 var setters = mirror.setters; | 69 var setters = mirror.setters; |
| 70 return publicGetters(mirror).where((each) => | 70 return publicGetters(mirror).where((each) => |
| 71 setters["${each.simpleName}="] != null).toList(); | 71 setters["${each.simpleName}="] != null); |
| 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 |