OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 /** |
| 6 * Provides some additional convenience methods on top of the basic mirrors |
| 7 */ |
| 8 library mirrors_helpers; |
| 9 |
| 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. |
| 12 import 'dart:mirrors'; |
| 13 export 'dart:mirrors'; |
| 14 import 'serialization_helpers.dart'; |
| 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 |
| 24 /** |
| 25 * Return a list of all the public fields of a class, including inherited |
| 26 * fields. |
| 27 */ |
| 28 Iterable<VariableMirror> publicFields(ClassMirror mirror) { |
| 29 var mine = mirror.declarations.values.where( |
| 30 (x) => x is VariableMirror && !(x.isPrivate || x.isStatic)); |
| 31 var mySuperclass = _getSuperclass(mirror); |
| 32 if (mySuperclass != null) { |
| 33 return append(publicFields(mySuperclass), mine); |
| 34 } else { |
| 35 return new List<VariableMirror>.from(mine); |
| 36 } |
| 37 } |
| 38 |
| 39 /** Return true if the class has a field named [name]. Note that this |
| 40 * includes private fields, but excludes statics. */ |
| 41 bool hasField(Symbol name, ClassMirror mirror) { |
| 42 if (name == null) return false; |
| 43 var field = mirror.declarations[name]; |
| 44 if (field is VariableMirror && !field.isStatic) return true; |
| 45 var superclass = _getSuperclass(mirror); |
| 46 if (superclass == null) return false; |
| 47 return hasField(name, superclass); |
| 48 } |
| 49 |
| 50 /** |
| 51 * Return a list of all the getters of a class, including inherited |
| 52 * getters. Note that this allows private getters, but excludes statics. |
| 53 */ |
| 54 Iterable<MethodMirror> publicGetters(ClassMirror mirror) { |
| 55 var mine = mirror.declarations.values.where( |
| 56 (x) => x is MethodMirror && x.isGetter && !(x.isPrivate || x.isStatic)); |
| 57 var mySuperclass = _getSuperclass(mirror); |
| 58 if (mySuperclass != null) { |
| 59 return append(publicGetters(mySuperclass), mine); |
| 60 } else { |
| 61 return new List<MethodMirror>.from(mine); |
| 62 } |
| 63 } |
| 64 |
| 65 /** Return true if the class has a getter named [name] */ |
| 66 bool hasGetter(Symbol name, ClassMirror mirror) { |
| 67 if (name == null) return false; |
| 68 var getter = mirror.declarations[name]; |
| 69 if (getter is MethodMirror && getter.isGetter && !getter.isStatic) { |
| 70 return true; |
| 71 } |
| 72 var superclass = _getSuperclass(mirror); |
| 73 if (superclass == null) return false; |
| 74 return hasField(name, superclass); |
| 75 } |
| 76 |
| 77 /** |
| 78 * Return a list of all the public getters of a class which have corresponding |
| 79 * setters. |
| 80 */ |
| 81 Iterable<MethodMirror> publicGettersWithMatchingSetters(ClassMirror mirror) { |
| 82 var declarations = mirror.declarations; |
| 83 return publicGetters(mirror).where((each) => |
| 84 // TODO(alanknight): Use new Symbol here? |
| 85 declarations["${each.simpleName}="] != null); |
| 86 } |
| 87 |
| 88 /** |
| 89 * Given either an instance or a type, returns the type. Instances of Type |
| 90 * will be treated as types. Passing in an instance is really just backward |
| 91 * compatibility. |
| 92 */ |
| 93 ClassMirror turnInstanceIntoSomethingWeCanUse(x) { |
| 94 if (x is Type) return reflectClass(x); |
| 95 return reflect(x).type; |
| 96 } |
OLD | NEW |