OLD | NEW |
| 1 // Copyright (c) 2014, 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. |
1 library polymer.src.common.declarations; | 4 library polymer.src.common.declarations; |
2 | 5 |
3 import 'package:reflectable/reflectable.dart'; | 6 import 'package:reflectable/reflectable.dart'; |
| 7 import '../../polymer_micro.dart'; |
4 | 8 |
5 typedef bool _WhereFn(String name, DeclarationMirror declaration); | 9 List<ClassMirror> mixinsFor(Type type, Reflectable reflectionClass, |
| 10 {bool where(ClassMirror mirror)}) { |
| 11 var typeMirror = _reflect(type, reflectionClass); |
| 12 var mixins = []; |
| 13 var superClass = _getSuper(typeMirror); |
| 14 while (superClass != null && superClass.mixin.reflectedType != PolymerMixin) { |
| 15 var mixin = superClass.mixin; |
| 16 if (mixin != superClass && (where == null || where(mixin))) { |
| 17 mixins.add(mixin); |
| 18 } |
| 19 superClass = _getSuper(superClass); |
| 20 } |
| 21 return mixins.reversed.toList(); |
| 22 } |
6 | 23 |
7 Map<String, DeclarationMirror> declarationsFor( | 24 Map<String, DeclarationMirror> declarationsFor( |
8 Type type, Reflectable reflectionClass, {_WhereFn where}) { | 25 Type type, Reflectable reflectionClass, |
9 var typeMirror; | 26 {bool where(String name, DeclarationMirror declaration)}) { |
10 try { | 27 var typeMirror = _reflect(type, reflectionClass); |
11 typeMirror = reflectionClass.reflectType(type); | |
12 } catch (e) { | |
13 throw 'type $type is missing the $reflectionClass annotation'; | |
14 } | |
15 var declarations = {}; | 28 var declarations = {}; |
16 var superClass = typeMirror; | 29 var superClass = typeMirror; |
17 while (superClass != null && superClass.reflectedType != Object) { | 30 while (superClass != null && superClass.mixin.reflectedType != PolymerMixin) { |
18 superClass.declarations.forEach((name, declaration) { | 31 superClass.declarations.forEach((name, declaration) { |
19 if (declarations.containsKey(name)) return; | 32 if (declarations.containsKey(name)) return; |
20 if (where != null && !where(name, declaration)) return; | 33 if (where != null && !where(name, declaration)) return; |
21 declarations[name] = declaration; | 34 declarations[name] = declaration; |
22 }); | 35 }); |
23 superClass = _getSuper(superClass); | 36 superClass = _getSuper(superClass); |
24 } | 37 } |
25 return declarations; | 38 return declarations; |
26 } | 39 } |
27 | 40 |
| 41 ClassMirror _reflect(Type type, Reflectable reflectionClass) { |
| 42 try { |
| 43 return reflectionClass.reflectType(type); |
| 44 } catch (e) { |
| 45 throw 'type $type is missing the $reflectionClass annotation'; |
| 46 } |
| 47 } |
| 48 |
28 ClassMirror _getSuper(ClassMirror clazz) { | 49 ClassMirror _getSuper(ClassMirror clazz) { |
29 // Currently throws post-transform if superclass isn't annotated with a | 50 // Currently throws post-transform if superclass isn't annotated with a |
30 // [Reflectable] class. | 51 // [Reflectable] class. |
31 try { | 52 try { |
32 return clazz.superclass; | 53 return clazz.superclass; |
33 } catch(e) { | 54 } catch (e) { |
34 return null; | 55 return null; |
35 } | 56 } |
36 } | 57 } |
37 | 58 |
38 bool isFinal(DeclarationMirror declaration) { | 59 bool isFinal(DeclarationMirror declaration) { |
39 if (declaration is VariableMirror) return declaration.isFinal; | 60 if (declaration is VariableMirror) return declaration.isFinal; |
40 if (declaration is MethodMirror && declaration.isGetter) { | 61 if (declaration is MethodMirror && declaration.isGetter) { |
41 return !hasSetter(declaration); | 62 return !hasSetter(declaration); |
42 } | 63 } |
43 return false; | 64 return false; |
44 } | 65 } |
45 | 66 |
46 bool isProperty(DeclarationMirror declaration) { | 67 bool isProperty(DeclarationMirror declaration) { |
47 if (declaration is VariableMirror) return true; | 68 if (declaration is VariableMirror) return true; |
48 if (declaration is MethodMirror) return !isRegularMethod(declaration); | 69 if (declaration is MethodMirror) return !isRegularMethod(declaration); |
49 return false; | 70 return false; |
50 } | 71 } |
| 72 |
51 bool isRegularMethod(DeclarationMirror declaration) { | 73 bool isRegularMethod(DeclarationMirror declaration) { |
52 return declaration is MethodMirror && declaration.isRegularMethod; | 74 return declaration is MethodMirror && declaration.isRegularMethod; |
53 } | 75 } |
54 | 76 |
55 bool isSetter(DeclarationMirror declaration) { | 77 bool isSetter(DeclarationMirror declaration) { |
56 return declaration is MethodMirror && declaration.isSetter; | 78 return declaration is MethodMirror && declaration.isSetter; |
57 } | 79 } |
58 | 80 |
59 bool hasSetter(MethodMirror getterDeclaration) { | 81 bool hasSetter(MethodMirror getterDeclaration) { |
60 assert(getterDeclaration.isGetter); | 82 assert(getterDeclaration.isGetter); |
61 var owner = getterDeclaration.owner; | 83 var owner = getterDeclaration.owner; |
62 assert(owner is LibraryMirror || owner is ClassMirror); | 84 assert(owner is LibraryMirror || owner is ClassMirror); |
63 return owner.declarations.containsKey('${getterDeclaration.simpleName}='); | 85 return owner.declarations.containsKey('${getterDeclaration.simpleName}='); |
64 } | 86 } |
OLD | NEW |